| GZU521.COM学习网 |
|
本课主题: 实验二 循环链表实验 教学目的: 掌握单向链表的实现方法 教学重点: 单向链表的存储表示及操作 教学难点: 单向链表的操作实现 授课内容: 一、单向链表的存储表示 c源程序 #include #include #include #define error 0 #define ok 1 #define equal 1 #define overflow -1 #define list_init_size 100 #define listincrement 10 struct stu{ char name[20]; char stuno[10]; int age; int score; }stu[50]; typedef struct stu elemtype; struct lnode { elemtype data; struct lnode *next; }; typedef struct lnode lnode; typedef struct lnode *linklist; int init(linklist *l) { *l=(lnode *)malloc(sizeof(lnode)); if(!l) exit(error); (*l)->next=null; return ok; }/*init */ int listlength(linklist l) { int j=0; while (l->next) { l=l->next; j++; } return j; } int getelem(linklist l,int i,elemtype *e) { linklist p; int j; p=l->next;j=1; while(p&&j p=p->next;++j; } if(!p||j>1) return error; *e=p->data; return ok; } int equallist(elemtype *e1,elemtype *e2) { if (strcmp(e1->name,e2->name)==0) return 1; else return 0; } int less_equallist(elemtype *e1,elemtype *e2) { if (strcmp(e1->name,e2->name)<=0) return 1; else return 0; } int locateelem(linklist la,elemtype e,int type) { int i; linklist p; p=la; switch (type) { case equal: while(p->next) { p=p->next; if(equallist(&p->data,&e)) return 1; } return 0; break; default: break; } return 0; } void mergelist(linklist la,linklist lb,linklist *lc) { linklist pa,pb,pc; pa=la->next;pb=lb->next; *lc=pc=la; while(pa && pb) { if(less_equallist(&pa->data,&pb->data)) { pc->next=pa;pc=pa;pa=pa->next; } else { pc->next=pb;pc=pb;pb=pb->next; } } pc->next=pa?pa:pb; free(lb); } int printlist(linklist l) { int i; linklist p; p=l; printf("name stuno age score\n"); while(p->next) { p=p->next; printf("%-10s %s\t%d\t%d\n", p->data.name, p->data.stuno, p->data.age, p->data.score); } printf("\n"); } int listinsert(linklist l,int i,elemtype e) { linklist p,s; int j; p=l;j=0; while(p&&j p=p->next; ++j; } if(!p||j>i-1) return error; s=(linklist)malloc(sizeof(lnode)); s->data=e; s->next=p->next; p->next=s; return ok; }/*listinsert before i */ main() { struct stu e; linklist la,lb,lc; clrscr(); printf("\n\n-------------------list demo is running...----------------\n\n"); printf("first is insertlist function.\n"); init(&la); |
责任编辑:gzu521