c语言栈队列链表算法代码实现整理.pdfVIP

  • 6
  • 0
  • 约7.53千字
  • 约 8页
  • 2021-12-10 发布于福建
  • 举报
#includestdio.h #define null 0 #define len sizeof(struct lnode) int n; struct lnode *creatlist(); struct lnode *listinsert(); struct lnode *listdel(); struct lnode{ int a; struct lnode *next; }; struct lnode *head; void main() { int n; do{ printf(===== 链式表练习 =====\n); printf( 请选择操作: \n); printf( 1、建立链式表 \n); printf( 2 、插入新元素 \n); printf( 3 、删除元素 \n); printf(====================\n); scanf(%d,n); switch(n) { case 1:creatlist();break; case 2:listinsert();break; case 3:listdel();break; default:printf( 选择错误,请确认输入! \n);break; } }while(1); } struct lnode *creatlist()// 建立链表 { struct lnode *p1,*p2,*p0; n=0; head=null; p1=(struct lnode *)malloc(len); printf( 请输入初始元素: \n); scanf(%d,p1-a); p1-next=null; while(p1-a!=0) { ++n; if(n==1) head=p1; else p2-next=p1; p2=p1; p1=(struct lnode *)malloc(len); scanf(%d,p1-a); p1-next=null; } free(p1); printf( 建立链表成功! \n); printf( 建立的链表为: \n); p0=head; do { printf(%d ,p0-a); p0=p0-next; }while(p0!=null); printf(\n); free(p0); return(head); } struct lnode *listinsert()// 插入链表元素 { int i,j=1; struct lnode *p,*q,*p0; p=p0=head; q=(struct lnode *)malloc(len); printf( 请选择插入的位置: \n); scanf(%d,i); printf( 请输入要插入元素的值: \n); scanf(%d,q-a); while(ji-1) { p=p-next; j++; } q-next=p-next; p-next=q; free(p); printf( 插入成功! \n)

文档评论(0)

1亿VIP精品文档

相关文档