关于尾插法建立单链表的问题!!

uloborid 2009-04-05 12:57:34
以下代码实现的功能是用尾插法建立一个单链表,可以实现插入、删除功能,在TC2编译器中通过,但是运行有问题,单链表建立好后无法打印链表,请问这是什么原因?应该怎么改?

#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 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;
}

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);
}

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

int main()
{
int a,b,c;
printf("Please create list:\n");
scanf("%d",&c);
while(c!=0)
{
CreateListR(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);
print(head);
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;
}
...全文
1216 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
baijipeng 2009-08-16
  • 打赏
  • 举报
回复
#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;

LinkList CreateListR(int ch)
{ ListNode *head=(ListNode*)malloc(sizeof(ListNode));
ListNode *s,*r;
r=head;

while(ch!=0&&ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
s->next=NULL;
r->next=s; //s为节点,r为指针
r=s;
scanf("%d",&ch);
}


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;
return 0;

}

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);
return 0;
}

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

int main()
{
int a,b,c;
LinkList q;
printf("Please create list:\n");
scanf("%d",&c);
q=CreateListR(c);

printf("The list is:\n");
print(q);
printf("Please input the number you want to insert:\n");
scanf("%d",&a);
printf("Please input insert position:\n");
scanf("%d",&b);
InsertList(q,a,b);
print(q);
printf("Please input the position you want to delete:\n");
scanf("%d",&b);
DeleteList(q,b);
printf("The list is:\n");
print(q);
return 0;
}
呵呵 这个可以
lvjaio5241 2009-04-16
  • 打赏
  • 举报
回复

#include "Stdio.h"
#include "Conio.h"
#include "malloc.h"
typedef int ElemType;
typedef struct node
{ElemType data;
struct node *next;
}Slink;
Slink *L;
Slink * Init_slink()//初始化分配头结点
{L=(Slink *)malloc(sizeof(Slink));
L->next=NULL;
return(L);
}

void Create_toucha_slink(Slink *L)// 头插法创建单链表,注意逆位序输入元素
{ElemType x;
Slink *p;
printf("___________________________________\n");
printf("inputdata end with -999\n");
scanf("%d",&x);
while(x!=-999)
{ p=(Slink *)malloc(sizeof(Slink));
p->data=x;
p->next=L->next;
L->next=p;
scanf("%d",&x);
}

}

void Create_weicha_slink(Slink *L)//尾插法创建单链表
{ElemType x;
Slink *p,*r;
r=L;
printf("___________________________________\n");
printf("inputdata end with -999\n");
scanf("%d",&x);
while(x!=-999)
{p=(Slink *)malloc(sizeof(Slink));
p->data=x;
p->next=NULL;
r->next=p;
r=p;
scanf("%d",&x);
}


}

int Ji_ou_slink(Slink *L)//找出最小结点,如偶则删除后继,如奇则与后继交换
{Slink *p,*q,*r=NULL;
p=L->next ;
q=p;
while(p!=NULL)
{if(p->data<q->data)
q=p;
p=p->next;
}
//printf("%d",q->data );
if(q->next!=NULL) // 判断是否有后继
{if(q->data%2==0)
{p=q->next ;
q->next =p->next ;
free(p);
}
else //奇数则与后继交换位置
{
p=L ;
while(p->next !=q) p=p->next ;
r=q->next;
p->next =r;
q->next =r->next ;
r->next =q;
}
}
else
{printf("最后一个元素,无后继");
}
if(q!=NULL) return 1;
else return 0;
}



void Pf_slink(Slink *L)
{Slink *p;
p=L->next;
while(p!=NULL)
{ printf("%4d",p->data);p=p->next;}

}
int main(void)
{ Slink *q=(Slink *)malloc(sizeof(Slink));
int i;
L=Init_slink();
Create_weicha_slink(L);

i=Ji_ou_slink(L);

Pf_slink(L);
getch();
return 0;
}
ericming200409 2009-04-16
  • 打赏
  • 举报
回复
xiehui3651 2009-04-16
  • 打赏
  • 举报
回复
看你的代码实在费劲,最后删除链表的时候从头开始删效率会高很多吧,每次getnode都会遍历一次
xiehui3651 2009-04-16
  • 打赏
  • 举报
回复
找到问题了,你在LinkList CreateListR(void)函数里定义了个局部变量的head,所以最后
全局变量的head仍然是空的
uloborid 2009-04-16
  • 打赏
  • 举报
回复
不能沉啊!自己顶上去!
uloborid 2009-04-13
  • 打赏
  • 举报
