正文

线性表的基本操作2005-05-08 13:47:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/899.html

分享到:

#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct *next;
}node,*link;
link L;
int  inital_linklist(link S)
{link head,new;
char ch;
int num=1;
head=NULL;
S=(node *)malloc(sizeof(node));
if(!S)
{
printf("overflow!");
  exit(0);
}
S->next=head;
head=S;
printf("please input the data:\n");
scanf("%d",&head->data);
printf("continue?Y or N?\n");
scanf("%c",ch);
while(ch!='N'||'n')
{
new=(node *)malloc(sizeof(node));
if(!new)
  {
  printf("overflow!");
  exit(0);
}
new->next=head;
head=new;
printf("please input the data:\n");
scanf("%d",&head->data);
num++;
printf("continue?Y or N?\n");
scanf("%c",ch);
}
printf("succeed in create a list!\n");
return num;
}
void output_linklist(link S)
{
printf("the original elems in the list are:\n");
while(S->next!=NULL)
{
  printf("%d",S->data);
  printf("\n");
S=S->next;
}
}
int insert_list(link S,int x)
{int n,i=1;
link p,new;
printf("where do you insert ?before which elem:\n");
scanf("%d",&n);
while(n>x||n<0)
{
printf("error due to the wrong input!please input it again!\n");
scanf("%d",&n);
}
while(i<n)
{
S=S->next;
i++;
}
p=S;
new=(node *)malloc(sizeof(node));
if(!new)
  {
  printf("overflow!");
  exit(0);
}
p->next=new;
new->next=S->next;
printf("please input the new node's data:\n");
scanf("%d",&new->data);
printf("You have added a new node!\n");
x=x+1;
return x
}
int delete_linklist(link S,int y)
{
int i,j=1;
link q;
printf("which elem do you want to delete!number:\n");
scanf("%d",&i);
while(i>y||i<0)
{
printf("error due to the wrong input!please input it again!\n");
scanf("%d",&i);
}
while(j<i)
{
S=S->next;
j++;
}
q=S;
q->next=S->next->next;
S=S->next;
free(S);
printf("You have deleted a node!\n");
y--;
return y;
}
main()
{
int m;
m=inital_linklist(L);
output_linklist(L);
m=insert_list(L,m);
m=delete_linklist(L,m);
}



阅读(3908) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册