回 帖 发 新 帖 刷新版面

主题:链栈帮忙改改

#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);
}
 

回复列表 (共1个回复)

沙发

#include<stdio.h>
#include<stdlib.h>
typedef struct name   
{
int data;
struct name *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);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册