C语言链表复习
使用C语言实现链表及其基本功能,每种功能实现都使用了迭代写法和与其对应的一种或多种递归写法
#include<stdio.h>
#include<stdlib.h>
#define count 0
#define max_f(a, b) (a > b)? a : b
#define min_f(a, b) (a < b)? a : b
typedef struct Node{
int data;
struct Node * next;
}node;
typedef struct Node *link;
typedef link list;
void print (list p) // 打印链表中所有数据
{
while (p!=NULL) {
printf("%d ", p->data);
p=p->next;
}
putchar ('\n');
}
list create (char *s) // 创建一个链表
{
link cur;
list res;
if (*s=='\0') return NULL;
res=cur=(link) malloc (sizeof (node));
cur->data = *s - '0';
s++;
while (*s!='\0') {
cur=cur->next=(list) malloc(sizeof (node));
cur->data=*s - '0';
s++;
}
cur->next = NULL;
return res;
}
/*链表的创建
1,-双头节点创建- 创建两个相同头节点指针,并动态malloc分配一个地址给其一,操作其中一个头节点,返回的另一个头节点即为所要的链表。
2,-头节点预处理- 因为已经提前创建了头节点,所以在进入下一步(循环)前,处理好头节点(赋值,next)
3,-循环-,处理节点(开辟内存,赋值)
4,-封尾- 尾节点加上p -> next = NULL;*/
int sum_1(list p){
// 迭代 求链表所有数据的和
link t = p;
int sum_all = 0;
while(t != NULL){
sum_all += t -> data;<