数据结构部分题目(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];
}
相关推荐
时见先生21 小时前
Python库和conda搭建虚拟环境
开发语言·人工智能·python·自然语言处理·conda
a努力。21 小时前
国家电网Java面试被问:混沌工程在分布式系统中的应用
java·开发语言·数据库·git·mysql·面试·职场和发展
Yvonne爱编码21 小时前
Java 四大内部类全解析:从设计本质到实战应用
java·开发语言·python
tobias.b21 小时前
408真题解析-2010-6-数据结构-哈夫曼树
数据结构·计算机考研·408真题解析
wqwqweee21 小时前
Flutter for OpenHarmony 看书管理记录App实战:搜索功能实现
开发语言·javascript·python·flutter·harmonyos
yongui478341 天前
基于MATLAB的NALM锁模光纤激光器仿真实现
开发语言·matlab
tobias.b1 天前
408真题解析-2010-7-数据结构-无向连通图
数据结构·算法·图论·计算机考研·408真题解析
-To be number.wan1 天前
Python数据分析:numpy数值计算基础
开发语言·python·数据分析
沃尔特。1 天前
直流无刷电机FOC控制算法
c语言·stm32·嵌入式硬件·算法
Cx330❀1 天前
【优选算法必刷100题】第038题(位运算):消失的两个数字
开发语言·c++·算法·leetcode·面试