forked from k-samarth/Data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleLinkedList.cpp
More file actions
90 lines (85 loc) · 1.98 KB
/
Copy pathSingleLinkedList.cpp
File metadata and controls
90 lines (85 loc) · 1.98 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
#include <iostream>
using namespace std;
struct node{
int data;
node* link;
};
node *head =NULL;
node* 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;
return head;
}
node* InsertEnd(node* head){
int element;
node* temp=new node;
cout<<"Please enter the element"<<endl;
cin>>element;
node* head1 =head;
temp->data=element;
temp->link=NULL;
if(head==NULL){
head=temp;
return head;
}
while(head->link!=NULL){
head=head->link;
}
head->link=temp;
return head1;
}
node* DeleteFront(node* head){
node* temp = new node;
temp=head;
head=head->link;
temp->link=NULL;
delete(temp);
return head;
}
node* DeleteEnd(node* head){
node* temp = new node;
temp=head;
node* head1 =head;
while(head->link!=NULL){
temp=head;
head=head->link;
}
temp->link=NULL;
delete(head);
return head1;
}
void Display(node* head){
while(head!=NULL){
cout<<head->data<<" ";
head=head->link;
}
}
int main()
{
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:head=InsertFront(head);
break;
case 2:head=InsertEnd(head);
break;
case 3:head=DeleteFront(head);
break;
case 4:head=DeleteEnd(head);
break;
case 5:Display(head);
break;
case 6:return 0;
default:cout<<"Sorry the entered number is invalid"<<endl;
}
}
return 0;
}