DATASTRUCTURESLIP

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 66

SLIP 1

Q1.Implement a list library (doublylist.h) for a doubly linked list of integers with the create, display
operations. Write a menu driven program to call these operations. [10]

SOLUTION:

HEADER FILE-

#include<stdio.h>

#include<stdlib.h>

struct node

struct node *prev;

int data;

struct node *next;

};

struct node *f;

void create()

int n,i;

struct node *s;

printf("enter number of nodes needed : ");

scanf("%d",&n);

f=(struct node *)malloc(sizeof(struct node));

printf("enter data : ");

scanf("%d",&f->data);

f->prev=NULL;

s=f;

for(i=1;i<n;i++)

s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;

printf("enter data :");

scanf("%d",&s->data);

}
s->next= NULL;

void display()

struct node *s; for(s=f;s!

=NULL;s=s->next)

printf(" %d -> ",s->data);

MAIN PROGRAM-

#include <stdio.h>

#include "doublylist.h"

int main()

int ch;

do

printf("\n1.create\n2.display\n0.exit");

printf("enter choice :");

scanf("%d",&ch);

switch (ch)

case 1: create();

break;

case 2: display();

break;

case 0: break;

}while(ch!=0);

}
Q2. Write a program that sorts the elements of linked list using any of sorting technique. [20]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

};

struct node *f;

void create()

int i,n;

struct node *s; printf("\

nEnter no of nodes ");

scanf("%d",&n);

f=(struct node *)malloc(sizeof(struct node));

printf("\n Enter node ");

scanf("%d",&f->data);

s=f;

for(i=1;i<n;i++)

s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;

printf("\n Enter node ");

scanf("%d",&s->data);

s->next=NULL;

void display()

struct node *s;


for(s=f;s!=NULL;s=s->next)

printf("\t %d ->",s->data);

void sort()

struct node *p,*q;

int temp;

for(p=f;p!=NULL;p=p->next)

for(q=p->next;q!=NULL;q=q->next)

if(p->data > q->data)

temp = p->data;

p->data = q->data;

q->data = temp;

int main()

create();

printf("\n Link list is : ");

display();

printf("\n After sorting Link list is = ");

sort();

display();

}
SLIP 2

Q1. Implement a list library (singlylist.h) for a singly linked list of integer with the operations create,
display. Write a menu driven program to call these operations [10]

SOLUTION:

HEADER FILE-

struct node

int data;

struct node *next;

};

struct node *f;

void create()

int n,i;

struct node *s;

printf("enter number of nodes needed : ");

scanf("%d",&n);

f=(struct node *)malloc(sizeof(struct node));

printf("enter data : ");

scanf("%d",&f->data);

s=f;

for(i=1;i<n;i++)

s->next=(struct node *)malloc(sizeof(struct node)); s=s->next;

printf("enter data :");

scanf("%d",&s->data);

s->next= NULL;

}
void display()

struct node *s; for(s=f;s!

=NULL;s=s->next)

printf("%d ->",s->data);

MAIN PROGRAM-

#include <stdio.h>

#include "singlylist.h"

int main()

{ int ch;

do{

printf("\n1.create\n2.display\n3.exit");

printf("\nenter choice :");

scanf("%d",&ch);

switch (ch)

case 1: create();

break;

case 2: display();

break;

case 3: break;

}while(ch!=3);

Q2. Write a program that copies the contents of one stack into another. Use stack library to perform
basic stack operations. The order of two stacks must be identical.(Hint: Use a temporary stack to
preserve the order). [20]
SOLUTION:

#include <stdio.h>

char s[20];

int top;

void init()

top=-1;

int isempty(){

if(top==-1)

return 1;

else

return 0;

int isfull()

if(top==19)

return 1;

else

return 0;

void push(char ch)

if(isfull()==1)

printf("stack is full");

else

top++;

s[top]=ch;

}
char pop()

char ch;

if(isempty()==1)

printf("stack is empty");

else

ch=s[top];

top--;

return ch;

int main()

int i,k=0;

char temp[20];

init();

char str[20];

printf("enter string ");

scanf("%s",str);

for(i=0;str[i]!='\0';i++)

push(str[i]);

while(!isempty())

temp[k]=pop();

k++;

}
temp[k]='\0';

