-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSingleLinkedList_PassByReference.cpp
More file actions
90 lines (84 loc) · 2.04 KB
/
Copy pathSingleLinkedList_PassByReference.cpp
File metadata and controls
90 lines (84 loc) · 2.04 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Single Linked List in cpp Pass-By-Reference
//Instead of making global variables
//Head node in main function
#include <iostream>
using namespace std;
struct node{
int data;
node* link;
};
void InsertFront(node** head){
int element;
node* temp = new node;
cout<<"Please enter the element"<<endl;
cin>>element;
temp->data =element;
temp->link =*head;
*head=temp;
}
void InsertEnd(node** head){
int element;
node* temp=new node;
node* temp1=*head;
cout<<"Please enter the element"<<endl;
cin>>element;
temp->data=element;
temp->link=NULL;
if(*head==NULL){
*head=temp;
return;
}
while(temp1->link!=NULL){
temp1=temp1->link;
}
temp1->link=temp;
}
void DeleteFront(node** head){
node* temp = *head;
*head=(*head)->link;
temp->link=NULL;
delete(temp);
}
void DeleteEnd(node** head){
node* temp = *head;
node* temp1 =*head;
while(temp1->link!=NULL){
temp=temp1;
temp1=temp1->link;
}
temp->link=NULL;
delete(temp1);
}
void Display(node* head){
cout<<"Elements in the list are ";
while(head!=NULL){
cout<<head->data<<" ";
head=head->link;
}
cout<<endl;
}
int main()
{
node *head =NULL;
while(1){
int option;
cout<<"Please choose your option"<<endl;
cout<<"1) Insert at the front of the Linked List"<<endl<<"2) Insert at the end of the Linked List"<<endl<<"3) Delete at the front of the Linked List"<<endl<<"4) Delete at the end of the Linked List"<<endl<<"5) Display all the elements"<<endl<<"6) Exit"<<endl;
cin>>option;
switch(option){
case 1:InsertFront(&head);
break;
case 2:InsertEnd(&head);
break;
case 3:DeleteFront(&head);
break;
case 4:DeleteEnd(&head);
break;
case 5:Display(head);
break;
case 6:return 0;
default:cout<<"Sorry the entered number is invalid"<<endl;
}
}
return 0;
}