-
Notifications
You must be signed in to change notification settings - Fork 0
/
LLDS.CPP
111 lines (108 loc) · 1.82 KB
/
LLDS.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
109
110
111
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node
{ int info;
Node *next;
} *start, *newptr, *save, *ptr,*rear,*temp,*temp2;
Node *create_new_node(int);
void Insert_End(Node*);
void Display(Node*);
void DelNode(int);
void main()
{ clrscr();
start=rear=NULL;
int inf,nno;
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;
}
do
{ cout<<"To delete an element enter y or Y.. N to exit..\n";
cin>>ch;
if(ch=='y'||ch=='Y')
{ cout<<"Enter the element to be deleted: ";
cin>>nno;
DelNode(nno);
Display(start);
}
else
{
Display(start);
}
}while(ch=='y'||ch=='Y');
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 DelNode(int n2)
{
temp2=temp=start;
while(temp->info!=n2)
{
temp=temp->next;
if(temp->info!=n2)
temp2=temp;
}
cout<<"\n\nn2= "<<n2<<"\n\n";
if(temp==start)
{ cout<<"\n\nstart\n\n";
start=start->next;
ptr=temp;
delete ptr;
}
else if(temp==rear)
{
rear=temp2;
rear->next=NULL;
}
else
{ ptr=temp;
temp=temp->next;
delete ptr;
temp2->next=temp;
}
}