C++ 使用代码块在链表开头插入节点
在链接列表的开头插入节点时,节点将插入开头并显示。如果我单独调用display,则它不起作用,并且对于在特定loc插入节点和在结束时,调用display函数效果良好C++ 使用代码块在链表开头插入节点,c++,C++,在链接列表的开头插入节点时,节点将插入开头并显示。如果我单独调用display,则它不起作用,并且对于在特定loc插入节点和在结束时,调用display函数效果良好 #include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; typedef struct node { int data; struct node* next; } node;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node {
int data;
struct node* next;
} node;
node* create(int n)
{
node* temp = NULL;
node* head = NULL;
node* p;
int i;
for (i = 0; i < n; i++) {
temp = (node*)malloc(sizeof(node));
cout << "enter the data for node number " << i << endl;
cin >> temp->data;
temp->next = NULL;
if (head == NULL) {
head = temp;
}
else {
p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = temp;
}
}
return head;
}
node* insertatbeg(node* head)
{
node* temp = NULL;
temp = (node*)malloc(sizeof(node));
cout << "\nenter the data for first node" << endl;
cin >> temp->data;
temp->next = head;
head = temp;
return head;
}
void display(node* head)
{
node* t = NULL;
t = head;
while (t != NULL) {
cout << t->data << "->";
t = t->next;
}
}
node* insertatspecloc(node* head)
{
int n;
node* temp = NULL;
node* t = head;
temp = (node*)malloc(sizeof(node));
cout << "enter the data of node after which you want to insert the
node "<<endl;
cin
>> n;
cout << "\nenter the data for last node" << endl;
cin >> temp->data;
while (t->data != n) {
t = t->next;
}
temp->next = t->next;
t->next = temp;
return head;
}
node* insertatend(node* head)
{
node* temp = NULL;
temp = (node*)malloc(sizeof(node));
cout << "\nenter the data for last node" << endl;
cin >> temp->data;
temp->next = NULL;
node* q;
q = head;
while (q->next != NULL) {
q = q->next;
}
q->next = temp;
return head;
}
int main()
{
int n, a;
struct node* head = NULL;
cout << "enter the number of nodes u want to add";
cin >> n;
head = create(n);
display(head);
cout << "\npress 1 to add node at the beginning";
cout << "\npress 2 to add node at the specific location";
cout << "\npress 3 to add node at the end\n";
cin >> a;
if (a == 1) {
insertatbeg(head);
cout << "\nlinked list after insertion:\n";
display(head);
}
if (a == 2) {
insertatspecloc(head);
cout << "\nlinked list after insertion:\n";
display(head);
}
if (a == 3) {
insertatend(head);
cout << "\nLinked list after insertion:\n";
display(head);
}
}
#包括
#包括
#包括
使用名称空间std;
类型定义结构节点{
int数据;
结构节点*下一步;
}节点;
节点*创建(int n)
{
节点*temp=NULL;
node*head=NULL;
节点*p;
int i;
对于(i=0;inext=NULL;
if(head==NULL){
压头=温度;
}
否则{
p=水头;
while(p->next!=NULL){
p=p->next;
}
p->next=温度;
}
}
回流头;
}
节点*插入标签(节点*头部)
{
节点*temp=NULL;
temp=(node*)malloc(sizeof(node));
cout temp->data;
温度->下一步=头部;
压头=温度;
回流头;
}
无效显示(节点*头部)
{
node*t=NULL;
t=头部;
while(t!=NULL){
下一步是收集数据;
}
}
节点*insertatspecloc(节点*head)
{
int n;
节点*temp=NULL;
节点*t=头部;
temp=(node*)malloc(sizeof(node));
cout数据;
而(t->data!=n){
t=t->next;
}
temp->next=t->next;
t->next=温度;
回流头;
}
节点*插入端(节点*头部)
{
节点*temp=NULL;
temp=(node*)malloc(sizeof(node));
cout temp->data;
temp->next=NULL;
节点*q;
q=头;
while(q->next!=NULL){
q=q->next;
}
q->next=温度;
回流头;
}
int main()
{
int n,a;
结构节点*head=NULL;
cout>n;
头=创建(n);
显示器(头部);
cout当调用insertabeg(head);
指针的副本作为函数的参数传递,然后在函数中修改局部变量(head的副本)
或者,您可以在上面的行中调用函数,而无需赋值,但您应该通过引用传递head
,以便能够修改函数中原始传递的对象:
node *insertatbeg(node *& head) // pass pointer by reference
{
node *temp=NULL;
//...
head = temp; // now it works
这不是C++,这是“C打印到CUT”的建议——在编写处理菜单和提示的任何代码之前,您可以通过直接调用函数来轻松地测试链表的工作。插入。这样,您的问题就不会被菜单和提示等不重要的事情淹没,此外,它还使您更容易对代码进行单元测试。如果不这样做,您会遇到堆积如山的代码,并且不知道从哪里开始找出错误。例如:int main(){head(create(5));insertabeg(head);insertabeg(head);}
并查看其是否有效。您的建议正常,代码正常,但其他功能(如在特定位置和结尾插入)在没有此功能的情况下也正常工作(head=insertabeg(head);),为什么会发生这种情况?在这些函数中,您不需要修改“head”,它用于迭代其他元素,以找到要放置新项的位置。
head = insertatbeg(head);
node *insertatbeg(node *& head) // pass pointer by reference
{
node *temp=NULL;
//...
head = temp; // now it works