c++单链表的遍历输出问题!求助!!!!

c++单链表的遍历输出问题!求助!!!!,第1张

错误比较隐晦,每次执行插入函数 q总是头结点的值~

一种改正参考:

template <class elemType>

class linklist

{

protected:

node<elemType> head,tem;

public:

linklist(){ head= new node<elemType>; tem = head;}

void insert(const elemType &data);

void print();

void empty();

~linklist(){empty();delete head;}

};

template<class elemType>

void linklist<elemType>::insert(const elemType &data)

{

elemType tmp=data;

node<elemType> p;

p=new node<elemType>(tmp);

tem->next=p;

tem=p;

tem->next=NULL;

}

#include"stdlibh"

#include"stdioh"

#include"timeh"

struct node

{

int data;

struct node link;

};

void main()

{

int i,max,t;

struct node head,u,v,p,h;

randomize();

for(i=1;i<10;i++)

{

u=(struct node )malloc(sizeof(struct node));

u->link=NULL;

t=rand();

u->data=t;

if(i==1) {head=v=u;}

else {v->link=u;v=u;}

}

max=head->data;

u=head;

for(p=head;p;p=p->link)

if(max<p->data) {max=p->data;u=p;} //最大结点在u中

for(v=head;v;v=v->link)

printf(" %d ",v->data);

printf("\nmax=%d\n",max);

return ;

}

struct node {

int data;

struct node next;

} ;

创建单链表后,最后一个结点的next是NULL,据此来遍历表,获得长度。

void get_len( struct node head )

{

struct node p=head->next ;

int len=0;

while ( p )

{

len++;

p=p->next;

}

head->data=len ; //存储长度到头结点

}

以上就是关于c++单链表的遍历输出问题!求助!!!!全部的内容,包括:c++单链表的遍历输出问题!求助!!!!、设计一个算法,通过一趟遍历在单链表中确定值最大的结点。要完整的程序,用C语言。急用。能运行出结果即可、c++单链表的遍历与长度计算,求这题的代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10099587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存