level 1
无灬名氏gIN
楼主
我将上面这段代码放到我宿友电脑里的code blocks结果完美运行,但在我的电脑和我同学的电脑创建链表后就不会再有下一次功能呢显示了,我室友的code blocks是我帮他装的,跟我的应该没有区别,这是为什么?



#include<stdio.h>
#include<stdlib.h>
#define N
#include<malloc.h>
#include<string.h>
typedef struct Node
{
int data;
struct Node *next;
}*prt_Node, Node; //起别名
typedef enum Status//枚举法返回成功和失败
{
SUCCESS,
ERROR
}Status;
prt_Node create(int i) //创建链表,必须使用Status做函数类型才能return success和error //
{
int j,data;
struct Node *p,*L;
L=(struct Node*)malloc(sizeof(Node));
if(!L)
printf("ERROR\n");
for(j=0;j<i;j++)
{
p=(struct Node*)malloc(sizeof(Node));
scanf("%d",&data);
p->data=data;
p->next=L->next;
L->next=p;
}
}
prt_Node Search(prt_Node L,int i)//查找元素
{
int j,e;
struct Node *p;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
printf("ERROR\n");
e=p->data;
}
Status Insert(prt_Node L,int i,int num)//插入指针
{
int j;
struct Node *p,*s;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
s=(struct Node*)malloc(sizeof(Node));
s->data=num;
s->next=p->next;
p->next=s;
return SUCCESS;
}
Status Delete(struct Node *L,int i)//删除单个指针
{
int j,e;
prt_Node p,q;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return SUCCESS;
}
Status Clear(prt_Node L)//整表删除
{
int j;
struct Node *p,*q;
p=L->next;
while(p)
{
q=p->next;
free(q);
p=q;
}
L->next=NULL;
return SUCCESS;
}
Status Exchange(int data,int e)//更改元素值//
{
int i=1;
struct Node *p;
while (!p)
{
p=p->next;
i++;
if(p->data==data)
{
p->data=e;
break;
}
}
return SUCCESS;
}
void print(prt_Node head)
{
prt_Node p;
p=head->next;
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\nd");
}
int main()
{
int i=1,a,n,index,data1,data2,data3,data4,data5,data6,data7;
prt_Node head;
while(i)
{
printf("****请选择操作类型****\n");
printf("1.创建一个链表。\n");
printf("2.输出该链表。\n");
printf("3.查找指定元素。\n");
printf("4.插入一个元素。\n");
printf("5.删除一个元素。\n");
printf("6.删除整个链表。\n");
printf("7.更改特定元素。\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("你想创建一个多长的链表?\n");
scanf("%d",&data1);
printf("请输入数据:\n");
head=create(n);
printf("链表输出如下:\n");
print(head);
break;
case 2:
printf("链表输出如下:\n");
print(head);
break;
case 3:
printf("你想查找哪个元素?\n");
scanf("%d",&index);
Search(head,index);
printf("你所查找的元素在第%d位\n",Search(head,index));
break;
case 4:
printf("你想插入一个什么数据?\n");
scanf("%d",&data3);
printf("你想将此数据插入到哪个位置?\n");
scanf("%d",&data4);
Insert(head,data4,data3);
printf("插入后数据输出如下:\n");
print(head);
break;
case 5:
printf("你想删除哪个数据?\n");
scanf("%d",&data5);
Delete(head,data5);
printf("删除后链表输出如下:\n");
print(head);
break;
case 6:
printf("数据已删除\n");
Clear(head);
break;
case 7:
printf("你想更改哪个元素?\n");
scanf("%d",&data6);
printf("你想把它更改为什么?\n");
scanf("%d",&data7);
Exchange(data6,data7);
printf("更改后输出如下:\n");
print(head);
break;
default:
printf("ERROR");
}
}
system("pause");
return 0;
}
2018年04月10日 13点04分
1



#include<stdio.h>#include<stdlib.h>
#define N
#include<malloc.h>
#include<string.h>
typedef struct Node
{
int data;
struct Node *next;
}*prt_Node, Node; //起别名
typedef enum Status//枚举法返回成功和失败
{
SUCCESS,
ERROR
}Status;
prt_Node create(int i) //创建链表,必须使用Status做函数类型才能return success和error //
{
int j,data;
struct Node *p,*L;
L=(struct Node*)malloc(sizeof(Node));
if(!L)
printf("ERROR\n");
for(j=0;j<i;j++)
{
p=(struct Node*)malloc(sizeof(Node));
scanf("%d",&data);
p->data=data;
p->next=L->next;
L->next=p;
}
}
prt_Node Search(prt_Node L,int i)//查找元素
{
int j,e;
struct Node *p;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
printf("ERROR\n");
e=p->data;
}
Status Insert(prt_Node L,int i,int num)//插入指针
{
int j;
struct Node *p,*s;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
s=(struct Node*)malloc(sizeof(Node));
s->data=num;
s->next=p->next;
p->next=s;
return SUCCESS;
}
Status Delete(struct Node *L,int i)//删除单个指针
{
int j,e;
prt_Node p,q;
p=L->next;
j=1;
while (p||j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
return ERROR;
q=p->next;
p->next=q->next;
e=q->data;
free(q);
return SUCCESS;
}
Status Clear(prt_Node L)//整表删除
{
int j;
struct Node *p,*q;
p=L->next;
while(p)
{
q=p->next;
free(q);
p=q;
}
L->next=NULL;
return SUCCESS;
}
Status Exchange(int data,int e)//更改元素值//
{
int i=1;
struct Node *p;
while (!p)
{
p=p->next;
i++;
if(p->data==data)
{
p->data=e;
break;
}
}
return SUCCESS;
}
void print(prt_Node head)
{
prt_Node p;
p=head->next;
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
printf("\nd");
}
int main()
{
int i=1,a,n,index,data1,data2,data3,data4,data5,data6,data7;
prt_Node head;
while(i)
{
printf("****请选择操作类型****\n");
printf("1.创建一个链表。\n");
printf("2.输出该链表。\n");
printf("3.查找指定元素。\n");
printf("4.插入一个元素。\n");
printf("5.删除一个元素。\n");
printf("6.删除整个链表。\n");
printf("7.更改特定元素。\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("你想创建一个多长的链表?\n");
scanf("%d",&data1);
printf("请输入数据:\n");
head=create(n);
printf("链表输出如下:\n");
print(head);
break;
case 2:
printf("链表输出如下:\n");
print(head);
break;
case 3:
printf("你想查找哪个元素?\n");
scanf("%d",&index);
Search(head,index);
printf("你所查找的元素在第%d位\n",Search(head,index));
break;
case 4:
printf("你想插入一个什么数据?\n");
scanf("%d",&data3);
printf("你想将此数据插入到哪个位置?\n");
scanf("%d",&data4);
Insert(head,data4,data3);
printf("插入后数据输出如下:\n");
print(head);
break;
case 5:
printf("你想删除哪个数据?\n");
scanf("%d",&data5);
Delete(head,data5);
printf("删除后链表输出如下:\n");
print(head);
break;
case 6:
printf("数据已删除\n");
Clear(head);
break;
case 7:
printf("你想更改哪个元素?\n");
scanf("%d",&data6);
printf("你想把它更改为什么?\n");
scanf("%d",&data7);
Exchange(data6,data7);
printf("更改后输出如下:\n");
print(head);
break;
default:
printf("ERROR");
}
}
system("pause");
return 0;
}