如题 这是我编的一段代码 在指定位置插入操作函数执行后遍历可以 但是一调用指定值插入和删除函数调用后就出问题了 麻烦大神求解
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
} *LIST;
//初始化链表
int InitList(LIST *L)
{
*L = (LIST)malloc(sizeof(LIST));
if(!(*L)){
printf("error\n");
return -1;
}
(*L)->next = NULL;
return 0;
}
//链表赋值
void CreatList(LIST *L,int i,int a[])
{
LIST p = *L ;
for(int j=i-1 ; j>=0 ; j--){
p = (LIST)malloc(sizeof(LIST));
p -> data = a[j];
p -> next = (*L) -> next ;
(*L) -> next = p;
}
}
//指定位置插入链表元素
void InsertList1(LIST *L,int rc,int item)
{
LIST p,r;
int i;
p = (LIST)malloc(sizeof(LIST));
p ->data = item;
for(i=0,r=*L; r&&i<rc ; i++)
r = r->next;
p -> next = r ->next;
r -> next = p;
}
//向有序列表的指定位置插入元素
void InsertList2(LIST *L,int item)
{
LIST p,r,q;
int i = 0;
p = (LIST)malloc(sizeof(LIST));
p ->data = item;
for(r=*L; r&&r->next->data<item ;r = r->next)
p -> next = r ->next;
r -> next = p;
}
//删除指定元素值
void DeleteList(LIST *L,int item)
{
LIST p,q;
p = (*L)->next;
while(p->data != item){
q = p;
p = p->next;
}
q->next = p->next;
}
//输出链表元素
void OutputList(LIST L)
{
LIST p = L->next;
while(p){
printf("%d ",p->data);
p = p -> next;
}
}
int main()
{
int i,rc,item,n;
int a[105];
LIST L;
printf("----------初始化链表------\n");
InitList(&L);
printf("请输入要插入的有序数组元素的个数n(100以内):");
scanf("%d",&n);
printf("请输入要插入的有序数组:");
for(i=0 ; i<n ; i++)
scanf("%d",&a[i]);
CreatList(&L,n,a);
OutputList(L);
printf("数组插入链表成功......\n");
printf("-----指定位置插入链表元素------\n");
printf("请输入要插入的位置rc和元素值item:");
scanf("%d%d",&rc,&item);
printf("\n");
InsertList1(&L,rc-1,item);
OutputList(L);
printf("请输入要插入元素值item:");
scanf("%d",&item);
InsertList2(&L,item);
OutputList(L);
printf("请输入要删除的元素值:");
scanf("%d",&item);
printf("\n");
DeleteList(&L,item);
OutputList(L);
}
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
} *LIST;
//初始化链表
int InitList(LIST *L)
{
*L = (LIST)malloc(sizeof(LIST));
if(!(*L)){
printf("error\n");
return -1;
}
(*L)->next = NULL;
return 0;
}
//链表赋值
void CreatList(LIST *L,int i,int a[])
{
LIST p = *L ;
for(int j=i-1 ; j>=0 ; j--){
p = (LIST)malloc(sizeof(LIST));
p -> data = a[j];
p -> next = (*L) -> next ;
(*L) -> next = p;
}
}
//指定位置插入链表元素
void InsertList1(LIST *L,int rc,int item)
{
LIST p,r;
int i;
p = (LIST)malloc(sizeof(LIST));
p ->data = item;
for(i=0,r=*L; r&&i<rc ; i++)
r = r->next;
p -> next = r ->next;
r -> next = p;
}
//向有序列表的指定位置插入元素
void InsertList2(LIST *L,int item)
{
LIST p,r,q;
int i = 0;
p = (LIST)malloc(sizeof(LIST));
p ->data = item;
for(r=*L; r&&r->next->data<item ;r = r->next)
p -> next = r ->next;
r -> next = p;
}
//删除指定元素值
void DeleteList(LIST *L,int item)
{
LIST p,q;
p = (*L)->next;
while(p->data != item){
q = p;
p = p->next;
}
q->next = p->next;
}
//输出链表元素
void OutputList(LIST L)
{
LIST p = L->next;
while(p){
printf("%d ",p->data);
p = p -> next;
}
}
int main()
{
int i,rc,item,n;
int a[105];
LIST L;
printf("----------初始化链表------\n");
InitList(&L);
printf("请输入要插入的有序数组元素的个数n(100以内):");
scanf("%d",&n);
printf("请输入要插入的有序数组:");
for(i=0 ; i<n ; i++)
scanf("%d",&a[i]);
CreatList(&L,n,a);
OutputList(L);
printf("数组插入链表成功......\n");
printf("-----指定位置插入链表元素------\n");
printf("请输入要插入的位置rc和元素值item:");
scanf("%d%d",&rc,&item);
printf("\n");
InsertList1(&L,rc-1,item);
OutputList(L);
printf("请输入要插入元素值item:");
scanf("%d",&item);
InsertList2(&L,item);
OutputList(L);
printf("请输入要删除的元素值:");
scanf("%d",&item);
printf("\n");
DeleteList(&L,item);
OutputList(L);
}