0% found this document useful (0 votes)
49 views4 pages

DSA 2nd Assignment

The document contains code to implement a stack and queue using single linked lists. For the stack, it includes functions to push, pop and display elements. For the queue, it includes functions to enqueue, dequeue and display elements. The main function tests these by pushing/enqueueing elements, popping/dequeueing one element, and displaying the remaining elements for both the stack and queue implementations.

Uploaded by

wajidullahh444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views4 pages

DSA 2nd Assignment

The document contains code to implement a stack and queue using single linked lists. For the stack, it includes functions to push, pop and display elements. For the queue, it includes functions to enqueue, dequeue and display elements. The main function tests these by pushing/enqueueing elements, popping/dequeueing one element, and displaying the remaining elements for both the stack and queue implementations.

Uploaded by

wajidullahh444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Structure and Algorithm

Name: Mamoor Khan Roll No: 40


Subject: DSA Semester: 3rd. Submitted to: Dr Mohammad Tahseen

Assignment No :2

Q. No 1 Make Stack using one-way linked list?


Q. No 2 Make Queue using one-way linked list?
Ans No. 1
1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4
5 struct node
6{
7 int data;
8 struct node *link;
9 };
10 struct node *top=0;
11 //The Below Function is used to insert data to stack
12 void push(int x)
13 {
14 struct node *newnode;
15 newnode=new struct node; //Create New Node
16 newnode->data=x;
17 newnode->link=top;
18 top=newnode;
19 }
20 //The Below Function is used to display data of stack
21 void display ()
22 {
23 struct node *temp;
24 temp=top;
25 if(top==0)
26 {
27 cout<<"The Stack is Empty \n";
28 }
29 else
30 {
31 while(temp!=0)
32 {
33 cout<<temp->data<<endl;
34 temp=temp->link;
35 }
36
37 }
38 }
39 //The Below Function is used to delete data from stack
40 void pop()
41 {
42 struct node *temp;
43 temp=top;
44 if(top==0)
45 {
46 cout<<"The Stack is Empty \n";
47 }
48 else
49 {
50 top=top->link;
51 free(temp);
52
53 }
54 }
55 int main()
56 {
57 push(2);
58 push(100);
59 push(6);
60 push(15);
61 pop();
62 display();
63 return 0;
64 }

OUTPUT:
Ans No. 2

1 #include<iostream>
2 #include<stdlib.h>
3 using namespace std;
4
5 struct node
6{
7 int data;
8 struct node *link;
9 };
10 struct node *front = 0;
11 struct node *rear = 0;
12
13 // Function to insert data into the queue
14 void enqueue(int x)
15 {
16 struct node *newnode;
17 newnode = new struct node;
18 newnode->data = x;
19 newnode->link = 0;
20
21 if (rear == 0) // If the queue is empty
22 {
23 front = rear = newnode;
24 }
25 else
26 {
27 rear->link = newnode;
28 rear = newnode;
29 }
30 }
31
32 // Function to display data of the queue
33 void display()
34 {
35 struct node *temp;
36 temp = front;
37 if (front == 0)
38 {
39 cout << "The Queue is Empty \n";
40 }
41 else
42 {
43 while (temp != 0)
44 {
45 cout << temp->data << endl;
46 temp = temp->link;
47 }
48 }
49 }
50
51
52
53 // Function to delete data from the queue
54 void dequeue()
55 {
56 struct node *temp;
57 temp = front;
58 if (front == 0)
59 {
60 cout << "The Queue is Empty \n";
61 }
62 else
63 {
64 front = front->link;
65 delete temp;
66 if (front == 0) // If the last element is removed, update
67 rear
68 {
69 rear = 0;
70 }
71 }
72 }
73
74 int main()
75 {
76 enqueue(2);
77 enqueue(100);
78 enqueue(6);
79 enqueue(15);
80 dequeue();
display();
return 0;
}
OUTPUT:

You might also like