一个简单的线性表
#include”stdio.h”
typedef struct linknode
{char data;
struct linknode *next;
}node;
node *head;
int n=0;
void CreateList()
{
node *p,*s;
char x;
int z=1;
head=new node;
p=head;
printf(“\n\t\t createlist”);
printf(“\n\t\t please input word,end’x'!\n”);
while(z)
{
printf(“\t\t input:”);
scanf(“%c”,&x);
getchar();
if(x!=’x')
{
s=new node;
n++;
s->data=x;
p->next=s;
s->next=NULL;
p=s;
}
else z=0;
}
}
void InsList(int i,char x)
{
node *s,*p;
int j;
s=new node;
n++;
s->data=x;
if(i==0)
{
s->next=head;
head=s;
}
else
{
p=head;j=1;
while(p!=NULL&&j<i)
{
j++;
p=p->next;
}
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else
printf(“\t\t no find!\n”);
}
}
void DelList(char x)
{
node *p,*q;
if(head==NULL)
{
printf(“\t\t lianbiaoxiayi!\n”);
return;
}
if(head->next==NULL)
{ printf(“\t\t linklist kong!”);
return;
}
q=head;
p=head->next;
while(p!=NULL&&p->data!=x)
{ q=p;
p=p->next;
}
if(p!=NULL)
{q->next=p->next;
delete p;
n–;
printf(“\t\t %c already DEL!”,x);
}
else
printf(“\t\t no find!\n”);
}
void ShowList()
{ node *p=head;
printf(“\n\t\t showlist:”);
if(head->next==NULL||p==NULL)
printf(“\n\t\t kong list!\n”);
else
{
printf(“\n\t\t”);
while(p->next!=NULL)
{printf(“%5c”,p->next->data);
p=p->next;
}
}
}
void main()
{ head=NULL;
int choise,i,j=1;
char x;
while(j)
{printf(“\n\n\n\n”);
printf(“\t\t\t linklist’s son\n”);
printf(“\n\t\t*********************”);
printf(“\n\t\t* 1—createlist *”);
printf(“\n\t\t* 2—inslist *”);
printf(“\n\t\t* 3—dellist *”);
printf(“\n\t\t* 4—showlist *”);
printf(“\n\t\t* 0—quit *”);
printf(“\n\t\t*********************”);
printf(“\n\t\t plsase chose(0—4):”);
scanf(“%d”,&choise);getchar();
if(choise==1)
CreateList();
else if (choise==2)
{ printf(“\n\t\t please input i and x:”); /*输入格式 例:“4,X”插入4*/
scanf(“%d,%c”,&i,&x);
InsList(i,x);
}
else if (choise==3)
{ printf(“\n\t\t please input del NO:”);
scanf(“%c”,&x);
DelList(x);
}
else if (choise==4)
if(head==NULL)
printf(“\n\t\t please createlist!”);
else
ShowList();
else if (choise==0)
j=0;
else
printf(“\n\t\t Wrong!!!\n”);
}
}
插入时程序不对,输入“4,x”时,显示的是“x”
有点迷茫。。。
是个子程序
主程序:
#include”stdio.h”
void Linklist()
{
}
void main(void)
{
int choice;
char ch;
ch=’y';
while(ch==’y')
{
printf(“\n\n\n\n\t\t system \n\n\n”);
printf(“\t\t list “);
printf(“\n\t\t******************”);
printf(“\n\t\t 1—linklist***”);
printf(“\n\t\t 2—stack ***”);
printf(“\n\t\t 3—queue ***”);
printf(“\n\t\t 4—string ***”);
printf(“\n\t\t 5—btree ***”);
printf(“\n\t\t 6—graph ***”);
printf(“\n\t\t 7—search ***”);
printf(“\n\t\t 8—sort ***”);
printf(“\n\t\t 0—quit ***”);
printf(“\n\t\t******************”);
printf(“\n\n\t\t chose(0—8):”);
scanf(“%d”,&choice);
getchar();
switch(choice)
{
case 1:Linklist();break;
case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
case 7:break;
case 8:break;
case 0:ch=’n';break;
default: printf(“wrong”);
}
}
}
怎样能让2个连在一起运行?
怎样在程序中加“2链表合并”???
>> 本文固定链接: http://www.vcgood.com/archives/1641