gpt4 book ai didi

c++ - 链表实现

转载 作者:行者123 更新时间:2023-11-28 03:45:53 25 4
gpt4 key购买 nike

这是一个简单的链表代码:

#include <iostream>
using namespace std;
class link
{
public:
int data;
double ddata;
link *next;
link(int id,double dd){
data=id;
ddata=dd;
}

void diplay(){
cout<<data<<" ";
cout<<data<<" ";

}
};

class linkedlist{
private :
link *first;
public:
linkedlist(){
first=NULL;
}

bool empthy(){
return (first==NULL);
}

void insertfirst(int id,double dd){
link *newlink=new link(id,dd);
newlink->next=first;
first=newlink;
}

link* deletefirst(){
link *temp=first;
first=first->next;
return temp;
}

void display(){
cout<<" (list ( first -> last ) ) ";
link *current=first;
while(current!=NULL){
current->diplay();
current=current->next;
}
cout<<endl;
}
};

int main(){
linkedlist *ll=new linkedlist();
ll->insertfirst(22,2.99);
ll->insertfirst(34,3.99);
ll->insertfirst(50,2.34);
ll->insertfirst(88,1.23);
ll->display();
return 0;
}

但它给了我意想不到的结果。它打印 88 88 50 50 34 34 22 22 而不是 (88,1.23) (50,2.34) (34,3.99) (22,2.99)

最佳答案

void diplay(){

cout<<data<<" ";
cout<<ddata<<" ";

}

用这段代码替换 display 函数。 (而不是打印 dataddata 你打印了两次数据)

关于c++ - 链表实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7702582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com