单链表的问题

uloborid 2009-03-29 02:14:17
下面代码是用头插法创建一个单链表,实现插入、删除功能,编译是没问题的,但是程序运行的时候,总是不能打印链表,个人觉得,打印函数print()应该是没问题的,但是在LinkList CreateListF(int ch)函数中head=NULL而且似乎后面也没给head赋值,导致在打印函数中一直是符合s->next==NULL的,所以不能打印。不知道分析的对不对?应该怎么改呢??

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERROR 0
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}ListNode;
typedef ListNode *LinkList;
ListNode *p;
LinkList head;

LinkList CreateListF(int ch)
{
ListNode *s;
head=NULL;
while(ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
s->next=head;
head=s;
ch=getchar();
}
return head;
}

ListNode *GetNode(LinkList head,int i)
{
int j;
ListNode *p;
p=head;
j=0;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return p;
else
return NULL;
}

int InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
p=GetNode(head,i-1);
if(p==NULL)
return ERROR;
s=(ListNode *)malloc(sizeof(ListNode));
s->data=x;
s->next=p->next;
p->next=s;
}

int DeleteList(LinkList head,int i)
{
ListNode *p,*r;
p=GetNode(head,i-1);
if(p==NULL||p->next==NULL)
return ERROR;
r=p->next;
p->next=r->next;
free(r);
}

void print(LinkList *l)
{
ListNode *s;
s=l;
while(s->next!=NULL)
{
printf("%5d",s->data);
s=s->next;
}
}

main()
{
int a,b,c;
clrscr();
printf("Please crease list:\n");
do
{
scanf("%d",&c);
CreateListF(c);
}while(c!=0);
printf("The list is:\n");
print(head);
printf("Please input the number you want to insert:\n");
scanf("%d",&a);
printf("Please input insert position:\n");
scanf("%d",&b);
InsertList(head,a,b);
printf("Please input the position you want to delete:\n");
scanf("%d",&b);
DeleteList(head,b);
printf("The list is:\n");
print(head);
return 0;
}
...全文
107 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
uloborid 2009-03-29
  • 打赏
  • 举报
回复
尾插法实现函数如下:

LinkList CreateListR(int ch)
{
ListNode *s,*r;
r=head;
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}

各位大佬,再帮我看下吧,尾插法也是打印不出来,晕了!
uloborid 2009-03-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 currenttt 的回复:]
两个问题
1. CreateListF()里不能对head赋值,就像你说的那样那个
2. void print(LinkList *l) -> void print(LinkList l),不然我搞不懂你是怎么编译通过的
3. void print()里,while(s->next!=NULL) -> while(s != NULL)
[/Quote]
…………尴尬,事实是,我确实编译通过了,jn989 给的代码在TC2上已经通过了,运行结果也是正确的,至于加clrscr()这个函数是我个人的小习惯,调试的时候方便,以免屏幕上显示的东西太杂。
感谢各位!十分万分特别以及非常之感谢!这几天都想爆脑袋了,实在是书不咋滴,拿的这是自考版本的数据结构书,说的点都不清楚,代码还好多不对,头大啊!总之,谢谢啦!
jn989 2009-03-29
  • 打赏
  • 举报
回复
楼上已经说了些问题,稍改了一下可以跑了,lz参考

// 322.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERROR 0
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}ListNode;

typedef ListNode *LinkList;
ListNode *p;
LinkList head=NULL;

LinkList CreateListF(int ch)
{
ListNode *s;
// head=NULL;
while(ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
s->next=head;
head=s;
ch=getchar();
}
return head;
}

ListNode *GetNode(LinkList head,int i)
{
int j;
ListNode *p;
p=head;
j=0;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return p;
else
return NULL;
}

int InsertList(LinkList head,DataType x,int i)
{
ListNode *p,*s;
p=GetNode(head,i-1);
if(p==NULL)
return ERROR;
s=(ListNode *)malloc(sizeof(ListNode));
s->data=x;
s->next=p->next;
p->next=s;
}

int DeleteList(LinkList head,int i)
{
ListNode *p,*r;
p=GetNode(head,i-1);
if(p==NULL||p->next==NULL)
return ERROR;
r=p->next;
p->next=r->next;
free(r);
}

void print(LinkList l) //LinkList已经是ListNode*指针类型,不要再加*
{
ListNode *s;
s=l;
while(s!=NULL) //循环里面已经取了next
{
printf("%5d",s->data);
s=s->next;
}
}

int main() //怎么少了返回int
{
int a,b,c;
// clrscr(); 不知道这个函数是什么用
printf("Please crease list:\n");
/* do 这里的“0”应该是控制结束的标志吧,按照你这样写,最后0是作为一个元素在链表中的,
{ 要是不想要的话,看我下面的写法
scanf("%d",&c);
CreateListF(c);
}while(c!=0);
*/
scanf("%d",&c);
while(c!=0)
{
CreateListF(c);
scanf("%d",&c);
}
printf("The list is:\n");
print(head);
printf("Please input the number you want to insert:\n");
scanf("%d",&a);
printf("Please input insert position:\n");
scanf("%d",&b);
InsertList(head,a,b);
printf("Please input the position you want to delete:\n");
scanf("%d",&b);
DeleteList(head,b);
printf("The list is:\n");
print(head);
return 0;

}

currenttt 2009-03-29
  • 打赏
  • 举报
回复
是三个问题……囧
currenttt 2009-03-29
  • 打赏
  • 举报
回复
两个问题
1. CreateListF()里不能对head赋值,就像你说的那样那个
2. void print(LinkList *l) -> void print(LinkList l),不然我搞不懂你是怎么编译通过的
3. void print()里,while(s->next!=NULL) -> while(s != NULL)

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