头插法和尾插法分别建立链表(复制即可应用)
生活随笔
收集整理的这篇文章主要介绍了
头插法和尾插法分别建立链表(复制即可应用)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
//头插法建立链表
#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;}
}
总结
以上是生活随笔为你收集整理的头插法和尾插法分别建立链表(复制即可应用)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 考前自学系列·计算机组成原理·计算机的硬
- 下一篇: 通俗理解卡尔曼滤波