首页 新闻 赞助 找找看

数据结构单链表问题

0
悬赏园豆:20 [待解决问题]

下面这个程序在VS2010里面运行总是出错啊,不能把插入后的单链表打印出来!超郁闷,求大牛花点时间帮我看下,先谢过了啊!!!
#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef struct LNode{
int e;
struct LNode *next;
}LNode,*LinkList;

void CreateList_L(LinkList,int);
int ListInsert_L(LinkList,int,char[]);
void PrintList_L(LinkList);

void CreateList_L(LinkList L,int n) //逆序建表
{
L = (LinkList) malloc (sizeof(LNode)); //头结点
L->next = NULL;
for(int i = n; i > 0; i--)
{
int x;
LinkList p = (LinkList) malloc (sizeof(LNode));
scanf("%d",&x);
p->e = x;
p->next = L->next;
L->next = p;
}
}

int ListInsert_L(LinkList L,int i,int a)
{
LinkList p = L;
int j = 0;
while(p->next && j < i - 1)
{
p = p->next;
j++;
}
if(!(p->next) || j > i-1) return ERROR;
LinkList s = (LinkList) malloc (sizeof(LNode));
if(!s) return OVERFLOW;
s->e = a;
s->next = p->next;
p->next = s;
return OK;
}

void PrintList_L(LinkList L)
{
LinkList p = L->next;
while(!(p->next))
{
printf("%d\n",p->e);
p = p->next;
}
}

void main()
{
LNode L;
int n;
n = 6;
CreateList_L(&L,n);
//PrintList_L(L);
ListInsert_L(&L,3,100);
PrintList_L(&L);
}

Sole_cc的主页 Sole_cc | 初学一级 | 园豆:71
提问于:2012-06-23 12:38
< >
分享
所有回答(1)
0

我给你写的不太一样,你先看看吧!不懂的咱们在讨论!

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define maxsize 1024
 5 #define OK 1
 6 #define ERROR 0
 7 
 8 typedef struct LNode
 9 {
10     int data;
11     struct LNode *next;
12 }LNode;
13 
14 //声明函数
15 void out_l(LNode *l);    //输出线性表
16 LNode *create_l();        //创建线性表
17 void insert_l(LNode *l);    //插入元素
18 
19 void main()
20 {
21     int i;
22     LNode *l;
23     l = create_l();        //创建链式链表
24     insert_l(l);        //插入元素
25     out_l(l);            //输出链表
26 }
27 
28 //插入元素
29 void insert_l(LNode *l)
30 {
31     int i,j,x;
32     LNode *p,*h;
33     j = 0;
34     h = l;
35     printf("请输入插入的位置:\n");
36     scanf("%d",&i);
37     printf("请输入插入的元素:\n");
38     scanf("%d",&x);
39     while(h != NULL && j < i - 1)
40     {
41         h = h->next;
42         j++;
43     }
44     if(h == NULL || j > i -1)
45     {
46         printf("输入的位置有误!");
47     }
48     else
49     {
50         p = (LNode *)malloc(sizeof(LNode));
51         p->data = x;
52         p->next = h->next;
53         h->next = p;
54     }
55     
56 }
57 
58 //创建链式线性表
59 LNode *create_l()        //返回的是指针类型的所以要用到*
60 {
61     LNode *h,*p,*l,*w;
62     int x;
63     h = (LNode *)malloc(sizeof(LNode));
64     h->next = NULL;
65     p = h;
66     printf("请输入创建的值:");
67     scanf("%d",&x);
68     while(x != -222)
69     {
70         l = (LNode *)malloc(sizeof(LNode));
71         l->data = x;
72         l->next = NULL;
73         p->next = l;
74         p = l;
75         printf("输入-222结束,其他则继续创建线性表;");
76         scanf("%d",&x);
77     }
78     return(h);
79 }
80 
81 //输出链式线性表的值
82 void out_l(LNode *l)
83 {
84     LNode *p;
85     p = l;
86     p = p->next;        //由于第一个节点是头结点不存数据时空的,所以要指向下一个节点才会有数据
87     while(p != NULL)
88     {
89         printf("%5d",p->data);
90         p = p->next;
91     }
92 }

没法发图片,你自己运行吧!

net小伙 | 园豆:232 (菜鸟二级) | 2012-11-22 15:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册