【数据结构】单链表
发布时间:2021-03-30 07:27 所属栏目:53 来源:网络整理
导读:#include stdio.h#include stdlib.h#include STRING.Htypedef struct LinkNode{int date; //数据域struct LinkNode * next;}linkNode;typedef struct Linklist{int length; linkNode *head;}linklist;linklist *Creatlist(){linklist *list;list=(linklist *
#include <stdio.h> #include <stdlib.h> #include <STRING.H> typedef struct LinkNode { int date; //数据域 struct LinkNode * next; }linkNode; typedef struct Linklist { int length; linkNode *head; }linklist; linklist *Creatlist() { linklist *list; list=(linklist *)malloc(sizeof(linklist)); memset(list,sizeof(linklist)); list->length=0; list->head=(linkNode*)malloc(sizeof(linkNode)); list->head->next=NULL; return list; } int insertlist(linklist *s,linkNode item) { linkNode *n; if(s==NULL) return 0; n=(linkNode*)malloc(sizeof(linkNode)); n->date=item.date; n->next=s->head->next; //头插法 s->head->next=n; s->length++; return 1; } int dellist(linklist *s,int pos) { int i=0; linkNode *p=s->head->next; linkNode *pa=s->head; if(s==NULL) return 0; while(i!=pos) { pa=p; p=p->next; } pa->next=p->next; free(p); s->length--; return 1; } void display(linklist *s) { int i=0; linkNode *p=s->head->next; while(i<s->length) { printf("%d ",p->date); p=p->next; i++; } printf("\n"); } int main() { linklist *list=NULL; int i; list=Creatlist(); for(i=0;i<10;i++) { linkNode n; n.date=i; insertlist(list,n); } display(list); for(i=0;i<10;i++) { dellist(list,0); display(list); } return 0; } <img src="http://img.blog.csdn.net/20150118213800491?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYTc4MTU1ODA2Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> <span style="white-space:pre"> </span> (编辑:ASP站长网) |
相关内容
网友评论
推荐文章
热点阅读