printf("second string :");

for(i=0;temp[i]!='\0';i++)

push(temp[i]);

while(!isempty())

printf("%c",pop());

}
SLIP 3

Q1. Sort a random array of n integers (accept the value of n from user) in ascending order by using
insertion sort algorithm. [10]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

int main()

int a[10],i,j,n,key;

printf("Enter how many numbers: ");

scanf("%d",&n);

for(i=0; i<n; i++)

a[i]=rand()%100;

printf("\n Before sorting array is ");

for(i=0;i<n;i++)

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

for(i=1; i<n; i++)

key = a[i];

for(j=i-1; j>=0; j--)


{

if(a[j] > key)

a[j+1]=a[j];

else

break;

a[j+1]=key;

printf("\nAfter sort array is: ");

for(i=0; i<n; i++)

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

Q2. Write a C program to evaluate postfix expression. [20]

SOLUTION:

#include<stdio.h>

#include<string.h>

char s[20];

int top;

void init()

top=-1;

int isempty()

{ if(top==-1)
return 1;

else return 0;

int isfull()

{ if(top==19)

return 1;

else

return 0;

void push(char data)

if(isfull()==1) printf("\

nStack is full "); else

{ top++;

s[top]=data;

char pop()

{ char data;

if(isempty()==1) printf("\

nStack is empty "); else

{ data=s[top];

top--;

return data;

void postfix_eval(char str[20])

{ int i,op1,op2,val;

for(i=0;str[i]!='\0';i++)
{ switch(str[i])

case '+': op2=pop();

op1=pop();

push(op1+op2); break;

case '-': op2=pop();

op1=pop();

push(op1-op2); break;

case '*': op2=pop();

op1=pop();

push(op1*op2); break;

case '/': op2=pop();

op1=pop();

push(op1/op2); break;

default:printf("Enter value of %c ",str[i]); scanf("%d",&val);

push(val);

printf("Ans =%d ",pop());

int main()

{ char str[20];

printf("Enter postfix string ");

scanf("%s",str);

postfix_eval(str);

}
SLIP 5

Q1. Create a random array of n integers. Accept a value x from user and use linear search algorithm
to check whether the number is present in the array or not and output the position if the number is
present. [10]

SOLUTION:

#include<stdio.h>

void linearsearch(int a[10],int n,int sr)

{ int i,p,cnt=0;

for(i=0;i<n;i++)

if(a[i]==sr)

{ p=i;

cnt++;

break;

if(cnt>=1)

printf("element found at %d position",p); else

printf("element NOT found ");

int main()

int n,i,sr,a[10];

printf("enter how many values");


scanf("%d",&n);

for(i=0;i<n;i++)

printf("enter values");

scanf("%d",&a[i]);

printf("\n enter search element");

scanf("%d",&sr);

linearsearch(a,n,sr);

Q2. Implement a priority queue library (PriorityQ.h) of integers using a static implementation of the
queue and implement the below two operations.

1) Add an element with its priority into the queue.

2) Delete an element from queue according to its priority. [20]

SOLUTION:

HEADER FILE-

#include<stdio.h>

int Q[20];

int f,R;

void init()

f=R=-1;

int isempty()

if(f==R)

return 1;

else

return 0;

}
int isfull()

if(R==19)

return 1;

else return 0;

void Add(int no)

{ int i;

if(isfull()==1)

printf("Queue is Full ");

else

for(i=R;i>f;i--)

{ if(no<Q[i])

Q[i+1]=Q[i];

else

break;

Q[i+1]=no;

R++;

int Delete()

{ int no;

if(isempty()==1)

printf("Queue is empty ");

else

{ f+

+;

no=Q[f];

}
return no;

void display()

int i;

for(i=f+1;i<=R;i++)

{ printf("%d ",Q[i]);

MAIN PROGRAM-

#include<stdio.h>

#include "PriorityQ.h"

int main()

int n,ch;

init();

do

printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit"); printf("\nEnter choice ");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter element "); scanf("%d",&n);

Add(n);

break;

case 2:if(isempty()==1) printf("\

nQueue is empty "); else

printf("deleted elemet =%d ",Delete());

break;

case 3:display();

break;
case 0:break;

}while(ch!=0);

SLIP 6

Q1. Sort a random array of n integers (accept the value of n from user) in ascending order by using
selection sort algorithm. [10]

SOLUTION:

#include<stdio.h>

#define size 5

void main()

int arr[size],i,j,temp,min;

printf("please enter element: \n");

for(i=0;i<=size-1;i++)

scanf("%d",&arr[i]);

for(i=0;i<=size-1;i++)

min=i;

for(j=0;j<=size-1;j++)

if(arr[min]<arr[j])
{

temp=arr[j];

arr[j]=arr[min];

arr[min]=temp;

printf("\n Entered Elements :");

for(i=0;i<=size-1;i++)

printf("\n %d",arr[i]);

Q2. Implement a queue library (dyqueue.h) of integers using a dynamic (linked list) implementation
of the queue and implement init, enqueue, dequeue, isempty, peek operations. [20]

SOLUTION:

HEADER FILE-

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *f,*r;

void init()

f=NULL;

r=NULL;

}
int isempty()

if(f==NULL)

return 1;

else

return 0;

void enqueue()

struct node*nw;

int n;

nw=(struct node*)malloc(sizeof(struct node));

nw->data=n;

nw->next=NULL;

if(f==NULL)

f=nw;

r=nw;

else

r->next=nw;

r=r->next;

int dequeue()

int n;

struct node *temp;

if(isempty()==1)

printf("queue is empty");
else

temp=f;

f=f->next;

n=temp->data;

free(temp);

int peek()

return f->data;

MAIN PORGRAM-

#include<stdio.h>

#include"dyqueue.h"

int main()

int ch,no;

init();

do

printf("\n1.enqueue \n2.dequeue \n3.peek \n0.exit"); printf("enter choice");

scanf("%d",&ch);

switch(ch)

case 1:printf("enter data:");

scanf("%d",&no);

enqueue(no);

break;

case 2:if(isempty()==1)

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


printf("dequeue element=%d",dequeue()); break;

case 3:printf("top element =%d",peek());

break;

case 0:break;

}while(ch!=0);

SLIP 7

Q1. Sort a random array of n integers (accept the value of n from user) in ascending order by using
quick sort algorithm. [10]

SOLUTION:

#include<stdio.h>

int main()

int n,array[1000],c,d,t;

printf("enter number of elements");

scanf("%d",&n);

printf("enter %d integers",n);

for(c=0;c<n;c++)

scanf("%d",&array[c]);

for(c=1;c<=n-1;c++)
{

d=c;

while(d>0&&array[d-1]>array[d])

t=array[d];

array[d]=array[d-1];

array[d-1]=t;

d--;

printf("sorted list in ascending order:\n");

for(c=0;c<=n-1;c++)

printf("%d\n",array[c]);

return 0;

Q2. Write a program that checks whether a string of characters is palindrome or not. The function
should use a stack library (cststack.h) of stack of characters using a static implementation of the
stack. [20]

SOLUTION:

HEADER FILE-

#define max 100

int top,stack[max];

void push(char x)

if(top == max-1){

printf("stack overflow");

} else { stack[+

+top]=x;
}

void pop()

printf("%c",stack[top--]);

MAIN PROGRAM-

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "cststack.h"

#define MAX 100

void main()

int i,front;

char s[MAX], b;

printf("Enter the String\n");

scanf("%s", s);

for (i = 0;s[i] != '\0';i++)

b = s[i];

push(b);

for (i = 0;i < (strlen(s) / 2);i++)

if (stack[top] == stack[front])

pop();

front++;

else
{

printf("%s is not a palindrome\n", s);

break;

if ((strlen(s) / 2) == front)

printf("%s is palindrome\n", s);

front = 0;

top = -1;

SLIP 8

Q1. Implement a list library (singlylist.h) for a singly linked list of integer With the operations create,
delete specific element and display. Write a menu driven program to call these operations
[10]

SOLUTION:

HEADER FILE-

void create();

void display();

void deletell();

struct node

int info;

struct node *next;

}*start=NULL;

void create()

int data;

struct node *q,*tmp;

printf("enter element");

scanf("%d",&data);
tmp=(struct node *)malloc(sizeof(struct node));

tmp->info=data;

if(start=NULL)

start=tmp;

else

q=start;

while(q->next!=NULL)

q=q->next;

q->next=tmp;

void display()

struct node *q;

if(start==NULL)

printf("list is empty");

else

printf("\n****elements in list*****\n");

q=start;

while(q!=NULL)

printf("%d\t",q->info);

q=q->next;

void deletell()
{

int data;

struct node *q,*tmp;

printf("enter elemnet to deleted");

scanf("%d",&data);

if(start->info==data)

tmp=start;

start=start->next;

free(tmp);

return;

q=start;

while(q->next->next!=NULL)

if(q->next=tmp->next)

tmp=q->next;

q- >next=tmp->next;

free(tmp);

return;

q=q->next;

if(q->next->info==data)

tmp=q->next;

q->next=NULL;

free(tmp);

return;

}
printf("elemnet not found");

MAIN PROGRAM-

#include<stdio.h>

#include<stdlib.h>

#include"singlylist.h"

void main()

int n;

printf("\n 1:create\n 2.display\n3:delete\n4:exit");

scanf("%d",&n);

while(1)

switch(n)

case 1: create();

break;

case 2: display();

break;

case 3: deletell();

display();

break;

case 4: exit(0);

Q2. Write a C program to check whether the contents of two stacks are identical. Use stack library to
perform basic stack operations. Neither stack should be changed. [20]

SOLUTION:
#include <stdio.h>

char s[20];

int top;

void init()

top=-1;

int isempty(){

if(top==-1)

return 1;

else

return 0;

int isfull()

if(top==19)

return 1;

else

return 0;

void push(char ch)

if(isfull()==1)

printf("stack is full");

else

top++;

s[top]=ch;

char pop()
{

char ch;

if(isempty()==1)

printf("stack is empty");

else

ch=s[top];

top--;

return ch;

int main()

int i,k=0;

char temp[20];

init();

char str[20];

printf("enter string ");

scanf("%s",str);

for(i=0;str[i]!='\0';i++)

push(str[i]);

while(!isempty())

temp[k]=pop();

k++;

temp[k]='\0';
printf("second string :");

for(i=0;temp[i]!='\0';i++)

push(temp[i]);

while(!isempty())

printf("%c",pop());

printf("second string :");

for(i=0;temp[i]!='\0';i++)

push(temp[i]);

while(!isempty())

printf("%c",pop());

}
SLIP 9

Q1. Write a program to convert an infix expression of the form (a*(b+c)*((da)/b)) into its equivalent
postfix notation. Consider usual precedence’s of operators. Use stack library of stack of characters
using static implementation. [10]

SOLUTION:

#include<stdio.h>

#include<ctype.h>

char stack[100];

int top = -1;

void push(char x)

stack[++top] = x;

}
char pop()

if(top == -1)

return -1;

else

return stack[top--];

int priority(char x)

if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

int main()

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

e = exp;

while(*e != '\0')

if(isalnum(*e))

printf("%c ",*e);

else if(*e == '(')

push(*e);
else if(*e == ')')

while((x = pop()) != '(')

printf("%c ", x);

else

while(priority(stack[top]) >= priority(*e))

printf("%c ",pop());

push(*e);

} e+

+;

while(top != -1)

printf("%c ",pop());

}return 0;

Q2 Read the data from the ‘employee.txt’ file and sort on age using Counting sort and Quick sort and
write the sorted data to another file 'sortedemponage.txt'. [20]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct employee

char name[20];

int age;

}emp[10];

int readFile(struct employee a[])


{

int i=0;

FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name,&a[i].age); i++;

return i-1;

void quicksort(struct employee a[10],int lb,int ub)

{ int i,j;

struct employee key,temp;

if(lb<ub)

i=lb+1;

key=a[lb];

j=ub;

while(i<=j)

while(a[i].age<=key.age && i<=ub)

i++;

while(a[j].age>key.age && j>=lb)

j--;

if(i<j)

temp=a[i];

a[i]=a[j];
a[j]=temp;

temp=a[j];

a[j]=a[lb];

a[lb]=temp;

quicksort(a,lb,j-1);

quicksort(a,j+1,ub);

void writeFile(struct employee a[],int n)

int i=0;

FILE *fp;

if((fp=fopen("sortedemp_quick_age.txt","w"))!=NULL) {

for(i=0;i<n;i++)

{ fprintf(fp,"%s %d\n",a[i].name,a[i].age); }

void main()

{ int n;

n=readFile(emp);

if(n==-1)

printf("File not found ");

else

quicksort(emp,0,n-1);

writeFile(emp,n);

printf("File Sorted ");


}

SLIP 10

Q1. Implement a linear queue library (st_queue.h) of integers using a static implementation of the
queue and implementing the init(Q), add(Q) and peek(Q) operations. Write a program that includes
queue library and calls different queue operations [10]

SOLUTION:

HEADER FILE-

#include<stdio.h>

int Q[20];

int f,R;

void init()
{ f=R=-1;

int isempty()

if(f==R)

return 1;

else

return 0;

int isfull()

if(R==19)

return 1;

else return 0;

void Add(int no)

if(isfull()==1)

printf("Queue is Full ");

else

{ R+

+;

Q[R]=no;

int Delete()

{ int no;

if(isempty()==1)

printf("Queue is empty ");

else

{
f++;

no=Q[f];

return no;

void display()

int i;

for(i=f+1;i<=R;i++)

{ printf("%d ",Q[i]);

MAIN PROGRAM-

#include<stdio.h>

#include "st_queue.h"

void main()

{ int n,ch;

init();

do

printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit"); printf("\nEnter choice ");

scanf("%d",&ch);

switch(ch)

{ case 1:printf("\nEnter element "); scanf("%d",&n);

Add(n);

break;

case 2:if(isempty()==1) printf("\

nQueue is empty "); else

printf("deleted elemet =%d ",Delete());

break;

case 3:display();
break;

case 0:break; default:printf("\

nInvalid choice "); }

}while(ch!=0);

Q2. Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp)
using bubble sort and selection sort. [20]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct employee

char name[20];

int age;

}emp[10];

int readfile(struct employee a[10])

int i=0;

FILE*fp;

if((fp=fopen("empl.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name ,&a[i].age); i++;

return i-1;

}
void writefile(struct employee a[],int n)

int i;

FILE *fp;

if((fp=fopen("bsort.txt","w"))!=NULL)

for(i=0;i<n;i++)

fprintf(fp,"%s %d \n",a[i].name ,a[i].age); }

void bubblesort(struct employee a[],int n)

int i,j;

struct employee temp;

for(i=0;i<n-1;i++)

for(j=0;j<n-1;j++)

if(strcmp(a[j].name,a[j+1].name)>0) {

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

void main()

int n;

n=readfile(emp);
if(n==-1)

printf("File is not found");

else

bubblesort(emp,n);

writefile(emp,n);

printf("File is found");

SLIP 11

Q1. Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name
of the city from user and use sentinel linear search algorithm to check whether the name is present
in the file and output the STD code, otherwise output “city not in the list”. [10]

SOLUTION:’

#include<stdio.h>
void sentinelsearch(int a[10],int n,int sr)

int i,cnt=0;

a[n]=sr;

while(sr!=a[i])

{ i++;

if(i<n)

printf("Element is found at %d position ",i); else

printf("element is not found ");

int main()

int n,i,sr,a[10];

printf("enter how many values");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("enter values");

scanf("%d",&a[i]);

printf("\n enter search element");

scanf("%d",&sr);

sentinelsearch(a,n,sr);

Q2. Implement a priority queue library (PriorityQ.h) of integers using a static implementation of the
queue and implementing the below two operations. Write a driver program that includes queue
library and calls different queue operations.

1) Add an element with its priority into the queue.


2) Delete an element from queue according to its priority. [20]

SOLUTION:

HEADER FILE-

#include<stdio.h>

int Q[20];

int f,R;

void init()

f=R=-1;

int isempty()

if(f==R)

return 1;

else

return 0;

int isfull()

if(R==19)

return 1;

else return 0;

void Add(int no)

{ int i;

if(isfull()==1)

printf("Queue is Full ");

else

for(i=R;i>f;i--)

{ if(no<Q[i])
Q[i+1]=Q[i];

else

break;

Q[i+1]=no;

R++;

int Delete()

{ int no;

if(isempty()==1)

printf("Queue is empty ");

else

{ f+

+;

no=Q[f];

return no;

void display()

int i;

for(i=f+1;i<=R;i++)

{ printf("%d ",Q[i]);

MAIN PROGRAM-

#include<stdio.h>

#include "PriorityQ.h"
int main()

int n,ch;

init();

do

printf("\n\n1.Add \n2.Delete \n3.Display \n0.EXit"); printf("\nEnter choice ");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter element "); scanf("%d",&n);

Add(n);

break;

case 2:if(isempty()==1) printf("\

nQueue is empty "); else

printf("deleted elemet =%d ",Delete());

break;

case 3:display();

break;

case 0:break;

}while(ch!=0);

SLIP 12

Q1. Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name
of the city from user and use linear search algorithm to check whether the name is present in the file
and output the STD code, otherwise output “city not in the list”. [10]

SOLUTION:
#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct city

char name[20];

int code;

}ct[10];

int readFile(struct city a[])

int i=0;

FILE *fp;

if((fp=fopen("city.txt","r"))!=NULL)

while(!feof(fp))

fscanf(fp,"%s%d",a[i].name,&a[i].code); i++;

return i-1;

void linearsearch(struct city a[10],int n,char sr[20])

{ int i,p,cnt=0;

for(i=0;i<n;i++)

if(strcmp(a[i].name,sr)==0)

{ p=i;

cnt++;

break;

}
}

if(cnt>=1)

printf("city is found and code is %d ",a[p].code); else

printf("city NOT found ");

void main()

{ int n;

char sr[20];

n=readFile(ct);

if(n==-1)

printf("File not found ");

else

printf("Enter city name to search ");

scanf("%s",sr);

linearsearch(ct,n,sr);

Q2.Implement a circular queue library (cir_queue.h) of integers using a dynamic (circular linked list)
implementation of the queue and implementing init(Q), AddQueue(Q) and DeleteQueue(Q)
operations. Write a menu driven program that includes queue library and calls different queue
operations. [20]

SOLUTION:

HEADER FILE-

#include<stdio.h>

#include<stdlib.h>

struct node

int data;
struct node *next;

};

struct node *r;

void init()

r=NULL;

int isempty()

if(r==NULL)

return 1;

else

return 0;

void Add(int n)

struct node *nw;

nw=(struct node *)malloc(sizeof(struct node));

nw->data=n;

if(r==NULL)

r=nw;

r- >next=r;

else

nw->next=r->next;

r->next=nw;

r=r->next;

}
int Delete()

int no;

struct node *temp;

temp=r->next;

if(r==temp->next)

{ r=NULL;

else

r- >next=temp->next;

no=temp->data;

free(temp);

return (no);

int peek()

return r->next->data;

MAIN PROGRAM-

#include<stdio.h>

#include "dyqueue.h"

int main()

{ int ch,no;

init();

do

printf("\n1.Add \n2.Delete \n0.Exit");


printf("Enter choice");

scanf("%d",&ch);

switch(ch)

case 1:printf("\n Enter element"); scanf("%d",&no);

Add(no);

break;

case 2:if(isempty()==1)

printf("\n Queue is empty"); else

printf("deleted element is %d",Delete());

break; }

case 0:break;

case 4:printf("Elemenent at peek %d ",peek()); break;

default:printf("Invalid choice"); }

}while(ch!=0);

SLIP 13
Q1. Implement a stack library (ststack.h) of integers using a static implementation of the stack and
implementing the operations like init(S), S=push(S) and S=pop(S). Write a driver program that
includes stack library and calls different stack operations. [10]

SOLUTION:

HEADER FILE-

#include<stdio.h>

char s[20];

int top;

void init()

top=-1;

int isempty()

{ if(top==-1)

return 1;

else return 0;

int isfull()

{ if(top==19)

return 1;

else

return 0;

void push(char data)

if(isfull()==1) printf("\

nStack is full "); else

{ top++;

s[top]=data;

}
char pop()

{ char data;

if(isempty()==1) printf("\

nStack is empty "); else

{ data=s[top];

top--;

return data;

int peek()

{ return s[top];

MAIN PROGRAM-

#include<stdio.h>

#include<stdlib.h>

#include"sstack.h"

int main()

int n,i=0,ch;

init();

do

printf("\n1.push \n2.pop \n3.chech stack is empty or not \n4.chech stack is full or not \n5.Peek
\n0.exit");

printf("\neneter your choice ");

scanf("%d",&ch);

switch(ch)

case 1: printf("enter elements");


scanf("%d",&n);

push(n);

break;

case 2:printf("\ndeleted elements :%d",pop()); break;

case 3:if(isempty()==1)

printf("stack is empty");

else

printf("stack is not empty");

break;

case 4:if(isfull()==1)

printf("stack is full");

else

printf("stack is not full");

break;

case 5:printf("\ntop of elements:%d",peek()); break;

case 0: break;

}while(ch!=0);

Q2. Write a program that sorts the elements of linked list using bubble sort technique. [20]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

};
struct node *f;

void create()

int i,n;

struct node *s; printf("\

nEnter no of nodes ");

scanf("%d",&n);

f=(struct node *)malloc(sizeof(struct node));

printf("\n Enter node ");

scanf("%d",&f->data);

s=f;

for(i=1;i<n;i++)

s- >next=(struct node *)malloc(sizeof(struct node)); s=s->next;

printf("\n Enter node ");

scanf("%d",&s->data);

s->next=NULL;

void display()

{ struct node *s; for(s=f;s!

=NULL;s=s->next)

{ printf("\t %d ->",s->data);

void sort()

{ struct node *p,*q;

int temp;

for(p=f;p!=NULL;p=p->next)
{

for(q=p->next;q!=NULL;q=q->next)

if(p->data > q->data)

{ temp = p->data;

p->data = q->data;

q->data = temp;

int main()

create();

printf("\n Link list is : ");

display();

printf("\n After sorting Link list is = ");

sort();

display();

}
SLIP 15

Q1. Sort a random array of n integers (accept the value of n from user) in ascending order by using
selection sort algorithm. [10]

SOLUTION:

#include<stdio.h>

#include<stdlib.h>

int main()

int i,a[10],n,min,pos,j,temp;

printf("Enter how many elements ");

scanf("%d",&n);

for(i=0;i<n;i++)

a[i]=rand()%100;

printf("\nBefore array sorting ");

for(i=0;i<n;i++)

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

for(j=0;j<n-1;j++)

min=a[j];

pos=j;for(i=j+1;i<n;i++)

if(a[i]<=min)

min=a[i];

pos=i;
}

temp=a[j];

a[j]=min;

a[pos]=temp;

printf("\nSorted array is ");

for(i=0;i<n;i++)

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

Q2. Implement a linear queue library (dyqueue.h) of integers using a dynamic (circular linked list)
implementation of the queue and implementing the five queue operations (init(Q), AddQueue(Q, x),
X=DeleteQueue(Q), X=peek(Q), isEmpty(Q)). [20] Write a
program to reverse the elements of a queue using queue library.

SOLUTION:

HEADER FILE-

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *r;

void init()

r=NULL;

}
int isempty()

if(r==NULL)

return 1;

else

return 0;

void Add(int n)

struct node *nw;

nw=(struct node *)malloc(sizeof(struct node));

nw->data=n;

if(r==NULL)

r=nw;

r->next=r;

else

nw->next=r->next;

r->next=nw;

r=r->next;

int Delete()

int no;

struct node *temp;

temp=r->next;

if(r==temp->next)

{ r=NULL;
}

else

r- >next=temp->next;

no=temp->data;

free(temp);

return (no);

int peek()

return r->next->data;

MAIN PROGRAM-

#include<stdio.h>

#include "dyqueue.h"

int main()

{ int ch,no;

init();

do

printf("\n1.Add \n2.Delete \n0.Exit");

printf("Enter choice");

scanf("%d",&ch);

switch(ch)

case 1:printf("\n Enter element"); scanf("%d",&no);

Add(no);

break;

case 2:if(isempty()==1)

printf("\n Queue is empty"); else


{

printf("deleted element is %d",Delete());

break; }

case 0:break;

case 4:printf("Elemenent at peek %d ",peek()); break;

default:printf("Invalid choice"); }

}while(ch!=0);

}
SLIP 17

Q1 Implement a list library (singlylist.h) for a singly linked list. Create a linked list, reverse it and
display reversed linked list. [10]

SOLUTION:

HEADER FILE-

#include <stdio.h>

#include <stdlib.h>

struct node

int data;

struct node *next;

};

struct node *f;

void create()

struct node *s;

int n,i;

printf("Enter how many nodes");

scanf("%d",&n);

f=(struct node *)malloc(sizeof(struct node));

printf("Enter data");

scanf("%d",&f->data);

s=f;

for(i=1;i<n;i++)

{
s- >next=(struct node *)malloc(sizeof(struct node)); s=s->next;

printf("Enter data");

scanf("%d",&s->data);

s->next=NULL;

void display()

struct node *s; for(s=f;s!

=NULL;s=s->next)

printf("%d ->",s->data);

void reverse()

int cnt=0,i;

struct node *s;

for(s=f;s!=NULL;s=s->next)

cnt++;

while(cnt>0)

for(i=1,s=f;i<cnt;i++)

s=s->next;

printf("%d ->",s->data);

cnt--;

}
}

MAIN PROGRAM-

#include<stdio.h>

#include"singlylist.h"

int main()

create();

display();

reverse();

Q2 Write a program that copies the contents of one stack into another. Use stack library to perform
basic stack operations. The order of two stacks must be identical.(Hint: Use a temporary stack to
preserve the order). [20]

SOLUTION:

#include <stdio.h>

char s[20];

int top;

void init()

top=-1;

int isempty(){

if(top==-1)

return 1;

else

return 0;

int isfull()

if(top==19)
return 1;

else

return 0;

void push(char ch)

if(isfull()==1)

printf("stack is full");

else

top++;

s[top]=ch;

char pop()

char ch;

if(isempty()==1)

printf("stack is empty");

else

ch=s[top];

top--;

return ch;

int main()

int i,k=0;

char temp[20];

init();
char str[20];

printf("enter string ");

scanf("%s",str);

for(i=0;str[i]!='\0';i++)

push(str[i]);

while(!isempty())

temp[k]=pop();

k++;

temp[k]='\0';

printf("second string :");

for(i=0;temp[i]!='\0';i++)

push(temp[i]);

while(!isempty())

printf("%c",pop());

You might also like