-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked list
58 lines (51 loc) · 1.02 KB
/
linked list
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
#include <iostream>
using namespace std;
//Declare Node
struct Node{
int num;
Node *next;
};
//Declare starting (Head) node
struct Node *head=NULL;
//Insert node at start
void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}
//Traverse/ display all nodes (print items)
void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}
//delete node from start
void deleteItem(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
cout<<head->num<<" is removed."<<endl;
head=head->next;
}
int main(){
display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
display();
return 0;
}