我昨晚弄到好晚改的 是好多错 但改的只剩3个了 帮我再看看啊 红色的 拜托了~~~!!!
#include<stdio.h>
#include<malloc.h>
menu()
{
printf(" ___________________________");
printf(" |(1)Creat a new linklist. |\n");
printf(" |(2)Locate. |\n");
printf(" |(3)Get element. |\n");
printf(" |(4)The length of linklist. |\n");
printf(" |(5)Inside an element. |\n");
printf(" |(6)Delete an element. |\n");
printf(" |(7)EXIT. |\n");
printf(" |___________________________|\n");
printf("Please input the number of your choose.\n");
}
int choose()
{
int a;
scanf("%d",&a);
return(a);
}
typedef struct Node
{ char data;
struct Node *next;
}Node,*LinkList;
Node *CreatL()
{
LinkList L;
Node *r,*s;
int flag=1;
char c;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
r=L;
while(flag)
{
c=getcher();
if(c!='$')
{ s=(Node *)malloc(sizeof(Node));
s->data=c;
r->next=s;
r=s;
}
else
{ flag=0;
r->next=NULL;
}
}
return L;
}
Node *Locate(LinkList L)
{
Node *p;
int b=1;
char i;
scanf("%c",&i);
p=L->next;
while(p!=NULL)
if(p->data!=i)
{
p=p->next;
b++;
}
else break;
printf("This element's locate is %d",b);
}
Node *Get(LinkList L)
{
int j,i;
char a;
Node *p;
scanf("%d",&i);
p=L;
j=0;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(i==j)
{
a=p->data;
printf("The i'th element is %c",a);
}
else printf("The i is wrong.\n");
}
int Listlength(LinkList L)
{
Node *p;
int j;
p=L->next;
j=0;
while(p!=NULL)
{
p=p->next;
j++;
}
printf("This Linklist's length is %d",j);
}
int InsList(LinkList L,int i,char 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("The i is wrong!\n");
return 0;
}
s=(Node *)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;
pre->next=s;
return 1;
}
int DelList(LinkList L,int i2,char e2)
{
Node *p,*r;
int k;
p=L;
k=0;
while(p->next!=NULL&&k<i2-1)
{
p=p->next;
k=k+1;
}
if(k!=i2-1)
{
printf("The position is wrong!\n");
return 0;
}
r=p->next;
p->next=p->next->next;
e2=r->data;
free(r);
return 1;
}
void Print_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
printf("%c--->",p->data);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}
void Free_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
L->next=p->next;
free(p);
p=L->next;
}
free(L);
}
main()
{
char c,e,e2;
LinkList L;
int i,i2,x=1,y,*p;
printf("Please creat a linklist.");
printf("Please input something and enter '$'to end.\n");
L=CreatL();
while(x)
{
menu();
y=choose();
if(y>0&&y<8)
{
if(y==1) { printf("please input something and enter '$' to end.\n");
L=CreatL();
}
else
if(y==2) { printf("Please enter the element which you find.\n");
Locate(L);
}
else
if(y==3)
{ printf("Please enter the position of the element which you find.\n");
Get(L);
}
else
if(y==4) Listlength(L);
else
if(y==5)
{
printf("Please input the element e and the locate i.\n");
printf("i="); scanf("%d",&i);
printf("\ne="); scanf("%c",&e);
printf("\n");
L=InsList(L, int i , char e);
printf("The new LinkList is:\n");
Print_Node(L);
}
else
if(y==6)
{
printf("Please input the element e and the locate i.\n");
printf("i2="); scanf("%d",&i2);
printf("\ne2="); scanf("%c",&e2);
printf("\n");
L=DelList(L, int i2 , char e2);
printf("The new LinkList is:\n");
Print_Node(L);
}
else if(y==7) return(1);
printf("(1)Go on (0)EXIT\n");
scanf("%d",&x);
}
else printf("ERROR");
}
Free_Node(L);
}
[此贴子已经被作者于2007-9-29 8:22:36编辑过]