头插法和尾插法分别建立链表(复制即可应用)
生活随笔
收集整理的這篇文章主要介紹了
头插法和尾插法分别建立链表(复制即可应用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//頭插法建立鏈表
#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;}
}
總結
以上是生活随笔為你收集整理的头插法和尾插法分别建立链表(复制即可应用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 考前自学系列·计算机组成原理·计算机的硬
- 下一篇: 通俗理解卡尔曼滤波