forked from anurag1802/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insertLL.cpp
94 lines (89 loc) · 1.47 KB
/
insertLL.cpp
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
91
92
93
94
#include<iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *start=NULL, *newnode, *temp;
//insert from beginning//
void insertbegin (void)
{
newnode->next=start;
start=newnode;
}
//insert from end//
void insertend (void)
{
temp=start;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next = newnode;
}
//insert from pos//
void insertpos (int p)
{
int count =1;
temp = start;
while(count<p)
{
temp = temp->next;
count++;
}
newnode->next = temp->next;
temp->next = newnode;
}
//display function//
void display (void)
{
temp=start;
while(temp != NULL)
{
cout<<temp->data;
temp=temp->next;
}
}
//main function//
int main()
{
int p,choice;
do{
cout<<"\n1:Insert from beginning\n";
cout<<"\n2:Insert from end\n";
cout<<"\n3:Insert from position\n";
cout<<"\n4:Display\n";
cout<<"\n5:Exit\n";
cout<<"Enter your choice : ";
cin>>choice;
if(choice==1 || choice==2 || choice==3)
{
newnode=new node;
cout<<"Enter data : ";
cin>>newnode->data;
newnode->next=NULL;
}
switch(choice)
{
case 1:
insertbegin();
break;
case 2:
insertend();
break;
case 3:
cout<<"Enter your position :";
cin>>p;
insertpos(p);
break;
case 4:
display();
break;
case 5:
break;
default:
cout<<"Please try again... Invalid choice!!\n";
}
}
while (choice != 5);
}