-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist_2
More file actions
59 lines (49 loc) · 1.09 KB
/
linkedlist_2
File metadata and controls
59 lines (49 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Write a C++ program to insert a new node at the beginning of a Singly Linked List.
#include <iostream>
using namespace std;
//Declare node
struct Node{
int num;
Node *next;
};
//Starting (Head) node
struct Node *head=NULL;
//Insert node at start
void insert_Node(int n){
struct Node *new_node=new Node;
new_node->num=n;
new_node->next=head;
head=new_node;
}
void NodeInsertatBegin(int new_element) {
// Create a new node
Node* new_Node = new Node();
new_Node->num = new_element;
new_Node->next = head;
head = new_Node;
}
//Display all nodes
void display_all_nodes()
{
struct Node *temp = head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
}
int main(){
insert_Node(1);
insert_Node(3);
insert_Node(5);
insert_Node(7);
insert_Node(9);
insert_Node(11);
insert_Node(13);
cout<<"Original Linked list:\n";
display_all_nodes();
cout<<"\n\nInsert a new node at the beginning of a Singly Linked List:\n";
NodeInsertatBegin(0);
display_all_nodes();
cout<<endl;
return 0;
}