-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstack using link list given mem func create disp insert.cpp
95 lines (95 loc) · 1.84 KB
/
stack using link list given mem func create disp insert.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
// Filename: \\U2Chap09\IM9da.CPP
// Addition and deletion of new books using linked list
# include<iostream.h>
# include <conio.h>
class NODE
{
public:
int bookno;
NODE *link;
};
class LINKLIST
{
private:
NODE *first,*last,*temp;
public:
void NCREATE(int); // Create a linked list with n nodes
void insertatk(int); // Inserting at kth position
void display(); // Display the linked list
};
void LINKLIST::NCREATE( int n)
{
first = new NODE;
cout << "\n Enter the book number ";
cin >> first->bookno;
first->link = NULL;
temp = first;
for(int i =1;i<n;i++)
{
last = new NODE;
cout << "\n Enter the book number ";
cin >> last->bookno;
last->link = NULL;
temp->link = last;
temp = last;
}
}
void LINKLIST::insertatk(int j) // Function to insert the node at kth position
{
int i = 0;
NODE *newnode,*back;
newnode = new NODE;
cout<< "\nEnter the data value ";
cin>>newnode->bookno;
newnode->link = NULL;
temp = first;
while (i < (j-1))
{
back = temp;
temp = temp->link;
i++;
}
back->link = newnode;
newnode->link = temp;
}
void LINKLIST::display() //Function to display the link list
{
temp = first;
cout<< "\n The linked list is \n";
while (temp != NULL)
{
cout<< "\n"<<temp->bookno;
temp = temp->link;
}
getch();
}
void main()
{
int ch,n,k;
char ch1 = 'y';
LINKLIST list;
clrscr();
cout << "\n Enter how many nodes in the list ";
cin >> n;
list.NCREATE(n);
do
{
clrscr();
cout<< "\n1. For insert ";
cout << "\n2. For display ";
cout << "\n3. For quit ";
cout << "\nEnter your choice ";
cin>>ch;
switch (ch)
{
case 1:
cout << "\nEnter the position at which insertion is required ";
cin >> k;
list.insertatk(k);
break;
case 2 :
list.display();
break;
}
} while (ch != 3);
}