Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 41

Name:- Shobhit Yadav

ID:- 2019UCP1403

Q1)

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int key;

struct node* next;

}node;

node* head = NULL;

node* tail = NULL;

void addIt(){

node *temp;

temp = (node*)malloc(sizeof(node));

printf("Please enter the data : ");

scanf("%d",&temp->key);

temp->next = NULL;

if(head == NULL){

head = temp;

tail = temp;

}
else{

tail->next = temp;

tail = tail->next;

void searchIn(){

int k,p = 0;

node* temp;

printf("Please enter the element to be searched : ");

scanf("%d",&k);

temp = head;

if(head == NULL){

printf("List is empty\n");

return;

while(temp != NULL){

if(temp->key == k){

p = 1;

break;

temp = temp->next;

if(p){

printf("Element found\n");
}

else{

printf("Element not found\n");

void deletefunc(){

int k,t=0;

node* p;

node* q = NULL;

printf("Please enter the key to be deleted : ");

scanf("%d",&k);

p = head;

if(head == NULL){

printf("List is empty\n");

return;

while(p != NULL){

if(p->key == k){

t = 1;

break;

q = p;

p = p->next;

}
if(t){

if(q != NULL){

q->next = p->next;

else{

head = NULL;

tail = NULL;

if(p->next == NULL){

tail = q;

p->next = NULL;

printf("Element deleted\n");

else{

printf("Element with key not found\n");

int main(){

int c = 0;

printf("1.) Append an element\n2.) Delete an element\n3.) Find an element\n4.) Exit\n");

while(1){
printf("Please enter your choice : ");

scanf("%d",&c);

switch(c){

case 1:

addIt();

break;

case 2:

deletefunc();

break;

case 3:

searchIn();

break;

default:

exit(0);

return 0;

}
Q2)

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int key;

struct node* next;

}node;

node* head = NULL;

node* tail = NULL;

node* listHead = NULL;


void findTheElement(){

int k;

node *p,*temp,*q;

printf("Please enter the key : ");

scanf("%d",&k);

if(head == NULL){

printf("List is empty\n");

else{

p = head;

while(p != NULL){

if(p->key == k){

temp = (node*)malloc(sizeof(node));

temp->key = k;

q = listHead;

listHead = temp;

listHead->next = q;

p = p->next;

void addElementAtBack(int k){


node *temp;

temp = (node*)malloc(sizeof(node));

temp->key = k;

temp->next = NULL;

if(head == NULL){

head = temp;

tail = temp;

else{

tail->next = temp;

tail = tail->next;

int main(){

int c = 0,k;

printf("1.) Add in main list\n2.) Find in main list\n3.) Exit\n");

while(1){

printf("Please enter the choice : ");

scanf("%d",&c);

switch(c){

case 1:

printf("Please enter the data : ");

scanf("%d",&k);

addElementAtBack(k);
break;

case 2:

findTheElement();

if(listHead == NULL){

printf("Element not found\n");

else{

while(listHead != NULL){

printf("%d ",listHead->key);

listHead = listHead->next;

listHead = NULL;

printf("\n");

break;

default:

exit(0);

return 0;

}
Q3)

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int key;

struct node *r;

struct node *l;

}node;

node *head = NULL;

node *tail = NULL;

void addElement(){
node *temp;

temp = (node*)malloc(sizeof(node));

printf("Please enter the key : ");

scanf("%d",&temp->key);

temp->l = NULL;

temp->r == NULL;

if(head == NULL){

head = temp;

tail = temp;

else{

tail->r = temp;

temp->l = tail;

tail = tail->r;

void revSeq(){

node *p,*q;

int l = 0,r = 0,c = 0,i = 0;

printf("Please enter the left and right index of the sub sequence till you want to reverse : ");

scanf("%d %d",&l,&r);

if(head == NULL || head == tail){

printf("Not possible\n");

return;
}

p = q = head;

while(q != NULL && c != r){

if(i < l){

p = p->r;

i++;

if(c < r){

q = q->r;

c++;

if(c < r){

printf("Not possible\n");

return;

c = (c+i)/2;

while(i <= c){

int temp = p->key;

p->key = q->key;

q->key = temp;

p = p->r;

q = q->l;

i++;

}
}

void showList(){

node *p;

p = head;

if(head == NULL){

printf("List is empty");

return;

while(p != tail){

printf("%d ",p->key);

p = p->r;

printf("%d\n",tail->key);

int main(){

int c;

printf("1.) Add element to list\n2.) Reverse sequence\n3.) Show list\n4.) Exit\n");

while(1){

printf("Please enter the choice : ");

scanf("%d",&c);

switch(c){

case 1:

addElement();
break;

case 2:

revSeq();

break;

case 3:

showList();

break;

default:

exit(0);

return 0;

}
Q4)

a)

#include <stdio.h>

#define SIZE 7

int S[SIZE+1];

int top = 0;

int is_empty() {

if(top == 0)

return 1;

return 0;

void push(int x) {

top = top+1;

if(top > SIZE) {

printf("Stack Overflow\n");

else {

S[top] = x;

}
int pop() {

if(is_empty()) {

printf("Stack Underflow\n");

return -1000;

else {

top = top-1;

return S[top+1];

int main() {

push(10);

push(20);

push(30);

push(40);

push(50);

push(60);

push(70);

int i;

printf(" Original Stack:-\n");


for(i=1; i<=SIZE; i++) {

printf("%d ",S[i]);

printf("\n");

pop();

pop();

printf("After Two POP Operations. Our Stack becomes:-\n");

for(i = 1 ; i<=SIZE-2 ; i++ )

printf("%d " , S[i]);

printf("\n");

push(200);

printf("After One PUSH Operations. Our Stack becomes:-\n");

for(i = 1 ; i<=SIZE-1 ; i++ )

printf("%d " , S[i]);

printf("\n");

return 0;
}

b)

#include <stdio.h>

#include <stdlib.h>

typedef struct queue {

int head;

int tail;

int size;

int Q[];

}queue;

queue* new_queue(int size) {

queue *q = (queue*)(malloc(sizeof(queue) + size*sizeof(int)));

q->head = 1;
q->tail = 1;

q->size = size;

return q;

int is_empty(queue *q) {

if(q->tail == q->head)

return 1;

return 0;

int is_full(queue *q) {

if(q->head == q->tail+1)

return 1;

return 0;

void enqueue(queue *q, int x) {

if(is_full(q)) {

printf("Queue Overflow\n");

else {

q->Q[q->tail] = x;

if(q->tail == q->size)

q->tail = 1;

else

q->tail = q->tail+1;
}

int dequeue(queue *q) {

if(is_empty(q)) {

printf("Underflow\n");

return -1000;

else {

int x = q->Q[q->head];

if(q->head == q->size) {

q->head = 1;

else {

q->head = q->head+1;

return x;

void display(queue *q) {

int i;

for(i=q->head; i<q->tail; i++) {

printf("%d ",q->Q[i]);

if(i == q->size) {

i = 0;

}
}

int main() {

queue *q = new_queue(10);

enqueue(q, 10);

enqueue(q, 20);

enqueue(q, 30);

enqueue(q, 40);

enqueue(q, 50);

printf("Original Queue:-\n");

display(q);

printf("\n");

dequeue(q);

dequeue(q);

printf("After Two Dequeue Operations. Our Queue becomes:-\n");

display(q);

printf("\n");

enqueue(q, 60);

printf("After One Enqueue Operations. Our Queue becomes:-\n");

display(q);

printf("\n");

return 0;
}

c)

#include <stdio.h>

#define SIZE 7

int S_1[SIZE] , S_2[SIZE];

int top_1 = 0 , top_2 = 0 , top_3 = 0;

int Q[SIZE];

int is_empty(int top) {

if(top == 0)

return 1;

return 0;
}

void push(int x) {

top_1 = top_1+1;

if(top_1 > SIZE) {

printf("Stack Overflow\n");

else {

S_1[top_1] = x;

int pop() {

if(is_empty(top_1)) {

printf("Stack Underflow\n");

return -1000;

else {

top_1 = top_1-1;

S_2[top_3] = S_1[top_1+1];

top_3 = top_3+1;

return S_1[top_1+1];

int main() {
push(10);

push(20);

push(30);

push(40);

push(50);

push(60);

push(70);

int i;

printf("Original Queue:-\n");

for(i=1; i<=SIZE; i++) {

printf("%d ",S_1[i]);

printf("\n");

pop();

pop();

printf("After Two DEQUEUE Operations. Our Queue becomes:-\n");

for(i = top_3+1 ; i<=SIZE ; i++ )

printf("%d " , S_1[i]);

printf("\n");

push(200);
printf("After One ENQUEUE Operations. Our Queue becomes:-\n");

for(i = top_3+1 ; i<SIZE-1 ; i++ )

printf("%d " , S_1[i]);

for( i = top_3-1 ; i>=0 ; i--){

printf("%d " , S_2[i]);

printf("%d" , S_1[SIZE-1]);

printf("\n");

return 0;

}
Q5)

a)

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int key;

struct node* next;

}node;

node* head = NULL;

void push(){

node* temp;

temp = (node*)malloc(sizeof(node));

printf("Please enter the data : ");

scanf("%d",&temp->key);

temp->next = NULL;

if(head == NULL){

head = temp;

else{

node* p;

p = head;

head = temp;

head->next = p;

p = NULL;
free(p);

void pop(){

if(head == NULL){

printf("Stack is empty\n");

else{

node* p;

p = head;

head = p->next;

printf("%d is popped from the stack\n",p->key);

p->next = NULL;

free(p);

int top(){

if(head == NULL){

printf("Stack is empty\n");

return -1;

else{

return head->key;

}
int main(){

int c;

printf("1.) Push an element in stack\n2.) Pop an element from stack\n3.) Top element of stack\n4.)
Exit\n");

while(1){

printf("Please enter the choice : ");

scanf("%d",&c);

switch(c){

case 1:

push();

break;

case 2:

pop();

break;

case 3:

c = top();

if(c != -1){

printf("%d is the top element\n",c);

break;

default :

exit(0);

return 0;

}
b)

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int key;

struct node* next;

}node;

node* head = NULL;

node* tail = NULL;

void enqueue(){

node* temp;

temp = (node*)malloc(sizeof(node));

printf("Please enter the data : ");


scanf("%d",&temp->key);

temp->next = NULL;

if(head == NULL){

head = temp;

tail = temp;

else{

tail->next = temp;

tail = tail->next;

int dequeue(){

int k;

if(head == NULL){

printf("The queue is empty\n");

return -1;

node* p;

p = head;

head = p->next;

k = p->key;

p->next = NULL;

free(p);

return k;

int top(){
if(head == NULL){

printf("Queue is empty\n");

return -1;

return head->key;

int length(){

int c = 0;

node* p;

if(head == NULL){

return 0;

p = head;

while(p != NULL){

c++;

p = p->next;

return c;

int main(){

int c,k;

printf("1.) Add element to queue\n2.) Delete element from queue\n3.) Length of queue\n4.) Top
element of queue\n5.) Exit\n ");

while(1){

printf("Please enter the choice : ");

scanf("%d",&c);
switch(c){

case 1:

enqueue();

break;

case 2:

k = dequeue();

if(k != -1){

printf("%d is the removed element\n",k);

break;

case 3:

k = length();

printf("%d is the length of queue\n",k);

break;

case 4:

k = top();

if(k != -1){

printf("%d is the top element\n",k);

break;

default:

exit(0);

return 0;

}
Q6)

a)

#include <stdio.h>

#include<stdlib.h>

#define CAPACITY 10

int stack1[CAPACITY];

int stack2[CAPACITY];

int stack3[CAPACITY];

int top1 = 0;

int top2 = 0;

int top3 = 0;

void push(int x,int stack[],int *top){

if(*top != CAPACITY){
stack[*top] = x;

*top = *top + 1;

else{

printf("Stack is full\n");

int pop(int stack[],int *top){

int e = -32547;

if(*top == 0){

printf("Stack is empty\n");

return e;

else{

e = stack[*top-1];

*top -= 1;

return e;

void exchange(){

int t,b,c;

printf("Please enter the indices to be exchanged : ");

scanf("%d %d",&t,&b);

c = b;

while(c){

push(pop(stack1,&top1),stack2,&top2);

c--;
}

c = b - t;

while(c){

push(pop(stack2,&top2),stack3,&top3);

c--;

push(pop(stack2,&top2),stack1,&top1);

c = b - t - 1;

while(c){

push(pop(stack3,&top3),stack2,&top2);

c--;

c = b - t - 1;

while(c){

push(pop(stack2,&top2),stack1,&top1);

c--;

push(pop(stack3,&top3),stack1,&top1);

c = t - 1;

while(c){

push(pop(stack2,&top2),stack1,&top1);

c--;

void traverse(){

for(int i = top1 - 1;i >= 0;i--){

printf("%d ",stack1[i]);

}
printf("\n");

int main(){

int c;

printf("1.) Push in stack\n2.) Exchange stack\n3.) Print Stack\n4.) Exit\n");

while(1){

printf("Please enter the choice : ");

scanf("%d",&c);

switch(c){

case 1:

printf("Please enter the data : ");

scanf("%d",&c);

push(c,stack1,&top1);

break;

case 2:

exchange();

break;

case 3:

traverse();

break;

default:

exit(0);

return 0;

}
b)

#include <stdio.h>

#include<stdlib.h>

#define CAPACITY 10

int stack1[CAPACITY];

int stack2[CAPACITY];

int stack3[CAPACITY];

int top1 = 0;

int top2 = 0;

int top3 = 0;

void push(int x,int stack[],int *top){

if(*top != CAPACITY){

stack[*top] = x;
*top = *top + 1;

else{

printf("Stack is full\n");

int pop(int stack[],int *top){

int e = -32547;

if(*top == 0){

printf("Stack is empty\n");

return e;

else{

e = stack[*top-1];

*top -= 1;

return e;

void revSubStack(){

int t,b,c;

printf("Please enter the range of the reversal : ");

scanf("%d %d",&t,&b);

c = b;

while(c){

push(pop(stack1,&top1),stack2,&top2);

c--;

}
c = b - t + 1;

while(c){

push(pop(stack2,&top2),stack3,&top3);

c--;

c = b - t + 1;

while(c){

push(pop(stack3,&top3),stack1,&top1);

c--;

c = t - 1;

while(c){

push(pop(stack2,&top2),stack1,&top1);

c--;

void traverse(){

for(int i = top1 - 1;i >= 0;i--){

printf("%d ",stack1[i]);

printf("\n");

int main(){

int c;

printf("1.) Push in stack\n2.) Reverse stack\n3.) Print Stack\n4.) Exit\n");


while(1){

printf("Please enter the choice : ");

scanf("%d",&c);

switch(c){

case 1:

printf("Please enter the data : ");

scanf("%d",&c);

push(c,stack1,&top1);

break;

case 2:

revSubStack();

break;

case 3:

traverse();

break;

default:

exit(0);

return 0;

You might also like