标题:[求助]:there is something wrong with my LinkList ? Please give a hand
只看楼主
毛毛小虫
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-11-1
 问题点数:0 回复次数:0 
[求助]:there is something wrong with my LinkList ? Please give a hand

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef struct Node
{ int data;
struct Node *next;
}Node, * LinkList;

int len;
LinkList pre;

LinkList CreateFromHead()
{
LinkList L;
Node *s;
int a,i;
int flag=1;
L=(LinkList)malloc(sizeof(Node));
L->next=NULL;

printf("请输入要创建线性表的长度\n");
scanf("%d",&a);
for(i=0;i<a;i++)
{
s=(Node*)malloc(sizeof(Node));
s->data=rand();
s->next=L->next;
L->next=s;
}
len = a;
return L;
}

int InsList(LinkList L,int i,int e)
{ Node *pre,*s;
int k;
pre=L;k=0;
while(pre!=NULL&&k<i+1)
{pre=pre->next;
k=k+1;
}
if(k!=i-1)
{printf("插入位置不合理!");
return ERROR;
}
s=(Node*)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;
pre->next=s;
return OK;
}

int DelList(LinkList L,int i,int *e)
{Node *p,*r;
int k;
p=L;k=0;
while(p->next!=NULL&&k<i-1)
{p=p->next;
k=k+1;
}
if (k!=i)
{printf("删除结点位置i不合理!");
return ERROR;
}
r=p->next;
p->next=p->next->next;
*e=r->data;
free(r);
--len;
return OK;
}

void main()
{
int c, i, j=0 , flag=0, *b, aa;
b = &aa;
LinkList S, ss;

do{
printf("头插法创建线性表请按1\n");

printf("插入元素请按4\n");
printf("删除元素请按5\n");
printf("退出请按\n");
scanf("%d",&c);
switch(c)
{
case 1:S=CreateFromHead();
ss=S;
printf("头插法创建线性表已经完成,创建的线性表为:\n");
for( i =0 ;i < len ; i++)
{
printf("%d ",S->next->data);
S = S->next;
}
printf("\n");
break;

case 4:
S=ss;
printf("请输入你要插到线性表中的位置及其数字:\n");
scanf("%d %d",&i,&j);
flag = InsList(ss,i-1,j);
if( flag == 1)
{
for( i =0 ;i < len; i++)
{
printf("%d ",S->data);
S = S->next;
}
}
break;
case 5:
printf("请输入你要删除的位置:\n");
scanf("%d",&i);
flag = DelList(ss,i-1,b);
if( flag == 1)
{
for( i =0 ;i < len; i++)
{
printf("%d ",S->data);
S = S->next;
}
}break;
default: break;

}
printf("\n");
}while(c!=5);
printf("*\n");
}
/*-----------Thanks!!!Sir------------- */

搜索更多相关主题的帖子: wrong something give hand Please 
2007-11-01 19:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-182646-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.121602 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved