0% found this document useful (0 votes)
6 views

Array Implementation - Stack, Queue, Circular Queue_ Attempt Review

Uploaded by

anneanitap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Array Implementation - Stack, Queue, Circular Queue_ Attempt Review

Uploaded by

anneanitap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Started on Sunday, 29 September 2024, 9:37 PM

State Finished
Completed on Tuesday, 19 November 2024, 2:32 PM
Time taken 50 days 16 hours
Marks 70.00/70.00
Grade 100.00 out of 100.00
Name HARSHINE S 730423243041
Question 1

Correct

Mark 10.00 out of 10.00

Implement push, pop and peek functions using array

Java students - Do not use collections


MAX stack size N

1 - PUSH
2 - POP
3 - PEEK
4 - EXIT

For example:

Input Result

5 Stack underflow
2 PUSH 10 SUCCESSFUL
1 10 PUSH 20 SUCCESSFUL
1 20 PUSH 30 SUCCESSFUL
1 30 PUSH 40 SUCCESSFUL
1 40 PUSH 50 SUCCESSFUL
1 50 Stack overflow
1 60 50
3 POP 50
2 40
3 POP 40
2 30
3 POP 30
2 20
3 POP 20
2 10
3 POP 10
2 STACK EMPTY
3 Stack underflow
2
4

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #include <stdlib.h>
3▼ struct Node {
4 int data;
5 struct Node* prev;
6 struct Node* next;
7 };
8
9▼ struct Stack {
10 struct Node* top;
11 int size;
12 int maxSize;
13 };
14 ▼ struct Node* newNode(int data) {
15 struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
16 temp->data = data;
17 temp->prev = NULL;
18 temp->next = NULL;
19 return temp;
20 }
21 ▼ struct Stack* createStack(int maxSize) {
22 struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack))
( ) ( ( ))
23 stack->top = NULL;
24 stack->size = 0;
25 stack->maxSize = maxSize;
26 return stack;
27 }
28
29 ▼ void push(struct Stack* stack, int data) {
30 ▼ if (stack->size == stack->maxSize) {
31 printf("Stack overflow\n");
32 return;
33 }
34
35 struct Node* temp = newNode(data);
36
37 ▼ if (stack->top == NULL) {
38 stack->top = temp;
39 ▼ } else {
40 temp->prev = stack->top;
41 stack->top->next = temp;
42 stack->top = temp;
43 }
44
45 stack->size++;
46 printf("PUSH %d SUCCESSFUL\n", data);
47 }
48
49 ▼ void pop(struct Stack* stack) {
50 ▼ if (stack->top == NULL) {
51 printf("Stack underflow\n");
52 return;

Input Expected Got

 5 Stack underflow Stack underflow 


2 PUSH 10 SUCCESSFUL PUSH 10 SUCCESSFUL
1 10 PUSH 20 SUCCESSFUL PUSH 20 SUCCESSFUL
1 20 PUSH 30 SUCCESSFUL PUSH 30 SUCCESSFUL
1 30 PUSH 40 SUCCESSFUL PUSH 40 SUCCESSFUL
1 40 PUSH 50 SUCCESSFUL PUSH 50 SUCCESSFUL
1 50 Stack overflow Stack overflow
1 60 50 50
3 POP 50 POP 50
2 40 40
3 POP 40 POP 40
2 30 30
3 POP 30 POP 30
2 20 20
3 POP 20 POP 20
2 10 10
3 POP 10 POP 10
2 STACK EMPTY STACK EMPTY
3 Stack underflow Stack underflow
2
4

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.
Question 2

Correct

Mark 20.00 out of 20.00

Implement enqueue, dequeue functions using an Array

MAX queue size N

First input is the size of the queue N - integer

1 - ENQUEUE

2 - DEQUEUE

3 - DISPLAY

4 - EXIT

For example:

Input Result

3 40 Cannot Enqueue Queue Full


1 10 Dequeue
10 20 30
1 20 Dequeue
20 30
1
30
1
40
2
3
2
3
4

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #include <stdlib.h>
3▼ struct Node {
4 int data;
5 struct Node* next;
6 };
7▼ struct Queue {
8 struct Node *front, *rear;
9 int size;
10 int maxSize;
11 };
12 ▼ struct Node* newNode(int data) {
13 struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
14 temp->data = data;
15 temp->next = NULL;
16 return temp;
17 }
18 ▼ struct Queue* createQueue(int maxSize) {
19 struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue))
20 queue->front = queue->rear = NULL;
21 queue->size = 0;
22 queue->maxSize = maxSize;
23 return queue;
24 }
25 ▼ void enqueue(struct Queue* queue, int data) {
26 ▼ if (queue->size == queue->maxSize) {
27 printf("%d Cannot Enqueue Queue Full\n",data);
28 return;
29 }
30 struct Node* temp = newNode(data);
31 ▼ if (queue->rear == NULL) {
32 queue->front = queue->rear = temp;
33 ▼ } else {
34 queue->rear->next = temp;
35 queue->rear = temp;
36 }
37 queue->size++;
38 //printf("%d Enqueued\n", data);
39 }
40 ▼ void dequeue(struct Queue* queue) {
41 ▼ if (queue->front == NULL) {
42 printf("Queue Empty\n");
43 return;
44 }
45
46 struct Node* temp = queue->front;
47 printf("%d Dequeue\n", temp->data);
48 queue->front = queue->front->next;
49
50 ▼ if (queue->front == NULL) {
51 queue->rear = NULL;
52 }

Input Expected Got

 3 40 Cannot Enqueue Queue Full 40 Cannot Enqueue Queue Full 


1 10 Dequeue 10 Dequeue
10 20 30 20 30
1 20 Dequeue 20 Dequeue
20 30 30
1
30
1
40
2
3
2
3
4

Passed all tests! 

Correct

Marks for this submission: 20.00/20.00.


Question 3

Correct

Mark 20.00 out of 20.00

Implement the following functions in a circular queue using an array with MAX_SIZE as 4

1. ENQUEUE
2. DEQUEUE
3. DISPLAY
4. EXIT

For example:

Input Result

1 30 40
10
1
20
1
30
2
2
1
40
3
4

1 Queue Overflow
10 10 20 30 40
1
20
1
30
1
40
1
50
3
4

2 Queue Underflow

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX_SIZE 4
5
6 int queue[MAX_SIZE];
7 int front = -1, rear = -1;
8
9 // Function to check if the queue is full
10 ▼ int isFull() {
11 return (front == (rear + 1) % MAX_SIZE);
12 }
13
14 // Function to check if the queue is empty
15 ▼ int isEmpty() {
16 return (front == -1);
17 }
18
19 // Function to enqueue an element into the queue
20 void enqueue(int element) {
20 ▼ void enqueue(int element) {
21 ▼ if (isFull()) {
22 printf("Queue Overflow\n");
23 ▼ } else {
24 if (front == -1) front = 0;
25 rear = (rear + 1) % MAX_SIZE;
26 queue[rear] = element;
27 }
28 }
29
30 // Function to dequeue an element from the queue
31 ▼ void dequeue() {
32 ▼ if (isEmpty()) {
33 printf("Queue Underflow\n");
34 ▼ } else {
35 ▼ if (front == rear) {
36 front = rear = -1; // Reset queue after last element is
37 ▼ } else {
38 front = (front + 1) % MAX_SIZE;
39 }
40 }
41 }
42
43 // Function to display the elements of the queue
44 ▼ void display() {
45 ▼ if (isEmpty()) {
46 printf("Queue Underflow\n");
47 ▼ } else {
48 int i = front;
49 ▼ while (1) {
50 printf("%d", queue[i]);
51 if (i == rear) break;
52 printf(" ");

Input Expected Got

 1 30 40 30 40 
10
1
20
1
30
2
2
1
40
3
4

 1 Queue Overflow Queue Overflow 


10 10 20 30 40 10 20 30 40
1
20
1
30
1
40
1
50
3
4

 2 Queue Underflow Queue Underflow 

Passed all tests! 

Correct
Marks for this submission: 20.00/20.00.
Question 4

Correct

Mark 20.00 out of 20.00

Write a program to implement Priority queue using a linked list. Following input is given for each element
1) Value of the element
2) Priority of the element
Priority 0 is the highest priority
Priority 100 is the lowest priority

1 - enqueue
2 - dequeue
3 - display
4 - exit

For example:

Input Result

1 30 10 43 20
10
78
1
20
79
1
30
0
1
43
78
3

1 10 43 20
10
78
1
20
79
1
30
0
2
1
43
78
3

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #include <stdlib.h>
3
4▼ struct Node {
5 int data;
6 int priority;
7 struct Node* next;
8 };
9
10 ▼ struct Node* newNode(int d, int p) {
11 struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
12 temp->data = d;
13 temp->priority = p;
14 temp->next = NULL;
p ;
15 return temp;
16 }
17
18 ▼ void enqueue(struct Node** head, int d, int p) {
19 struct Node* start = *head;
20 struct Node* temp = newNode(d, p);
21
22 ▼ if (*head == NULL || (*head)->priority > p) {
23 temp->next = *head;
24 *head = temp;
25 ▼ } else {
26 ▼ while (start->next != NULL && start->next->priority <= p) {
27 start = start->next;
28 }
29 temp->next = start->next;
30 start->next = temp;
31 }
32 }
33
34 ▼ void dequeue(struct Node** head) {
35 ▼ if (*head == NULL) {
36 return;
37 }
38 struct Node* temp = *head;
39 *head = (*head)->next;
40 free(temp);
41 }
42
43 ▼ void display(struct Node* head) {
44 struct Node* temp = head;
45 ▼ while (temp != NULL) {
46 printf("%d ", temp->data);
47 temp = temp->next;
48 }
49 printf("\n");
50 }
51
52 ▼ int main() {

Input Expected Got

 1 30 10 43 20 30 10 43 20 
10
78
1
20
79
1
30
0
1
43
78
3
Input Expected Got

 1 10 43 20 10 43 20 
10
78
1
20
79
1
30
0
2
1
43
78
3

Passed all tests! 

Correct
Marks for this submission: 20.00/20.00.

You might also like