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

Stack Operations - Program_ 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)
3 views

Stack Operations - Program_ 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/ 8

Started on Tuesday, 19 November 2024, 2:32 PM

State Finished
Completed on Tuesday, 19 November 2024, 2:34 PM
Time taken 1 min 45 secs
Marks 50.00/50.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 doubly linked list.

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

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.
Question 3

Correct

Mark 20.00 out of 20.00

Implement enqueue, dequeue functions using an array.

MAX stack size N

1 - ENQUEUE

2 - DEQUEUE

3 - DISPLAY

4 - EXIT

For example:

Input Result

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

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 #define MAX_SIZE 4
3
4 int queue[MAX_SIZE];
5 int front = -1, rear = -1;
6
7▼ void enqueue(int value) {
8▼ if ((rear + 1) % MAX_SIZE == front) {
9 printf("Queue Overflow\n");
10 return;
11 }
12 ▼ if (front == -1) {
13 front = 0;
14 }
15 rear = (rear + 1) % MAX_SIZE;
16 queue[rear] = value;
17 }
18
19 ▼ void dequeue() {
20 ▼ if (front == -1) {
21 printf("Queue Underflow\n");
22 return;
23 }
24 ▼ if (front == rear) {
25 front = rear = -1;
26 ▼ } else {
27 front = (front + 1) % MAX_SIZE;
28 }
29 }
30
31 ▼ void display() {
32 ▼ if (front == -1) {
( ) {
33 printf("\n");
34 return;
35 }
36 int i = front;
37 ▼ while (1) {
38 printf("%d", queue[i]);
39 ▼ if (i == rear) {
40 break;
41 }
42 printf(" ");
43 i = (i + 1) % MAX_SIZE;
44 }
45 printf("\n");
46 }
47
48 ▼ int main() {
49 int choice, value;
50 ▼ while (1) {
51 scanf("%d", &choice);
52 ▼ switch (choice) {

Input Expected Got

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

Passed all tests! 

Correct

Marks for this submission: 20.00/20.00.

You might also like