You are on page 1of 6

Assignment 1

Name: Rahul Kumar

Q1. Implementation of fixed size STACK data structure in C using array. The stack
should have push, pop, create, isempty, isfull functions.

#include <stdio.h>
#include <stdlib.h>

#define SIZE 15

struct Stack {
int items[SIZE];
int top;
};

struct Stack* createStack();


void push(struct Stack* stack, int item);
int pop(struct Stack* stack);
int is_Empty(struct Stack* stack);
int is_Full(struct Stack* stack);

struct Stack* createStack() {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = -1;
return stack;
}

void push(struct Stack* stack, int item) {


if (is_Full(stack)) {
printf("Stack is full\n");
} else {
stack->top++;
stack->items[stack->top] = item;
}
}

int pop(struct Stack* stack) {


int item;
if (is_Empty(stack)) {
printf("Stack is empty\n");
return -1;
} else {
item = stack->items[stack->top];
stack->top--;
return item;
}
}

int is_Empty(struct Stack* stack) {


return stack->top == -1;
}

// check if the stack is full


int is_Full(struct Stack* stack) {
return stack->top == SIZE - 1;
}

int main() {
struct Stack* stack = createStack();

push(stack, 1);
push(stack, 2);
push(stack, 3);
push(stack, 4);
push(stack, 5);
push(stack, 6);
push(stack, 7);
push(stack, 8);

printf("Element popped: %d\n", pop(stack));


printf("Element popped: %d\n", pop(stack));

return 0;
}

Q2. Implementation of dynamic size STACK data structure in C using linked list. The
stack should have push, pop, create, isempty, isfull functions.

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};
struct Stack {
struct Node* top;
};

struct Stack* createStack();


void push(struct Stack* stack, int data);
int pop(struct Stack* stack);
int is_Empty(struct Stack* stack);
int is_Full(struct Stack* stack);

struct Stack* createStack() {


struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->top = NULL;
return stack;
}

void push(struct Stack* stack, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;
newNode->next = stack->top;
stack->top = newNode;
}

int pop(struct Stack* stack) {


if (is_Empty(stack)) {
printf("Stack is empty\n");
return -1;
} else {
struct Node* temp = stack->top;
int data = temp->data;
stack->top = temp->next;
free(temp);
return data;
}
}

int is_Empty(struct Stack* stack) {


return stack->top == NULL;
}
int is_Full(struct Stack* stack) {

// Linked list implementation does not have a fixed size

return 0;

int main() {
struct Stack* stack = createStack();

push(stack, 1);
push(stack, 2);
push(stack, 3);
push(stack, 4);
push(stack, 5);
push(stack, 6);
push(stack, 7);

printf("Element popped: %d\n", pop(stack));


printf("Element popped: %d\n", pop(stack));

return 0;
}

Q3. Implementation of QUEUE data structure in C using linked list. It should have
enqueue, dequeue, isempty and size functions.

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Queue {
struct Node* front;
struct Node* back;
int size;
};

struct Queue* createQueue();


void enqueue(struct Queue* queue, int data);
int dequeue(struct Queue* queue);
int is_Empty(struct Queue* queue);
int size(struct Queue* queue);

struct Queue* createQueue() {


struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->front = NULL;
queue->back = NULL;
queue->size = 0;
return queue;
}

void enqueue(struct Queue* queue, int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (is_Empty(queue)) {
queue->front = newNode;
} else {
queue->back->next = newNode;
}
queue->back = newNode;
queue->size++;
}

int dequeue(struct Queue* queue) {


if (is_Empty(queue)) {
printf("Queue is empty\n");
return -1;
} else {
struct Node* temp = queue->front;
int data = temp->data;
queue->front = temp->next;
free(temp);
queue->size--;
return data;
}
}

int is_Empty(struct Queue* queue) {


return queue->size == 0;
}
int size(struct Queue* queue) {
return queue->size;
}

// main function to test the queue


int main() {
struct Queue* queue = createQueue();

enqueue(queue, 1);
enqueue(queue, 2);
enqueue(queue, 3);
enqueue(queue, 4);
enqueue(queue, 5);
enqueue(queue, 6);
enqueue(queue, 7);

printf("Element dequeued: %d\n", dequeue(queue));


printf("Element dequeued: %d\n", dequeue(queue));

return 0;
}

You might also like