Data Structure Linked List Implementation Source Code

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

C:\Users\sajee\source\repos\DSASS4\DSASS4\Main.

cpp 1
1 /*
2 Assignment # 04
3 Sajeel Ahmad Hassan (032)
4 Umair Mustajab (048)
5 Abdul Moiz (002)
6 */
7 #include<iostream>
8 #include "Dequeue.h";
9 #include "Stack.h";
10 #include "Queue.h";
11 using namespace std;
12
13 int main() {
14 Stack<int> objStack;
15 Queue<int> objQueue;
16 Dequeue<int> objDequeue;
17
18 //Implementing Priority Queue
19 int data, choice;
20 Queue<int> objPQ[3] = {}; // <- array of 3 objects is created
21
22 do
23 {
24
25 cout << " Press:";
26 cout << "\n 1 to insert data in object 1:";
27 cout << "\n 2 to insert data in object 2:";
28 cout << "\n 3 to insert data in object 3:";
29 cout << "\n Press 0 to display:";
30 cout << "\n >> ";
31 cin >> choice;
32
33 if (choice == 1)
34 {
35 cout << "\n Enter data element to be inserted\n";
36 cout << "\n >> ";
37 cin >> data;
38 objPQ[0].insert(data);
39 }
40 else if (choice == 2)
41 {
42 cout << "\n Enter data element to be inserted\n";
43 cout << "\n >> ";
44 cin >> data;
45 objPQ[1].insert(data);
46 }
47 else if (choice == 3)
48 {
49 cout << "\n Enter data element to be inserted\n";
50 cout << "\n >> ";
51 cin >> data;
52 objPQ[2].insert(data);
C:\Users\sajee\source\repos\DSASS4\DSASS4\Main.cpp 2
53 }
54 else {
55 cout << "\nInvalid!!!\n";
56 }
57
58 } while (choice != 0);
59
60 //Objects with highest priority will have their data inserted and removed first!

61
62 for (int k = 0; k < 3; k++)
63 {
64 cout << "\nData of " << k << " th object\n";
65 for (int l = 0; l < 5; l++)
66 {
67 objPQ[k].remove();
68 }
69 cout << endl;
70 }
71 //Implementation of Priority Queue Ended
72
73 //Implementing Stack.h
74 do {
75 cout << "\n Press:";
76 cout << "\n 1. to push";
77 cout << "\n 2 to pop";
78 cout << "\n >> ";
79 cin >> choice;
80
81 if (choice == 1)
82 {
83 cout << "\n Enter data element to be pushed\n";
84 cout << "\n >> ";
85 cin >> data;
86 objStack.push(data);
87 }
88 else if (choice == 2)
89 {
90 objStack.pop();
91 }
92 else
93 {
94 cout << "\nInvalid\n";
95 }
96 } while (choice != 0);
97
98 //Implementing Queue
99
100
101
102 do {
103 cout << "\n Press:";
C:\Users\sajee\source\repos\DSASS4\DSASS4\Main.cpp 3
104 cout << "\n 1. to insert";
105 cout << "\n 2 to remove";
106 cout << "\n >> ";
107 cin >> choice;
108
109 if (choice == 1)
110 {
111 cout << "\n Enter data element to be inserted\n";
112 cout << "\n >> ";
113 cin >> data;
114 objQueue.insert(data);
115 }
116 else if (choice == 2)
117 {
118 objQueue.remove();
119 }
120 else
121 {
122 cout << "\nInvalid\n";
123 }
124 } while (choice != 0);
125
126 //Implementing Dequeue.h
127
128 do
129 {
130 cout << "\n Press:";
131 cout << "\n 1. to insert from front";
132 cout << "\n 2. to insert from rear";
133 cout << "\n 3. to remove from rear";
134 cout << "\n 4. to remove from front";
135 cout << "\n Choice: >> ";
136 cin >> choice;
137
138 if (choice == 1)
139 {
140 cout << "\n Enter data element to be inserted from front:";
141 cout << "\n >> ";
142 cin >> data;
143 objDequeue.insertFront(data);
144 }
145 else if (choice == 2)
146 {
147 cout << "\n Enter data element to be inserted from rear:";
148 cout << "\n >> ";
149 cin >> data;
150 objDequeue.insertEnd(data);
151 }
152 else if (choice == 3)
153 {
154 objDequeue.deleteRear();
155 }
C:\Users\sajee\source\repos\DSASS4\DSASS4\Main.cpp 4
156 else if (choice == 4)
157 {
158 objDequeue.deleteFront();
159 }
160 else
161 {
162 cout << "\n Invalid\n";
163 }
164 } while (choice != 0);
165
166 }
C:\Users\sajee\source\repos\DSASS4\DSASS4\Link.h 1
1 #include<iostream>
2 using namespace std;
3
4 typedef struct Node* Nodeptr;
5
6 struct Node {
7int info;
8Nodeptr next;
9Nodeptr prev;
10 };
11
12 Nodeptr list = NULL;
13
14 template <class T>
15 class Link {
16 public:
17 Nodeptr makeNode()
18 {
19 Nodeptr p = new Node();
20 p->info = 0;
21 p->next = NULL;
22 p->prev = NULL;
23 return p;
24 }
25
26 void iAS(T x) {
27 Nodeptr q = makeNode();
28 q->info = x;
29 q->next = list;
30 list = q;
31 q->prev = NULL;
32 }
33
34 void iAM(T x, T y) // insertAfterY(int x, int y)
35 {
36 if (list == NULL)
37 iAS(x);
38 else
39 {
40 Nodeptr q = 0;
41 Nodeptr p;
42 for ( p = list; p != NULL && p->info != y; p = p->next);
43 if (p == NULL)
44 {
45 cout << "Not found";
46 //iAS(x);
47 }
48 Nodeptr r = p->next;
49 q = makeNode();
50 q->info = x;
51 q->next = p->next;
52 q->prev = p;
C:\Users\sajee\source\repos\DSASS4\DSASS4\Link.h 2
53 r->prev = q;
54 p->next = q;
55 }
56 }
57
58 void iAE(T x)
59 {
60 if (list == NULL)
61 iAS(x);
62 else
63 {
64 Nodeptr q = NULL;
65 Nodeptr p;
66 for ( p = list; p->next != NULL; p = p->next);
67 q = makeNode();
68 q->info = x;
69 q->next = NULL;
70 q->prev = p;
71 p->next = q;
72 }
73 }
74
75 int dAS()
76 {
77 if (list == NULL)
78 cout << "\nUnderflow\n";
79 else
80 {
81 Nodeptr p = list;
82 list = list->next;
83 list->prev = NULL;
84 int temp = p->info;
85 delete p;
86 return temp;
87 }
88 }
89
90 int dAE()
91 {
92 if (list == NULL)
93 cout << "Underflow";
94 if (list->next == NULL)
95 return (dAS());
96 else
97 {
98 Nodeptr q = NULL;
99 Nodeptr p;
100 for ( p = list; p->next != NULL; p = p->next)
101 {
102 q = p;
103 }
104 q->next = NULL;
C:\Users\sajee\source\repos\DSASS4\DSASS4\Link.h 3
105 int temp = p->info;
106 delete p;
107 return temp;
108 }
109 }
110
111 int dAM(T y)
112 {
113 if (list == NULL)
114 cout << "Underflow";
115 if (list->next == NULL)
116 return (dAS());
117 else
118 {
119 Nodeptr q = NULL;
120 Nodeptr p;
121 for (p = list; p != NULL && p->info != y; p = p->next)
122 q = p; //P points to the last node and q points to the 2nd last
123 if (p == NULL)
124 cout << "Not found";
125 else
126 {
127 Nodeptr r = p->next;
128 q->next = p->next;
129 r->prev = q;
130 int temp = p->info;
131 delete p;
132 return temp;
133
134 }
135 }
136 }
137
138 Nodeptr getHead() {
139 return list;
140 }
141
142 Nodeptr getTail() {
143 Nodeptr p;
144 for (p = list; p->next != NULL; p = p->next);
145 return p;
146 }
147 };
148
C:\Users\sajee\source\repos\DSASS4\DSASS4\Dequeue.h 1
1 #include<iostream>
2 #include"Link.h";
3 using namespace std;
4
5 template <class T>
6 class Dequeue {
7 private:
8Nodeptr front, rear;
9Link<T> obj;
10 public:
11 Dequeue() {
12 front = obj.getHead();
13 rear = obj.getTail();
14 }
15
16 bool isEmpty() {
17 if (front == NULL && rear == NULL) {
18 return 1;
19 }
20 else {
21 return 0;
22 }
23 }
24
25 void insertFront(T data) {
26 obj.iAS(data);
27 front = obj.getHead();
28 }
29
30 void insertEnd(T data){
31 obj.iAE(data);
32 rear = obj.getTail();
33 }
34
35 void deleteFront() {
36 if (isEmpty()) {
37 cout << "\n Double Ended Queue Underflow\n";
38 }
39 else {
40 obj.dAS();
41 front = obj.getHead();
42 }
43 }
44
45 void deleteRear() {
46 if (isEmpty()) {
47 cout << "\n Double Ended Queue Overflow\n";
48 }
49 else {
50 obj.dAE();
51 rear = obj.getTail();
52 }
C:\Users\sajee\source\repos\DSASS4\DSASS4\Dequeue.h 2
53 }
54
55 };
C:\Users\sajee\source\repos\DSASS4\DSASS4\Queue.h 1
1 #include<iostream>
2 #include"Link.h";
3 using namespace std;
4
5 template <class T>
6 class Queue {
7 private:
8Nodeptr front, rear;
9Link<T> obj;
10 public:
11 Queue() {
12 front = obj.getHead();
13 rear = obj.getTail();
14 }
15
16 bool isEmpty() {
17 if (rear == NULL) {
18 return 1;
19 }
20 else {
21 return 0;
22 }
23 }
24
25 void insert(T data) {
26 obj.iAE(data);
27 rear = obj.getTail();
28 }
29
30 void remove() {
31 if (isEmpty()) {
32 cout << "\n Queue Underflow\n";
33 }
34 else {
35 cout << "\n Removed element is: " << obj.dAS() << endl;
36 front = obj.getHead();
37 }
38 }
39 };
C:\Users\sajee\source\repos\DSASS4\DSASS4\Stack.h 1
1 #include<iostream>
2 #include "Link.h";
3 using namespace std;
4
5
6 template <class T>
7 class Stack {
8 private:
9Nodeptr top;
10 Link<T> obj;
11 public:
12 Stack() {
13 top = obj.getHead();
14 }
15
16 bool isEmpty() {
17 if (top == NULL) {
18 return 1;
19 }
20 else {
21 return 0;
22 }
23 }
24
25 void push(T data) {
26 obj.iAS(data);
27 top = obj.getHead();
28 }
29
30 void pop() {
31 if (isEmpty()) {
32 cout << "\n Stack Underflow\n";
33 }
34 else {
35 cout << "\n Data removed is >> " << obj.dAS() << endl;
36 top = obj.getHead();
37 }
38 }
39
40 };

You might also like