数据结构之链表(C语言)

HACKHT 2007-09-26 09:49:34
#include<stdio.h>
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}NODE;

void *InitList(NODE *L){
L=(NODE *)malloc(sizeof(NODE));
L->next=NULL;
return 0;
}


void *creat(NODE *L){
int m,i; NODE *q,*p ;
printf("qing shu ru shu mu:");
scanf("%d",&m);
printf("qing shu ru shu zi:");
q=(NODE *)malloc(sizeof(NODE));
q=L;
for(i=0;i<m;i++){
p=(NODE *)malloc(sizeof(NODE));
scanf("%d",&p->data);
p->next=NULL;
q->next=p;
q=p;}
printf("wan cheng\n");
return 0;
}


void *ListInsert(NODE *L){
int i,e,j; NODE *p,*s;
printf("Now you will push a number into the list !\n");
printf("Where do you want to push the number,pleast input the xuhao:");
scanf("%d",&i);
printf("Now the number will push before %d! \n",i);
printf("Please input your number:");
scanf("%d",&e); printf("\n");
p=L; j=0;
while(p!=NULL&&j<i-1){
p=p->next; ++j;}
if(p==NULL||j>i-1) return 0;
s=(NODE *)malloc(sizeof(NODE));
s->data=e; s->next=p->next;
p->next=s;
printf("The %d have been push into the list! \n",e);
return 0;
}

void *ListDel(NODE *L){
int i,j; NODE *p,*q;
printf("Now you will delete a number from the list! \n");
printf("Please input the number's xuhao :");
scanf("%d",&i); printf("\n");
p=L; j=0;
while(p->next!=NULL&&j<i-1){
p=p->next; ++j;}
if(p->next==NULL||j>i-1) return 0;
q=p->next; p->next=q->next;
printf("The %d is delete successfully!\n",q->data);
free(q);
return 0;
}


void PrintList(NODE *L){
NODE *p;
p=(NODE *)malloc(sizeof(NODE));
p=L->next;
while(p!=NULL){
printf("%5d",p->data);
p=p->next;}

}


void main(){
NODE *T; int n=0;
printf("1.InitList 2.ListInsert 3.ListDel 4.PrintfList 5.Exit \n");
while(n!=5){
printf("Please choose the gongneng:");
scanf("%d",&n);
switch(n){
case 1:
InitList(T);
creat(T);printf("\n");
break;
case 2:
ListInsert(T);
break;
case 3:
ListDel(T);
break;
case 4:
PrintList(T); printf("\n");
break;
case 5:
break;}
}
}
...全文
648 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
javabird1986 2008-04-02
  • 打赏
  • 举报
回复
呵呵,普及的意思很不错,但我还是不大会哦,LZ可以仔细教哈我吗,链表很痛苦,我的邮箱javaliuliu@126.com
manrenmanren 2007-09-26
  • 打赏
  • 举报
回复
也许吧 呵呵
ckt 2007-09-26
  • 打赏
  • 举报
回复
不是给你回过?

SNODE t;
SNODE* T = &t;
// T没有指向有效地址就传入使用,所以出错
xsir317 2007-09-26
  • 打赏
  • 举报
回复
难道LZ是来做新手普及工作的?
manrenmanren 2007-09-26
  • 打赏
  • 举报
回复
什么问题?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