顺序构造单链表
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{//指向結構體成員 必然是結構體指針
? int data;
? struct LNode *next;
}LNode,*LinkList;
?
void InitList(LinkList &L){//構造空線性表L
?? L = (LinkList)malloc(sizeof(struct LNode));//產生頭結點 L
?? if(!L)//存儲分配失敗
????? exit(0);
?? L -> next = NULL; //頭結點指針域為空
}
void CreateList(LinkList &L){
?int i;
??? LinkList pnew,pa;
?pa = L;
??? printf("please enter a number or q exit:");
??? while(scanf("%d",&i)){
????? pnew = (LinkList)malloc(sizeof(LNode)) ;
????? pnew -> data =i;
????? pnew->next = NULL;
????? pa ->next = pnew;
????? pa = pnew;
???? printf("please enter a number or q exit:");
???
???? }
}
void output(LinkList L){
?
?L = L->next;
?while(L){
??printf("%d\n",L->data);
?? L = ?L->next;
?
?}
}
void DestroyList(LinkList &L){
?? LinkList q;
?? while(L){
???? q = L -> next;
???? free(L);
???? L = q;
}
}
int main(){
?LinkList L;
?InitList(L);
?CreateList(L);
?output(L);
?DestroyList(L);
?return 0;
}
轉載于:https://www.cnblogs.com/gaoanchen/p/gac.html
總結
- 上一篇: JSTL1.1函数标签库(functio
- 下一篇: 费马小定理与素数判定