线性子系统C语言,线性表子系统C语言版

数据结构

#include

#include

typedef struct linknode

{

char data;

struct linknode *next;

}linknode;

linknode *head;

int n;

//建表

void creatlist()

{ linknode *p,*s;

char x;

int z=1;

head=(linknode*)malloc(sizeof(linknode));

p=head;

printf("\n\t\t 创建线性表");

printf("\n\t\t 请逐个输入字符,以'x'结尾!\n");

while(z)

{printf("\t\t 输入字符:");

scanf("%c",&x);getchar();

if(x!='x')

{s=(linknode *)malloc(sizeof(linknode));

n++;

s->data=x;

p->next=s;

s->next=NULL;

p=s;}

else z=0;

}

}

/*{ char x;

linknode *s, *r;

head=NULL;

r=NULL;

n=0;

printf("\n\t\t输入结点,以x为结束标记!");

printf("\n");

scanf("%c",&x);getchar();

while(x!='x')

{

s=(linknode*)malloc(sizeof(linknode));

n++;

s->data=x;

if(head==NULL) head=s;

else r->next=s;

r=s;

scanf("%c",&x);

}

if(r!=NULL) r->next=NULL;

}*/

//插入

void insertdata(int i,char x)

{

linknode *p,*s;

int j=1;

p=head;

while(p!=NULL && j

{

j++;

p=p->next;

}

if(p!=NULL)//此时j==i

{

s=(linknode*)malloc(sizeof(linknode));

s->data=x;

s->next=p->next;

p->next=s;

n++;

printf("插入成功!");

}

else printf(" 线性表为空或位置超出!");

}

//删除节点

void DelList(char x)

{

linknode *p,*q;

if(head==NULL)

{

printf("\n\t\t链表下溢! ");

return;

}

if(head->next==NULL)

{

printf("\n\t\t线性表已经为空! ");

return;

}

q=head;

p=head->next;

while(p!=NULL&&p->data!=x)

{

q=p;

p=p->next;

}

if(p!=NULL)

{

q->next=p->next;//前驱指向P的后继

free(p);

n--;

printf("\n\t\t结点%c已经被删除!",x);

}

else

printf("\n\t\t抱歉!没有找到您要删除的结点.");

}

//显示数据

void showlist()

{

linknode *p;

p=head;

printf("\n\t\t显示线性表所有元素:\n");

if(head->next==NULL||p==NULL)

printf("\n\t\t链表为空");

else

{

printf("\n\t\t");

while(p->next!=NULL)

{

printf("%7c",p->next->data);

p=p->next;

}

}

}

//查找

void searchlist(char x)

{

linknode *p;

int i=1;

if(head==NULL)

{

printf("\n\t\t链表下溢");

return;

}

if(head->next==NULL)

{

printf("\n\t\t线性表为空,没有任何结点");

return;

}

p=head->next;

while(p!=NULL&&p->data!=x)

{

p=p->next;

i++;

}

if(p!=NULL)//此时p->data==x

{

printf("\n\t\t在表的第%d位上找到值为%c的结点!",i,x);

}

else

printf("\n\t\t没找到值为%c的结点!",x);

}

void main()

{

int choice,i,j;

char x;

head=NULL;

j=1;

while(j)

{

prin

tf("\n");

printf("\n\t\t 线性表子系统 ");

printf("\n\t\t*************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值