欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

头插法和尾插法分别建立链表(复制即可应用)

发布时间:2025/4/5 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 头插法和尾插法分别建立链表(复制即可应用) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
//头插法建立链表 #include <stdio.h> #include <malloc.h> typedef struct Node{int data;struct Node* next; }Node; int main(void){int i;Node* head,*p=NULL;head=(Node*)malloc(sizeof(Node));head->next=p;for(i=0;i<4;i++){p=(Node*)malloc(sizeof(Node));printf("输入:");scanf("%d",&p->data);p->next=head->next;head->next=p;}while(NULL!=head){printf("%d\n",head->data);head=head->next;} } //尾插法建立链表 #include <stdio.h> #include <malloc.h> typedef struct Node{int data;struct Node* next; }Node; int main(void){int i;Node* end=NULL;Node* p=NULL;Node* head=(Node*)malloc(sizeof(Node));end=head;for(i=0;i<4;i++){p=(Node*)malloc(sizeof(Node));printf("输入:");scanf("%d",&p->data);end->next=p;end=p;}end->next=NULL;while(NULL!=head){printf("%d\n",head->data);head=head->next;} }

总结

以上是生活随笔为你收集整理的头插法和尾插法分别建立链表(复制即可应用)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。