-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLIA.CPP
108 lines (103 loc) · 1.65 KB
/
LLIA.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{ int info;
Node *next;
} *start, *newptr, *save, *ptr,*rear,*temp;
Node *create_new_node(int);
void Insert_End(Node*);
void Insert_Pos(Node*,pos);
void Display(Node*);
void main()
{ clrscr();
start=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')
{
clrscr();
cout<<"\n Enter info for new Node:- ";
cin>>inf;
cout<<"\n Creating new node!!! Press Enter to continue...";
getch();
newptr=create_new_node(inf);
if(newptr!=NULL)
{
cout<<"\n\nNew Node Created...";
getch();
}
else
{
cout<<"Cannot create new node.. Aborting";
getch();
exit(1);
}
cout<<"\nInserting in End of the list... \n";
Insert_End(newptr);
cout<<"\n\nNow the list is:- ";
Display(start);
cout<<"Enter y or Y to enter more.. N to continue..\n";
cin>>ch;
}
ch='y';
while(ch=='y'||ch='Y')
{
cout<<"\nEnter y or Y to enter at any position..\n";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"Enter the position:- ";
cin>>pos;
Insert_Pos(newptr,pos);
}
}
getch();
}
Node *create_new_node(int n)
{ ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void Insert_End(Node* np)
{ if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void Display(Node* np)
{ while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<"!!!\n";
}
void Insert_Pos(Node* np,int n)
{
temp=start;
while(n>1)
{
temp=temp->next;
n--;
}
if(start==NULL)
start=rear=np;
else
{
if(pos==1)
{
save=start;
start=np;
np->next=save;
}
else
{
start->next=np;
np->next=temp;
}
}
}