数据结构部分题目(c语言版本)

1.反转链表

【1】代码思想:

1、设置三个结点,分别为pre、cur、temp。其中cur指向头节点处(cur=head),pre是cur的前面,temp是cur的后面。

2、先初始化(把pre和temp置空),然后使用一个循环,把pre和temp放到上面说的位置,然后让cur和pre换位置(cur->next=pre;pre=cur;)

3.还在循环内,将pre、cur和temp均往后移动,直到循环结束即可~

【2】代码

复制代码
//结构体定义
typedef struct node{
   int val;
   struct node *next;
}ListNode;

//单链表反转
ListNode* ReverseList(struct ListNode* head) {
   ListNode *pre=NULL,*cur=head,*temp=NULL;
   while(cur!=NULL){
    temp=cur->next;//保存当前结点的下一个结点
    cur->next=pre;//交换
    pre=cur;//交换
    cur=temp;//结点后移
  }
  return pre;
}

2.判断回文字符串:回文是正着反着都相等

复制代码
bool judge(char* str ) {
  int len=strlen(str);
  int i=0;
  int j=len-1;
  while(i<=j){
   if(str[i]!=str[j]){
   return false;
   }
   i++;
   j--;
  }
  return ture;
}

3.反转字符串

复制代码
char* solve(char* str ) {
  int len=strlen(str);
  int i=0;
  int j=len-1;
  while(i<=j){
   char temp=str[i];
   str[i]=str[j];
   str[j]=temp;
   i++;
   j--;
  }
  return str;
}

4.斐波那契数列:前两项为1,第三项开始,该项等于前两项的和

复制代码
int Fibonacci(int n ) {
  if(n==0||n==1){
    return 1;
  }
  int a[41];//n小于等于40
  a[1]=a[2]=1;
  for(int i=3;i<=n;i++){
   a[i]=a[i-1]+a[i-2];
  }
  return a[n];
}
相关推荐
青峰讲运维1 小时前
OpenStack Heat 编排:用模板一键交付云业务,告别手动点页面
开发语言·php·openstack
MyFreeIT2 小时前
Python Windows Manual
开发语言·python
kiracrimson2 小时前
STL:List的使用和模拟实现
开发语言·c++·list
二十七日秋2 小时前
算法刷题笔记「Day 1|四道 OJ 入门题里的深坑」
c语言·汇编·数据结构·c++·笔记·python·算法
北冥you鱼3 小时前
Go 语言 GORM 使用 Preload:从关联查询到嵌套预加载的完整实践
开发语言·jvm·golang
dingzd953 小时前
TikTok账号登录态失效的Cookie字段排查路径
开发语言·php·跨境电商·营销策略
abc light3 小时前
ARM 裸机开发(1)——ARM 体系结构
c语言·arm开发
曹牧3 小时前
C#:The operation has timed out
开发语言·c#
你看我名字3 小时前
数眼智能 Search + Reader 实战:联网问答怎样避免“搜到了,却引用错了”
android·java·开发语言
小嘉丶学长3 小时前
Day13 · 列表与元组
linux·运维·服务器·开发语言·python