创建单链表并遍历
以下函數(shù)createlink的功能是創(chuàng)建帶頭結(jié)點(diǎn)的單鏈表,并輸入n個整數(shù),依次為各節(jié)點(diǎn)的數(shù)據(jù)域賦值。
#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);} 創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)