创建单链表并遍历
以下函數createlink的功能是創建帶頭結點的單鏈表,并輸入n個整數,依次為各節點的數據域賦值。
#include<stdio.h> #include<stdlib.h>typedef struct Node{int data;struct Node* next; }A;A *createlink(int n) {A *h=NULL,*p,*s;int i,x;p=(A*) malloc(sizeof(A));h=p;p->next=NULL;for(i=1;i<=n;i++){s=(A*)malloc(sizeof(A));scanf("%d",&x);s->data=x;s->next=p->next;p->next=s;p=s;}return h; }void outlink(A *h) {A *p;p=h->next;printf("the list is:");while(p){printf("->%d",p->data);p=p->next;} }int main(){A *head;head = createlink(4);outlink(head);} 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: (C语言)数组去重
- 下一篇: (一)Web Service——基本概念