2019UCP1403 DSA Lab Assignment
2019UCP1403 DSA Lab Assignment
2019UCP1403 DSA Lab Assignment
ID:- 2019UCP1403
Q1)
#include <stdio.h>
#include <stdlib.h>
int key;
}node;
void addIt(){
node *temp;
temp = (node*)malloc(sizeof(node));
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;
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{
void deletefunc(){
int k,t=0;
node* p;
node* q = NULL;
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{
int main(){
int c = 0;
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>
int key;
}node;
int k;
node *p,*temp,*q;
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;
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;
while(1){
scanf("%d",&c);
switch(c){
case 1:
scanf("%d",&k);
addElementAtBack(k);
break;
case 2:
findTheElement();
if(listHead == NULL){
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>
int key;
}node;
void addElement(){
node *temp;
temp = (node*)malloc(sizeof(node));
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;
printf("Please enter the left and right index of the sub sequence till you want to reverse : ");
scanf("%d %d",&l,&r);
printf("Not possible\n");
return;
}
p = q = head;
p = p->r;
i++;
q = q->r;
c++;
printf("Not possible\n");
return;
c = (c+i)/2;
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;
while(1){
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;
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("%d ",S[i]);
printf("\n");
pop();
pop();
printf("\n");
push(200);
printf("\n");
return 0;
}
b)
#include <stdio.h>
#include <stdlib.h>
int head;
int tail;
int size;
int Q[];
}queue;
q->head = 1;
q->tail = 1;
q->size = size;
return q;
if(q->tail == q->head)
return 1;
return 0;
if(q->head == q->tail+1)
return 1;
return 0;
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;
}
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;
int 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);
display(q);
printf("\n");
enqueue(q, 60);
display(q);
printf("\n");
return 0;
}
c)
#include <stdio.h>
#define SIZE 7
int Q[SIZE];
if(top == 0)
return 1;
return 0;
}
void push(int x) {
top_1 = top_1+1;
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");
printf("%d ",S_1[i]);
printf("\n");
pop();
pop();
printf("\n");
push(200);
printf("After One ENQUEUE Operations. Our Queue becomes:-\n");
printf("%d" , S_1[SIZE-1]);
printf("\n");
return 0;
}
Q5)
a)
#include <stdio.h>
#include <stdlib.h>
int key;
}node;
void push(){
node* temp;
temp = (node*)malloc(sizeof(node));
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;
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){
scanf("%d",&c);
switch(c){
case 1:
push();
break;
case 2:
pop();
break;
case 3:
c = top();
if(c != -1){
break;
default :
exit(0);
return 0;
}
b)
#include <stdio.h>
#include <stdlib.h>
int key;
}node;
void enqueue(){
node* temp;
temp = (node*)malloc(sizeof(node));
temp->next = NULL;
if(head == NULL){
head = temp;
tail = temp;
else{
tail->next = temp;
tail = tail->next;
int dequeue(){
int k;
if(head == NULL){
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){
scanf("%d",&c);
switch(c){
case 1:
enqueue();
break;
case 2:
k = dequeue();
if(k != -1){
break;
case 3:
k = length();
break;
case 4:
k = top();
if(k != -1){
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;
if(*top != CAPACITY){
stack[*top] = x;
*top = *top + 1;
else{
printf("Stack is full\n");
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;
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(){
printf("%d ",stack1[i]);
}
printf("\n");
int main(){
int c;
while(1){
scanf("%d",&c);
switch(c){
case 1:
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;
if(*top != CAPACITY){
stack[*top] = x;
*top = *top + 1;
else{
printf("Stack is full\n");
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;
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(){
printf("%d ",stack1[i]);
printf("\n");
int main(){
int c;
scanf("%d",&c);
switch(c){
case 1:
scanf("%d",&c);
push(c,stack1,&top1);
break;
case 2:
revSubStack();
break;
case 3:
traverse();
break;
default:
exit(0);
return 0;