永发信息网

带头尾结点的单链表怎么实现?

答案:2  悬赏:0  手机版
解决时间 2021-12-29 09:24
"#include "stdafx.h"
struct node //链表的节点
{
unsigned value;
node *pnext;
node()
{
value = 0;
pnext = NULL;
}
};

struct node_list //链表的定义结构,有头有尾
{
node *pfirst; //指向第一个可用的节点,如果无可用节点,则值为NULL
node *plast; //指向最后一个可用的节点,如果无可用的节点,则值为NULL

node_list()
{
pfirst = NULL;
plast = NULL;
}

};
谁会做?
最佳答案
struct node_list //链表的定义结构,有头有尾
{
node *pfirst; //指向第一个可用的节点,如果无可用节点,则值为NULL
node *plast; //指向最后一个可用的节点,如果无可用的节点,则值为NULL

node_list()
{
pfirst = NULL;
plast = NULL;
};

PushBegin(unsigned insert_value)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->value = insert_value;
temp->pnext = pfirst;
pfirst = temp;
return 0;
}; //插入头
PushEnd(unsigned insert_value)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->value = insert_value;
temp->pnext = NULL;
plast = temp;
return 0;
}; //插入尾
FindNode(node *pbegin,unsigned find_value)
{
while(pbegin != NULL)
{
if(pbegin->value == find_value)
break;

pbegin = pbegin->pnext;
}
return 0;
}; //从pbegin节点开始查找第一个value等于find_value的值
EraseValue(unsigned erase_value)
{
node* pbegin = pfirst;
while(pbegin != NULL)
{
if(pbegin->pnext->value == erase_value)
{
node* temp = pbegin->pnext;
pbegin->pnext = temp->pnext;
free(temp);
}

pbegin = pbegin->pnext;
}
return 0;
}; //删除所有的值为value等于erase_value的节点
};

结束, 不过这结构定义的还真奇怪, 只好走奇怪路线
全部回答
用实例说明,下面的程序在win-tc和tc2.0下都调试通过,其中tail=tail->next;tail->next=p;(就是生成带头节点的单链表),这两句改成注释中的那两句就会生成不带头结点的单链表,你自己对着程序画一画就明白了。 什么时候要使用带头节点的单链表?为了在第一个数据元素前面加入新元素或者删除第一个节点时头指针的值不变,在第一个数据元素前面要加一个所谓的头节点。 在带头节点的单链表中,头指针(head)只有一个域,即链指针,它指向头节点,头节点有两个域,一个是数据域,值为0(null),还有一个域,链指针,这个链指针指向单链表的第一个数据元素。 而在不带头结点的单链表中,头指针也只有一个链指针,但它指向单链表的第一个数据元素。现在你一定明白了吧。 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #definenum4 structchain { charname[20]; charcity[20]; charsex[10]; charage[10]; charjob[10]; structchain*next; }; structchain*create(); structchain*sequelseach(structchain*head,charname[]); voidprint_data(structchain*point); structchaindatas[num]= { "sun","wuhan","male","24","student",null, "tom","beijing","male","31","doctor",null, "marry","shanghai","female","19","teacher",null, "willing","tianjing","female","21","worker",null }; intmain(void) { structchain*head; structchain*p; charname[30]; head=create(); printf("pleaseinputthename:\n"); scanf("%s",name); p=sequelseach(head,name); print_data(p); getch(); return0; } structchain*create() { structchain*head,*tail,*p; inti; head=tail=null; printf("putthedatasintothelist.\n"); for(i=0;i<num;i++) { p=(structchain*)malloc(sizeof(structchain)); strcpy(p->name,datas[i].name); strcpy(p->city,datas[i].city); strcpy(p->sex,datas[i].sex); strcpy(p->age,datas[i].age); strcpy(p->job,datas[i].job); p->next=null; if(head==null) head=tail=p; else tail=tail->next; tail->next=p; } returnhead; } structchain*sequelseach(structchain*head,charname[]) { structchain*temp; temp=head; for(temp=head;temp!=null;) { if(strcmp(temp->name,name)==0) break; else temp=temp->next; } if(temp==null) printf("nofound!\n"); returntemp; } voidprint_data(point) structchain*point; { if(point==null) return; printf("results:\n"); printf("name:%s\n",point->name); printf("city:%s\n",point->city); printf("sex:%s\n",point->sex); printf("age:%s\n",point->age); printf("profession:%s\n",point->job); }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
湖南师范学校的分数线是多少
做美女的假男友属于哪一类书 作者还有别的作
汽车运输企业物资管理体系要坚持什么原则
单选题已知豌豆的黄色Y对绿色y为显性,圆粒R
用什么擦墙壁纸更干净
android sdk build-tools、sdk platform、sam
怎么判断一串字符串不是全部为空格
Mum,I'mthirsty.Wouldyoupleasepass_____
派出所所内户口移居 我想把户口从本镇一个村
什么是对应账户
一个人闲的时候,为什么总是想很多自己做过的
将相和的和指的是什么
外交间两国关系用语都是些什么意思啊?
按人口数由多到少,下列排序正确的是A.亚洲、
有谁知道解剖学中腹部分区的左季肋区的肋是什
推荐资讯
共享单车什么牌子的好
填空题不等式|sinx|+|lg(1-x2)|>|sinx+lg
单选题在对待1792年英国外交使团访问一事上,
单选题Thelittlegirltoldhimthatshehadwa
为什么中国这么多创业者这么喜欢做「母婴类社
单选题下列工业中心,位于我国棉花产区、棉花
自来水管锈住了怎么拧下来?
执念这个字翻译成俄语
请问电大和夜大能同时上吗?就是在一段时间内
中国烟草海安县烟草局专卖局曲塘基层服务站地
加福社区居委会办公地址在什么地方?我要处理
---John, why are you so upset?-I­­
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?