数据结构部分题目(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];
}
相关推荐
小魏每天都学习12 分钟前
【算法——c/c++]
c语言·c++·算法
lly20240643 分钟前
jQuery Mobile 表格
开发语言
智码未来学堂1 小时前
探秘 C 语言算法之枚举:解锁解题新思路
c语言·数据结构·算法
惊讶的猫1 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
m0_748233171 小时前
30秒掌握C++核心精髓
开发语言·c++
Fleshy数模2 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
Duang007_2 小时前
【LeetCodeHot100 超详细Agent启发版本】字母异位词分组 (Group Anagrams)
开发语言·javascript·人工智能·python
froginwe112 小时前
Redis 管道技术
开发语言
u0109272712 小时前
C++中的RAII技术深入
开发语言·c++·算法
青桔柠薯片2 小时前
数据结构:顺序表与链表
数据结构·链表