Professional Documents
Culture Documents
Q1: Write a program to implement a 3-stacks of size ‘m’ in an array of size ‘n’ with
all the basic operations such as IsEmpty(i), Push(i), Pop(i), IsFull(i) where ‘i’ denotes
the stack number (1,2,3), m n/3. Stacks are not overlapping each other. Leftmost stack
facing the left direction and other two stacks are facing in the right direction.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#ifndef T
#define T int
#define FT "%d "
#endif
typedef T Type;
typedef struct
{
Type *arr;
int pointer[3];
int top[3];
int bottom[3];
int size;
} stack;
3 * size};
int bottom[3] = {size - 1,
size,
2 * size};
memcpy(s->top, top, sizeof(top));
memcpy(s->bottom, bottom, sizeof(bottom));
memcpy(s->pointer, bottom, sizeof(bottom));
s->size = size;
}
void print_stack(stack s)
{
printf("Stacks current state:\n");
int i, j, k;
char *val[3];
for (i = s.top[0] + 1, j = s.top[1] - 1, k = s.top[2] - 1; i <= s.bottom[0]; i++, j--, k--)
{
if (i <= s.pointer[0])
{
val[0] = "-";
}
else
{
val[0] = get_string(&s, i);
}
if (j >= s.pointer[1])
{
val[1] = "-";
}
else
{
val[1] = get_string(&s, j);
}
if (k >= s.pointer[2])
{
val[2] = "-";
}
else
{
val[2] = get_string(&s, k);
}
printf("|\t%s\t|\t%s\t|\t%s\t|\n", val[0], val[1], val[2]);
}
}
int main()
{
stack new_stack;
initialize_stack(&new_stack, 3);
print_stack(new_stack);
push(&new_stack, 1, 3);
print_stack(new_stack);
push(&new_stack, 1, 4);
push(&new_stack, 2, 4);
push(&new_stack, 2, 5);
push(&new_stack, 3, 3);
print_stack(new_stack);
pop(&new_stack, 2);
print_stack(new_stack);
push(&new_stack, 2, 6);
push(&new_stack, 3, 10);
print_stack(new_stack);
print_arr(&new_stack);
return 0;
}
OUTPUT:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/**
* This first part of the code defines the data structure for the submitted assignments.
* This is a basic implementation of the stack data structure.
*/
#define CLASS_STRENGTH 17
char *submissions[CLASS_STRENGTH] = {"17BEC0071", "17BIE1000", "17BCE1014",
"17BCE0175", "17BCL0031", "17BEE0077", "17BEI0078", "17BPE0079", "17BES0080",
"17BEI0081", "17BEU0082", "17BDE0083", "17BEP0084", "17BEE0085", "17BCE0086",
"17BCE0087", "17BME0088"};
typedef struct
{
char **arr;
int pointer;
int size;
} stack;
/**
* Given below is the definitation of the data structure use to get the last 10 students.
* For this I will use a queue data structure of capacity 10. Whener a new value comes if
* queue is full I dequeue and then enqueque and check for the condition if the stack is
empty or
* not. At the end I will have only 10 numbers who submitted first;
*/
typedef struct
{
char **arr;
int back;
int size;
} queue;
print_arr(queue *q)
{
int i;
printf("Last 10\n");
for (i = 9; i >= 0; i--)
{
printf("%s ", q->arr[i]);
}
printf("\n");
}
int main()
{
stack papers;
initialize_stack(&papers);
print_stack(&papers);
queue last_10;
init_queue(&last_10, 10);
while (!is_stack_empty(&papers))
{
enque(&last_10, pop(&papers));
}
print_arr(&last_10);
return 0;
}
OUTPUT:
Q3: To facilitate a thorough net surfing, any web browser has back and forward
buttons that allow the user to move backward and forward through a series of web
pages. To allow the user to move both forward and backward two stacks are
employed. When the user presses the back button, the link to the current web page is
stored on a separate stack for the forward button. As the user moves backward
through a series of previous pages, the link to each page is moved in turn from the
back to the forward stack. When the user presses the forward button, the action is the
reverse of the back button. Now the item from the forward stack is popped, and
becomes the current web page. The previous web page is pushed on the back stack.
Simulate the functioning of these buttons using array implementation of Stack. Also
provide options for displaying the contents of both the stacks whenever required.
CODE:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 100
int forw[100],back[100],curr,ch,top1=-1,top2=-1;
void push_forw() {
if(top1+1>=SIZE)
printf("\nOverflow\n");
else{
top1++;
forw[top1]=curr;
}
}
void push_back() {
if(top2+1>=SIZE)
printf("\nOverflow\n");
else {
top2++; back[top2]=curr;
}
}
void pop_forw() {
top1--;
}
void pop_back() {
back[top2]=curr;
top2--;
}
int main() {
printf("Enter your current element\n");
scanf("%d",&curr);
flag: printf("Do you want to go forward or backward?");
scanf("%s",&ch);
if(ch=='F' || ch=='f' || ch=='B' || ch=='b'){
switch(ch) {
case 'F':
case 'f':
printf("Enter your element\n");
push_back();
pop_forw();
scanf("%d",&curr);
if(top1==-1)
printf("Current=%d backward=%d\n",curr,back[top2]);
else
printf("Current=%d Back=%d Forward=%d\n",curr,back[top2],forw[top1]);
goto flag;
case 'B':
case 'b':
push_forw();
pop_back();
if(top2==-1)
printf("Current=%d forward=%d\n",curr,forw[top1]);
else
printf("Current=%d Back=%d Forward=%d\n",curr,back[top2],forw[top1]);
goto flag;
}
}
else
exit(0);
}
OUTPUT: