You are on page 1of 11

DIGITAL ASSIGNMENT – 1

Problem solving with data structures


and algorithms
Name:Shaik Suhil Irshad
Reg.no: 18BEC2007

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;

void initialize_stack(stack *s, int size)


{
s->arr = (Type *)malloc(3 * size * sizeof(Type));
int top[3] = {-1,
2 * size,

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;
}

bool is_empty(const stack *s, int i)


{
i = i - 1;
if (s->pointer[i] == s->bottom[i])
{
return true;
}
return false;
}

bool is_full(const stack *s, int i)


{
i = i - 1;
if (s->pointer[i] == s->top[i])
{
return true;
}
return false;
}

void push(stack *s, int i, Type n)


{
if (is_full(s, i))
{
printf("Stack %d OverFlowed\n", i);
return;
}
i = i - 1;
if (i == 0)
{
s->arr[s->pointer[i]--] = n;
return;
}
s->arr[s->pointer[i]++] = n;
}
const char *get_string(stack *s, int pos)
{
char *value = malloc(20 * sizeof(char));
sprintf(value, FT, s->arr[pos]);
return value;
}
Type pop(stack *s, int i)
{
if (is_empty(s, i))
{
printf("Stack %d Underflowed\n", i);
exit(1);
}
i = i - 1;
Type temp = (Type)(s->arr[s->pointer[i]]);
if (i == 0)
{
return (Type)(s->arr[s->pointer[i]++]);
}
return (Type)(s->arr[s->pointer[i]--]);
}

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]);
}
}

void print_arr(stack *s)


{
int i;
printf("Current array state:\n");
for (i = 0; i < 3 * s->size; i++)
{
printf(FT, s->arr[i]);
}
printf("\n");
}

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:

Q2: Students of a Programming class arrive to submit assignments. Their register


numbers are stored in a LIFO list in the order in which the assignments are submitted.
Write a program using array to display the register number of the ten students who
submitted first. Register number of the ten students who submitted first will be at the
bottom of the LIFO list. Hence pop out the required number of elements from the top
so as to retrieve and display the first 10 students.

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;

char *pop(stack *s)


{
if (s->pointer == -1)
{
printf("Stack underflow\n");
exit(1);
}
return s->arr[(s->pointer)--];
}

void push(stack *s, char *reg)


{
if (s->pointer == s->size - 1)
{
printf("Stack Overflow\n");
exit(1);
}
char *temp = (char *)malloc(sizeof(char) * 9);
memcpy(temp, reg, sizeof(char) * 9);
s->arr[++(s->pointer)] = temp;
}

void initialize_stack(stack *s)


{
s->pointer = -1;
s->size = CLASS_STRENGTH;
s->arr = malloc(CLASS_STRENGTH * sizeof(char *));
int i = CLASS_STRENGTH - 1;
for (; i >= 0; i--)
{
push(s, submissions[i]);
}
}

bool is_stack_empty(stack *s)


{
return s->pointer == -1;
}

void print_stack(stack *s)


{
printf("Stack:\n");
int i;
for (int i = CLASS_STRENGTH - 1; i >= 0; i--)
{
printf("%s\n", s->arr[i]);
}
}

/**
* 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;

void init_queue(queue *q, int size)


{
q->arr = malloc(size * sizeof(char *));
q->back = size - 1;
q->size = size;
int i;
for (i = 9; i >= 0; i--)
{
q->arr[i] = "";
}
}

void enque(queue *q, char *reg_no)


{
if (q->back == -1)
{
deque(q);
}
char *temp = (char *)malloc(sizeof(char) * 9);
memcpy(temp, reg_no, sizeof(char) * 9);
q->arr[q->back--] = temp;
}

void deque(queue *q)


{
if (q->back == q->size - 1)
{
printf("Queue underflow\n");
exit(1);
}
int i;

for (i = 9; i > 0; i--)


{
q->arr[i] = q->arr[i - 1];
}
q->arr[i] = "";
q->back++;
}

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:

You might also like