主题:[讨论]数据结构单链表问题
为什么我这个单链表的插入和删除函数都不起作用啊
是输出函数的问题还是确实没有插入
求各位帮帮忙啊
这个问题纠结我好几天啦
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define INFEASLIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data; //数据域
struct LNode * next; //指针域
}LNode, * Linklist;
void CreatList_L( Linklist &L, int n)//初始化链表
{
Linklist p;
int i;
L = ( Linklist ) malloc ( sizeof ( LNode ) );
L -> next = NULL;
printf("请输入元素:\n");
for( i = n; i > 0; -- i )
{
p = ( Linklist ) malloc ( sizeof ( LNode ) );
scanf( "%d", & p -> data);
p -> next = L -> next;
L -> next = p;
}
}
void print( Linklist L)//输出整个链表
{
Linklist p;
p = L -> next;//先将第一个指针指向p,如果没有这一句,将输出头指针指向的内容
printf("输出的元素是:\n");
while( p != NULL)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
}
Status ListInsert_L( Linklist &L, int i, ElemType &e)//向链表中插入元素
{
LNode *p, *s;
int j;
p = L ;
j = 0;
while( p && j < i - 1 )
{
p = p -> next;
++ j;
}
if( !p || j > i - 1)
return ERROR;
s = ( Linklist ) malloc ( sizeof ( LNode ) );
s -> data = e;
s -> next = p -> next;
p -> next = s;
return OK;
}
Status ListDelete_L( Linklist &L, int i, ElemType &e)//删除链表的元素
{
Linklist p, q;
int j;
p = L;
j = 0;
while( p -> next && j < i - 1)
{
p = p -> next;
++j;
}
if( ! ( p -> next ) || j < i - 1)
return ERROR;
q = p -> next;
p -> next = q -> next;
e = q -> data;
free ( q );
return OK;
}
int ListLength( Linklist &L)//算整个链表的长度
{
int n=0;
Linklist p;
p = L -> next;
while( p != NULL)
{
p = p -> next;
n++;
}
return n;
}
int main( )
{
Linklist L;
int i, e, n;
printf("请输入元素个数:\n");
scanf("%d", &n);
CreatList_L( L, n );
print( L );
n = ListLength( L );
printf("请输入要插入的位置:1--%d\n", n+1);
scanf("%d", &n);
printf("请输入要插入的元素:\n");
scanf("%d",&e);
ListInsert_L( L, i, e);
printf("%d",ListLength( L ));
print( L );
printf("请输入要删除的元素位置:1--%d\n",ListLength( L ));
scanf("%d",&n);
ListDelete_L( L , i, e);
print( L );
}
是输出函数的问题还是确实没有插入
求各位帮帮忙啊
这个问题纠结我好几天啦
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define TRUE 1
#define FALES 0
#define OK 1
#define ERROR 0
#define INFEASLIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LNode
{
ElemType data; //数据域
struct LNode * next; //指针域
}LNode, * Linklist;
void CreatList_L( Linklist &L, int n)//初始化链表
{
Linklist p;
int i;
L = ( Linklist ) malloc ( sizeof ( LNode ) );
L -> next = NULL;
printf("请输入元素:\n");
for( i = n; i > 0; -- i )
{
p = ( Linklist ) malloc ( sizeof ( LNode ) );
scanf( "%d", & p -> data);
p -> next = L -> next;
L -> next = p;
}
}
void print( Linklist L)//输出整个链表
{
Linklist p;
p = L -> next;//先将第一个指针指向p,如果没有这一句,将输出头指针指向的内容
printf("输出的元素是:\n");
while( p != NULL)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
}
Status ListInsert_L( Linklist &L, int i, ElemType &e)//向链表中插入元素
{
LNode *p, *s;
int j;
p = L ;
j = 0;
while( p && j < i - 1 )
{
p = p -> next;
++ j;
}
if( !p || j > i - 1)
return ERROR;
s = ( Linklist ) malloc ( sizeof ( LNode ) );
s -> data = e;
s -> next = p -> next;
p -> next = s;
return OK;
}
Status ListDelete_L( Linklist &L, int i, ElemType &e)//删除链表的元素
{
Linklist p, q;
int j;
p = L;
j = 0;
while( p -> next && j < i - 1)
{
p = p -> next;
++j;
}
if( ! ( p -> next ) || j < i - 1)
return ERROR;
q = p -> next;
p -> next = q -> next;
e = q -> data;
free ( q );
return OK;
}
int ListLength( Linklist &L)//算整个链表的长度
{
int n=0;
Linklist p;
p = L -> next;
while( p != NULL)
{
p = p -> next;
n++;
}
return n;
}
int main( )
{
Linklist L;
int i, e, n;
printf("请输入元素个数:\n");
scanf("%d", &n);
CreatList_L( L, n );
print( L );
n = ListLength( L );
printf("请输入要插入的位置:1--%d\n", n+1);
scanf("%d", &n);
printf("请输入要插入的元素:\n");
scanf("%d",&e);
ListInsert_L( L, i, e);
printf("%d",ListLength( L ));
print( L );
printf("请输入要删除的元素位置:1--%d\n",ListLength( L ));
scanf("%d",&n);
ListDelete_L( L , i, e);
print( L );
}