主题:[讨论]如何实现多次选择(单链表)
#include<stdio.h>
#include <stdlib.h>
#define null 0
typedef struct node
{
char data;
struct node *next;
}lnode;
lnode *create()
{
lnode *head,*tail,*p;
char x;
head=(lnode *)malloc(sizeof(lnode));
tail=head;
x=getchar();
while(x!='$')
{
p=(lnode *)malloc(sizeof(lnode));
p->data=x;
tail->next=p;
tail=p;
x=getchar();
}
tail->next=null;
return head;
}
void Insert(lnode *p,char x)
{
lnode *s=(lnode *)malloc(sizeof (lnode));
s->data=x;
s->next=p->next;
p->next=s;
}
void Delete(lnode *h,char x)
{
lnode *q,*p=h;
while(p&&p->next)
if(p->next->data==x)
{
q=p->next;
p->next=q->next;
free(q);
}
else
p=p->next;
}
lnode *nizhi(lnode *h)
{
lnode *back,*p,*front;
back=null;
p=h->next;
h->next=null;
while(p!=null)
{
front=p->next;
p->next=back;
back=p;
p=front;
}
h->next=back;
return h;
}
void print(lnode *h)
{
lnode *p=h->next;
while(p)
{
printf("%c ",p->data);
p=p->next;
}
}
void main()
{
lnode *x,*y;
int a,b,c;
printf("请输入字符串\n");
x=create();
printf("请选择您需要的功能:\t0---逆置\t1---插入\t2---删除");
a=getchar();
a=getchar();
switch(a)
{
case '0':
x=nizhi(x);
print(x);
printf("\n");
getchar();
break;
case '1':
printf("请输入将要插入的字母:");
c=getchar();
c=getchar();
Insert(x,c);
print(x);
printf("\n");
getchar();
break;
case '2':
printf("请输入将要删除的字母:");
b=getchar();
b=getchar();
Delete(x,b);
printf("\n");
getchar();
break;
}
printf("请选择\t0---逆置\t1---插入\t2---删除");
getchar();
}
怎样实现多次选择呢??
#include <stdlib.h>
#define null 0
typedef struct node
{
char data;
struct node *next;
}lnode;
lnode *create()
{
lnode *head,*tail,*p;
char x;
head=(lnode *)malloc(sizeof(lnode));
tail=head;
x=getchar();
while(x!='$')
{
p=(lnode *)malloc(sizeof(lnode));
p->data=x;
tail->next=p;
tail=p;
x=getchar();
}
tail->next=null;
return head;
}
void Insert(lnode *p,char x)
{
lnode *s=(lnode *)malloc(sizeof (lnode));
s->data=x;
s->next=p->next;
p->next=s;
}
void Delete(lnode *h,char x)
{
lnode *q,*p=h;
while(p&&p->next)
if(p->next->data==x)
{
q=p->next;
p->next=q->next;
free(q);
}
else
p=p->next;
}
lnode *nizhi(lnode *h)
{
lnode *back,*p,*front;
back=null;
p=h->next;
h->next=null;
while(p!=null)
{
front=p->next;
p->next=back;
back=p;
p=front;
}
h->next=back;
return h;
}
void print(lnode *h)
{
lnode *p=h->next;
while(p)
{
printf("%c ",p->data);
p=p->next;
}
}
void main()
{
lnode *x,*y;
int a,b,c;
printf("请输入字符串\n");
x=create();
printf("请选择您需要的功能:\t0---逆置\t1---插入\t2---删除");
a=getchar();
a=getchar();
switch(a)
{
case '0':
x=nizhi(x);
print(x);
printf("\n");
getchar();
break;
case '1':
printf("请输入将要插入的字母:");
c=getchar();
c=getchar();
Insert(x,c);
print(x);
printf("\n");
getchar();
break;
case '2':
printf("请输入将要删除的字母:");
b=getchar();
b=getchar();
Delete(x,b);
printf("\n");
getchar();
break;
}
printf("请选择\t0---逆置\t1---插入\t2---删除");
getchar();
}
怎样实现多次选择呢??