回复
????这段代码我在做头插法的时候是通过的,后来改做尾插法就改了下主函数和建表函数,以下三个函数实现的是寻找节点,插入,和删除功能,我没改过,应该是没问题的。
ListNode *GetNode(LinkList head,int i)
int InsertList(LinkList head,DataType x,int i)
int DeleteList(LinkList head,int i)
主要问题应该就在

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

这段代码里,楼上ToBeTough兄给的意见我也都试过了,问题依旧
虽然定义为char类型,但是依旧以ASCII码保存,输出的时候用%d应该是可以输出数字的,int InsertList(LinkList head,DataType x,int i)实现的是插入功能,i是用当然是要插入的位置啊,这个和建表没什么冲突啊?建表是通过LinkList CreateListR(void)实现的,里面并没有定义参数i,返回的是head。

依旧等待救援…………
bambookudo 2009-04-12
  • 打赏
  • 举报
回复


printf("%5d",s->data);//这个链表里的数据元素不是char型的么。。。

另插入函数里那个int i表示插入的位置。。。既然是尾插,直接插入到尾部就好了。。。为什么还要查找插入点位置呢?
uloborid 2009-04-11
  • 打赏
  • 举报
回复

#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 CreateListR(void)
{
LinkList head=(LinkList)malloc(sizeof(ListNode));
ListNode *s,*r;
int ch;
r=head;
printf("Please create list:\n");
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
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);
}

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

int main()
{
int a,b,c;
CreateListR();
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);
print(head);
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;
}
]
LS,我已经按你说的改了,但还是不行啊,抓狂ING.....
继续等待救援 - -!
ToBeTough 2009-04-06
  • 打赏
  • 举报
回复
如果把r=head;放在CreateListR中,你就应该一次性完成链表的创建;
void CreateListR()
{
int ch;
r=head;//r=head
scanf("%d",&ch);
while(ch!=0)
{
   q=(ListNode *)malloc(sizeof(ListNode));
   q->data=ch;
    q->next=NULL;
   r->next=q;
   r=q;
   scanf("%d",&ch);
}
}
你的main函数里用while循环;表示你每次只创建了一个接点;如果在函数内有r=head的话,就标识不了最后一个接点了;

int print(LinkList l)
{
ListNode *s;
s=l;
while(s->next!=NULL)//不要用s!=NULL
{
s=s->next;
printf("%5d",s->data);

}
printf("\n");
return 1;
}
如果你用s!=NULL;到了最后一个元素的时候,s=s->next;这句就越界拉;
uloborid 2009-04-05
  • 打赏
  • 举报
回复
ls,你说的我不是很明白,下面是我修改后的尾插法插入函数,给head空间了,但是如果r=head;不放在函数里我想不出还能放哪里?

LinkList CreateListR(int ch)
{
LinkList head=(LinkList)malloc(sizeof(ListNode));
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;
}

另,那个打印函数:

int print(LinkList l)
{
ListNode *s;
s=l;
while(s->next!=NULL)//不要用s!=NULL
{
s=s->next;
printf("%5d",s->data);

}
printf("\n");
return 1;
}

因为我已经定义了s=s->next; 那如果停止条件变为s->next!=NULL岂不是变为s->next->next!=NULL了?
出现的问题依旧是无法打印链表里的数,一个数字也打印不了
麻烦各位再给看看??
ToBeTough 2009-04-05
  • 打赏
  • 举报
回复
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;
}
首先你应该为你的head分配空间,否则r->next=s;
这句就没意义;另外你把 r=head;放在这个函数中,每次执行的时候都赋一次,那不是把前面输入的都覆盖拉,打印的时候就只有一个拉;
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define ERROR 0
typedef char DataType;
typedef struct node{
DataType data;
struct node *next;
}ListNode,*LinkList;

ListNode * head=(ListNode *)malloc(sizeof(ListNode));
ListNode *p,*q;

void CreateListR(char ch)
{
q=(ListNode *)malloc(sizeof(ListNode));
q->data=ch;
q->next=NULL;
p->next=q;
p=q;
}
int print(LinkList l)
{
ListNode *s;
s=l;
while(s->next!=NULL)//不要用s!=NULL
{
s=s->next;
printf("%5d",s->data);

}
printf("\n");
return 1;
}
int main()
{
p=head;
char c;
printf("Please create list:\n");
scanf("%d",&c);
while(c!='0')
{
CreateListR(c);
scanf("%c",&c);
}
print(head);
return 0;
}//其他的函数没看

33,008

社区成员

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

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