关于双链表插入结点

payen_1985 2008-01-21 06:47:46
请大家看这个程序:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define N sizeof(struct node)

struct node
{
int num;//数据域
int feq;//频度域
struct node *prior;
struct node *next;
};

struct node *creat(int n)//创建双向循环链表,n为链表长度,head为头结点,feq为n值出现的次数。
{
struct node *head,*p,*last;
head=(struct node *)malloc(N);
head->num=0;
head->feq=0;
last=head;
for(int i=0;i<n;i++)
{
p=(struct node *)malloc(N);
scanf("%d",&p->num);
p->feq=0;
last->next=p;
p->prior=last;
last=p;
}
last->next=head;
head->prior=last;
return head;
}

void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next,*p=q,*pre=q;
while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}
else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}

while(p!=head&&p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;
}
if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}

void main()
{
struct node *head,*p;
int x,n;
scanf("%d",&n);
head=creat(n);
while(1)
{
printf("input x:");
if(scanf("%d",&x)==0) break;
locate(head,x);
p=head->next;
for(int i=0;i<n;i++)
{
printf("%d %d\n",p->feq,p->num);
p=p->next;
}
}
}

上面程序错误仅在结点插入位置在首元结点的情况,如:
当输入为:
5
1 2 3 4 5
x=1
输出为:
0 2
0 3
0 4
0 5
0 0
当输入为:
5
1 2 3 4 5
x=2
输出为:
1 2
0 1
0 3
0 4
0 5
请大家看看问题在哪!


...全文
151 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
payen_1985 2008-01-21
  • 打赏
  • 举报
回复
嗯 对
结贴!
wenjun1130 2008-01-21
  • 打赏
  • 举报
回复
在函数头 给p,pre赋值为head->next;这时为原链表的第一个节点。
如果你后面没改变第一个节点就没错,但当你取出的是第一个节点时,
第2个节点就变为第一个节点,这时p,pre应该跟着改变才能指向改变
后的第一个节点
payen_1985 2008-01-21
  • 打赏
  • 举报
回复
to 1楼
程序运行结果对了,不过我没想明白,为什么在初始化时对p,pre赋值不对,
而在结点取出之后赋值就对了?
ps:好像第一个删除首元结点是多余了...
forandom 2008-01-21
  • 打赏
  • 举报
回复
void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next,*p=q,*pre=q; //此处p,pre有问题,当删除首节点后,p,pre仍指向被删除的首节点
while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}
else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}

//虽然删除了原首节点,但在此时p仍指向原首节点,而不是新的首节点,仍会进入循环
while(p!=head&&p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;
}
if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}
-----------------------------------------------------------------------------------------
这样改过后就可以了:
void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next;

while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}
else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}

struct node *p=head->next,*pre=head->next;
while(p!=head && p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;

}

if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}


C/C++ code
forandom 2008-01-21
  • 打赏
  • 举报
回复

void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next,*p=q,*pre=q; //此处p,pre有问题,当删除首节点后,p,pre仍指向被删除的首节点
while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}
else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}

//虽然删除了原首节点,但在此时p仍指向原首节点,而不是新的首节点,仍会进入循环
while(p!=head&&p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;
}
if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}


这样改过后就可以了:

void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next;

while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}
else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}

struct node *p=head->next,*pre=head->next;
while(p!=head && p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;

}

if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}
xub666 2008-01-21
  • 打赏
  • 举报
回复
改locate函数
void locate(struct node *head,int x)//查找数据域num为x的结点,然后将该结点的频度域feq+1,再根据频度域按非递增对链表进行排序
{
struct node *q=head->next,*p=q,*pre=q;
while(q!=head)//查找x的在链表中的位置
{
if(q->num==x)
{
q->feq++;
break;
}
q=q->next;
}
if(q==head) //当链表中无x时
{
printf("can't find x,please input another integer!\n");
return;
}
else if(q==head->next) //当x在首元结点时,将该结点取出
{
head->next=q->next;
head->next->prior=q->prior;
}//多余了吧.

else //其他情况当链表中含有x时,将该结点取出
{
q->prior->next=q->next;
q->next->prior=q->prior;
}
//这里需要重新对指针进行赋值.
p = head->next;
pre = p;
//

while(p!=head&&p->feq>=q->feq)//寻找插入的位置
{
pre=p;
p=p->next;
}
if(p==pre)//当插入位置在首元结点时
{
q->next=p;
p->prior=q;
head->next=q;
q->prior=head;
}
else //当在其他位置插入时
{
pre->next=q;
q->prior=pre;
q->next=p;
p->prior=q;
}
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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