我已经为循环链表实现了代码,但除以下内容外,输出窗口上未显示任何输出:
打印列表的时间
这是我的编译器给出的消息:
--------------构建:在Circular_Linked_List中进行调试(编译器:GNU GCC编译器)---------------]
目标是最新的。无需执行任何操作(所有项目都是最新的)。
--------------运行:在Circular_Linked_List中调试(编译器:GNU GCC编译器)---------------
检查是否存在:C:\ Users \ hp \ Desktop \ CPP编程\ Circular_Linked_List \ bin \ Debug \ Circular_Linked_List.exe执行:“ C:\ Program Files(x86)\ CodeBlocks / cb_console_runner.exe”“ C:\ Users \ hp \ Desktop \ CPP编程\ Circular_Linked_List \ bin \ Debug \ Circular_Linked_List.exe”(在C:\ Users \ hp \桌面\ CPP编程\ Circular_Linked_List。)
这是下面的代码,我正在尝试运行并基于代码:: blocks17.12
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
void push(Node* head,int data)
{
Node *new_node = new Node();
Node *temp = head;
new_node->data = data;
new_node->next = head;
if(head!=NULL)
{
while(temp->next!=head)
{
temp = temp->next;
}
temp->next = new_node;
}
else{
new_node->next = new_node;
}
head = new_node;
}
void printList(Node* head)
{
Node *temp = head;
if(head!=NULL){
while(temp->next!=head)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
else{
return;
}
}
int main()
{
Node *head = NULL;
push(head,12);
push(head,14);
push(head,15);
push(head,16);
cout<<"Time to print the List\n";
printList(head);
return 0;
}
您需要如下更改推送功能和打印列表功能:
void push(Node* &head,int data){
Node *new_node = new Node();
Node *temp = head;
new_node->data = data;
new_node->next = head;
if(head!=NULL)
{
while(temp->next!=head)
{
temp = temp->next;
}
temp->next = new_node;
}
else
new_node->next = new_node;
head = new_node;}
void printList(Node* head){
Node *temp = head;
if(head!=NULL){
while(temp->next!=head)
{
cout<<temp->data<<" "<<std::endl;
temp=temp->next;
}
if(head!=NULL)
cout<<temp->data<<" "<<std::endl;
}
else
return;}
您可以在此处找到有关通过引用传递指针的有用信息:https://www.geeksforgeeks.org/passing-reference-to-a-pointer-in-c/