当前位置:网站首页>Linked list: insert a node in the head
Linked list: insert a node in the head
2022-06-30 03:12:00 【Ycx the beginning of rain】
take head Declare as a global variable
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* head;// take head Declare as a global variable
void Insert(int x)
{
Node* temp = new Node;
temp -> data = x;
temp -> next = head;
head = temp;
}
void Print()
{
Node* temp = head;
printf("List is: ");
while(temp != NULL)
{
printf(" %d",temp -> data);
temp = temp -> next;
}
printf("\n");
}
int main()
{
head = NULL;
printf("How many numbers?\n");
int n, x;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
printf("Enter the number \n");
scanf("%d",&x);
Insert(x);
Print();
}
return 0;
}
take head Defined as a local variable (Insert Function pass value )
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* Insert(Node* head, int x)
{
Node* temp = new Node;
temp -> data = x;
temp -> next = head;
head = temp;
return temp;
}
void Print(Node* head)
{
printf("List is: ");
while(head != NULL)
{
printf(" %d",head -> data);
head = head -> next;
}
printf("\n");
}
int main()
{
Node* head = NULL;
printf("How many numbers?\n");
int n, x;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
printf("Enter the number \n");
scanf("%d",&x);
head = Insert(head, x);
Print(head);
}
return 0;
}
take head Defined as a local variable (Insert Function address )
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* next;
};
Node* Insert(Node** pointerToHead, int x)
{
Node* temp = new Node;
temp -> data = x;
temp -> next = *pointerToHead;
*pointerToHead = temp;
}
void Print(Node* head)
{
printf("List is: ");
while(head != NULL)
{
printf(" %d",head -> data);
head = head -> next;
}
printf("\n");
}
int main()
{
Node* head = NULL;
printf("How many numbers?\n");
int n, x;
scanf("%d",&n);
for(int i = 0; i < n; i++)
{
printf("Enter the number \n");
scanf("%d",&x);
Insert(&head, x);
Print(head);
}
return 0;
}
边栏推荐
- golang bilibili直播彈幕姬
- SQLite use
- Huawei interview question: tall and short people queue up
- Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
- Customize the buttons of jvxetable and the usage of $set under notes
- JS 互相引用的问题
- How can redis+aop customize annotations to achieve flow restriction
- uniapp 地址转换经纬度
- Some technology sharing
- 快速排序、聚簇索引、尋找數據中第k大的值
猜你喜欢

HOOK Native API

Gulang bilibilibili Live Screen Jackie

How to prevent duplicate submission under concurrent requests

浅谈IDEA的优化和使用

golang bilibili直播弹幕姬

Use compose to realize the effect of selecting movie seats by panning tickets

Heavy attack -- ue5's open source digital twin solution

Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)

中断操作:AbortController学习笔记

Golang BiliBili live broadcast bullet screen
随机推荐
Neo4j---性能优化
What kind of foreign exchange trading platform is regulated and safe?
Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
Mysql 带上库名称跨库操作可能出现的问题
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
华为面试题: 高矮个子排队
Study diary: February 15, 2022
Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
Federal learning: dividing non IID samples by Dirichlet distribution
快速排序、聚簇索引、尋找數據中第k大的值
【实战技能】如何撰写敏捷开发文档
自定义MVC的使用
Link garbled escape character
Use compose to realize the effect of selecting movie seats by panning tickets
What is the concept of string in PHP
[live broadcast notes 0629] Concurrent Programming II: lock
【微信小程序】条件渲染 列表渲染 原来这样用?
Gulang bilibilibili Live Screen Jackie