0% found this document useful (0 votes)
40 views

DS - 25 Marks Slip - 1

Uploaded by

aryaganeshmhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

DS - 25 Marks Slip - 1

Uploaded by

aryaganeshmhaske
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

DS_25Marks

Slip1_2 : carete singly link list i) create ii) display iii)insert first position iv)
Delete node at any position

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

typedef struct node


{
int data;
struct node *next;
}NODE;
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);

}
t->next=NULL;
}

void display()
{
struct node *t;
Page 1
for(t=head;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}

void insert_last()
{
struct node *nw,*t;
nw=(NODE*)malloc(sizeof(NODE));
printf("\n Enter node for inserting: ");
scanf("%d",&nw->data);
for(t=head;t->next!=NULL;t=t->next);
t->next=nw;
nw->next=NULL;

void delete()
{
struct node *t,*temp;
int cnt=0,i,p;
printf("\n Enter position for delete Data : ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t->next)
cnt++;
if(p>cnt)
{
printf("\n Invalid position ");
}
else
{
if(p==1)
{
temp=head;
head=head->next;
free(temp);
}
else if(cnt==p)
Page 2
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next=NULL;
free(temp);
}
else
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next = temp->next;
free(temp);
}
}
}

main()
{
int ch;
clrscr();

do
{
printf("\n MENU \n 1.Create \n 2.Display \n 3.Add at last position \n
4.Delete node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_last();
break;

case 4: delete();
Page 3
break;

Page 4
case 5:break;

}
}
while(ch!=5);
getch();
}

Slip2_1 : Dynamic implemenataion of stack

#include<conio.h>
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *top;

void init()
{
top=NULL;
}

int isEmpty()
{
if(top==NULL)
return 1;
else
return 0;
}

void push(int n)
{
struct node *nw;
nw = (struct node*)malloc(sizeof(struct node));
nw->data = n;
nw->next=top;
top=nw;
Page 5
}

int pop()
{
struct node *temp;
temp=top;
if(isEmpty())
printf("Stack is empty");
else
{
int n;
n=top->data;
top=top->next;
free(temp);
return n;
}
}

void display()
{
struct node *t;
for(t=top;t!=NULL;t=t->next)
printf("%d\t",t->data);
}

main()
{
int ch,n;
init();

do
{
printf("\n 1.push \n 2.pop \n 3.display \n 4.exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);

switch(ch)
{
Page 6
case 1:printf("\n Enter data ");

Page 7
scanf("%d",&n);
push(n);
break;
case 2: printf("\n TOS is : ");
printf("\t %d",pop());
break;
case 3: printf("\n Stack elements are : ");
display();
break;
case 4:break;
}
}while(ch!=4);
}

Slip3_1 :Write a ‘C’ program to create linked list with given number in which
data part of each node contains individual digit of the number.
(Ex. Suppose the number is 584 then the nodes of linked list should contain
5, 8, 4.)

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

typedef struct node


{
int data;
struct node *next;
}NODE;
NODE *head=NULL;

void create(int no)


{
NODE *s;

if(head==NULL)
{ head=(NODE *)malloc(sizeof(NODE));
head->data=no;
head->next=NULL;
Page 8
}
else
{
for(s=head;s->next!=NULL;s=s->next);
s->next=(NODE*)malloc(sizeof(NODE));
s=s->next;
s->data=no;
s- >next=NULL;
}
}

void display()
{
NODE *s;
for(s=head;s!=NULL;s=s->next)
{ printf("\t %d -> ",s->data);
}
}

void main()
{
int no,k;
int r=0;

printf("\nEnter The Number ");


scanf("%d", &no);

while(no>0)
{ k=no%10;
r=r*10+k;
no=no/10;
}

while(r>0)
{
k=r%10;
create(k);
r=r/10;
}
Page 9
printf("\n link list is :");
display();
getch();
}

Slip 4_2 : static implementation of circular queue i)Insert ii)Delete iii)display


iv)Exit

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define SIZE 20

int Q[SIZE];
int F,R;
void init()
{
F=R=-1;
}

int CQEmpty()
{
if(F==R)
return 1;
else
return 0;
}

int CQFull()
{
if(((R+1)%SIZE == F) || (F==-1 && R==SIZE-1))
return 1;
else
return 0;
}

void AddQ(int n)
Page 10
{

Page 11
if(CQFull())
printf("\n Queue is Full");
else
{
R=(R+1)%SIZE;
Q[R]=n;
}
}

int DelQ()
{
if(CQEmpty())
{ printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
int n;
F=(F+1)%SIZE;
n=Q[F];
return n;
}
}

void display()
{
int i=F+1;
do
{
printf("%d \t",Q[i]);
i=(i+1)%SIZE;
}while(i!=(R+1)%SIZE);
}

void main()
{
int ch,no,i;
clrscr();
Page 12
init();
do

Page 13
{
printf("\nMenu \n1.Add \n2.Delete \n3.Display\n4.Exit");

printf("\nEnter the Choice");


scanf("%d",&ch);

switch(ch)
{
case 1: printf("\n Enter elements : ");
scanf("%d",&no);
AddQ(no);
break;
case 2: no=DelQ();
if(no!=-1)
printf("\n deleted data is : %d ",no);
break;
case 3: printf("\n Elements are : ");
display();
break;
case 4: exit(0);
default: printf("\n Invalid choice");
}
}while(ch!=4);
getch();
}

Slip5_2 : Addition of two Polynomial using Function

#include<stdio.h>
#include<conio.h>
#include<math.h>
void initpoly(int z[])
{
int i;
for(i=0;i<10;i++)
z[i]=0;
}

Page 14
void add(int a[],int b[],int c[],int n)
{
int i;
for(i=n;i>=0;i--)
{
c[i]=a[i]+b[i];
}
}
void accept(int a[],int n)
{
int i;

for(i=n;i>=0;i--)
{
printf("\nEnter the Co-efficient of a[%d] term :\t",i);
scanf("%d",&a[i]);
}
}
void disp(int a[],int n)
{
int i;
for(i=n;i>0;i--)
{
if(a[i]!=0)
{
printf("%dX^%d+",a[i],i);
}
}
printf("%d",a[i]);
}
int pow(int x,int i)
{
int k=0;int ans=1;
for(k=1;k<=i;k++)
{ ans=ans*x;}
return ans;
}
int eval(int a[],int n,int x)
{
Page 15
int i,ans=0;
for(i=n;i>=0;i--)
{
ans=ans+(a[i]*pow(x,i));
}
return ans;
}

void main()
{
int a[10],b[10],c[10],n,ch,x,k,m;
clrscr();
do
{
printf("\n1.Evalution \n2.Addition of Polynomial \n3.Exit");
switch(ch)
{
case 1:
printf("\nEnter the Order of Polynomial : ");
scanf("%d",&n);
accept(a,n);
printf("\nPolynomial is : ");
disp(a,n);
printf("\nEnter the Value for Variable X : ");
scanf("%d",&x);
printf("\n Evaluated Value is %d",eval(a,n,x));
break;
case 2:
initpoly(a);
initpoly(b);
initpoly(c);
printf("\nEnter the Order of 1st Polynomial : ");
scanf("%d",&m);
accept(a,m);
printf("\nEnter the order of 2nd Polynomial : ");
scanf("%d",&n);
accept(b,n);
printf("\nPolynomial 1 : ");
disp(a,m);
printf("\nPolynomial 2 : ");
Page 16
disp(b,n);
k=m>n ? m:n;
add(a,b,c,k);
printf("\nResultant polynomial Is : ");
disp(c,k);
break;
case 3:
exit(0);
}
}
while(ch!=3);
getch();
}

Slip6_2 : carete singly link list i) create ii) display iii) insert at last position iv)
search node v) exit

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)

Page 17
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);

}
t->next=NULL;
}

void display()
{
struct node *t;

for(t=head;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}
void insert()
{
struct node *nw,*t;

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


printf("\n Enter node for inserting: ");
scanf("%d",&nw->data);

for(t=head;t->next!=NULL;t=t->next);
t->next=nw;
nw->next=NULL;

void search()
{
struct node *t;
int flag=0,sr;
printf("\n Enter data to be search :");
scanf("%d",&sr);
Page 18
for(t=head;t!=NULL;t=t->next)
{
if(sr==t->data)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n Data is found \n");
else
printf("\n Data is NOT found \n");
}

main()
{
int ch;

clrscr();

do
{
printf("\n MENU \n 1.Create \n 2.Display \n 3.Insert a new node \n
4.Search node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2:
display();
break;
case 3: insert();
break;

case 4:
search();
Page 19
break;

Page 20
case 5:
exit(0);

}
}
while(ch!=5);
getch();
}

Slip7_2 : Union of two link list


Slip9_2 : intersection of two link list

#include<stdio.h>

typedef struct node


{
int data;
struct node *next;
}NODE;

NODE *alloc(int val)


{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=NULL;
return temp;
}

NODE *getnode()
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
printf("\nEnter the data: ");
scanf("%d",&temp->data);
temp->next=NULL;
return temp;
}
NODE *search(NODE *list,int val)

Page 21
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;ptr=ptr->next);
return(ptr);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}

NODE *create()
{
NODE *ptr,*temp,*list=NULL;
int n,i;
printf("\n Enter how many nodes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=temp=(NODE *)malloc(sizeof(NODE));
printf("\nEnter the data: ");
scanf("%d",&temp->data);
temp->next=NULL;
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}

return(list);
}

NODE *unionlist(NODE *list1,NODE *list2)


Page 22
{

Page 23
NODE *temp,*ptr1,*ptr2,*list3=NULL;
int i,val;

for(ptr1=list1;ptr1!=NULL;ptr1=ptr1->next)
{ temp=alloc(ptr1->data);
if(list3==NULL)
list3=temp;
else
{
ptr2=findlast(list3);
ptr2->next=temp;
}
}

for(ptr1=list2;ptr1!=NULL;ptr1=ptr1->next)
{
ptr2=search(list1,ptr1->data);
if(ptr2==NULL)
{
temp=alloc(ptr1->data);

ptr2=findlast(list3);
ptr2->next=temp;
}
}
return(list3);
}

NODE *intersect(NODE *list1,NODE *list2)


{
NODE *temp,*ptr1,*ptr2,*list3=NULL;

for(ptr1=list1;ptr1!=NULL;ptr1=ptr1->next)
{
ptr2=search(list2,ptr1->data);
if(ptr2!=NULL)
{
temp=alloc(ptr1->data);
if(list3==NULL)
list3=temp;
Page 24
else

Page 25
{
ptr2=findlast(list3);
ptr2->next=temp;
}
}
}

return(list3);
}

void display(NODE *list)


{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf("%d->",ptr->data);
printf("NULL");
}

void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
printf("\nCreate first list.");
list1=create();
printf("\nCreate second list.");
list2=create();

printf("\n Data in first list: ");


display(list1);

printf("\n Data in second list: ");


display(list2);

printf("\n\n Union of two list is: ");


list3=unionlist(list1,list2);
if(list3==NULL)
printf("\nList of union is empty");
else
display(list3);

printf("\n\n Intersection of two list: ");


Page 26
list3=intersect(list1,list2);
if(list3==NULL)
printf("\nThere is no common elements in list1 and list2");
else
display(list3);

getch();
}

Slip 8_2 : Static implemention of queue

#include<stdio.h>
#include<conio.h>
#define SIZE 20
int F,R;
int Q[SIZE];
void initQ()
{
F=-1;
R=-1;
}

int isEmptyQ()
{
if(F==R)
return 1;
else
return 0;

int isFullQ()
{
if(R==SIZE-1)
return 1;
else
return 0;

}
Page 27
void AddQ(int n)
{
if(isFullQ())
printf("Queue is full");
else
{
R++;
Q[R]=n;
}
}

int DeleteQ()
{
if(isEmptyQ())
{ printf("\n Queue is empty");
return -1;
}
else
{
int n;
n=Q[++F];
return n;
}

void display()
{
int i=F+1;

while(i<=R)
{
printf("%d\t",Q[i]);
i++;
}
}
main()
Page 28
{
int ch,n;

Page 29
clrscr();
initQ();

do
{
printf("\n 1.Add \n 2.Delete \n 3.Display \n 4.Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter data ");
scanf("%d",&n);
AddQ(n);
break;
case 2: n=DeleteQ();
if(n!=-1)
{printf("\n deleted data is : %d ",n);
}
break;
case 3 :printf("Elements are :");
display();
case 4:break;
}
}while(ch!=4);
getch();
}

Slip 10_2 : Dynamic implemention of queue

#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *F,*R;
void initQ()

Page 30
{
F=R=NULL;
}

int isEmptyQ()
{
if(F==R)
return 1;
else
return 0;

void AddQ(int n)
{
NODE *nw;
nw=(NODE *)malloc(sizeof(NODE));
nw->data=n;
nw->next=NULL;
if(F==NULL)
F=R=nw;

else
{
R->next=nw;
R=nw;
}
}

int DeleteQ()
{
NODE *temp;int no;
if(isEmptyQ())
{ printf("\n Queue is empty");
return -1;
}
else
{
temp=F;
Page 31
n
o
=
t
e
m
p
-
>
d
a
t
a
;

Page 32
F=F->next;
free(temp);
}
return no;

void display()
{
NODE *t;
for(t=F;t!=NULL;t=t->next)
printf("%d \t",t->data);
}
main()
{
int ch,n;
clrscr();
initQ();

do
{
printf("\n 1.Add \n 2.Delete \n 3.Display \n 4.Exit");
printf("\n Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter data ");
scanf("%d",&n);
AddQ(n);
break;
case 2: n=DeleteQ();
if(n!=-1)
{printf("\n deleted data is : %d ",n);
}
break;
case 3 :printf("Elements are :");
display();
case 4:break;
default :printf("\n Invalid Choice");
}
Page 33
}while(ch!=4);
getch();
}

Slip13_2 : carete singly link list i) create ii) display iii) insert at any position
iv) search node v) exit

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);

}
t->next=NULL;
}

Page 34
void display()
{
struct node *t;

for(t=head;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}
void insert()
{
struct node *nw,*t;
int cnt=0,i,p;
printf("\n Enter position ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t->next)
cnt++;
if(p>=cnt+1 )
{
printf("\n Invalid position ");
}
else
{
nw=(struct node *)malloc(sizeof(struct node));
printf("\n Enter node for inserting: ");
scanf("%d",&nw->data);
if(p==1)
{
nw->next=head;
head=nw;
}
else if(p==cnt)
{
for(t=head;t->next!=NULL;t=t->next);
t->next=nw;
nw->next=NULL;
}

else
{
Page 35
for(i=1,t=head;i<p-1;i++)
t=t->next;
nw->next = t->next;
t->next = nw;
}
}

void search()
{
struct node *t;
int flag=0,sr;
printf("\n Enter data to be search :");
scanf("%d",&sr);
for(t=head;t!=NULL;t=t->next)
{
if(sr==t->data)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n Data is found \n");
else
printf("\n Data is NOT found \n");
}

main()
{
int ch;

clrscr();

do
{
Page 36
printf("\n MENU \n 1.Create \n 2.Display \n 3.Insert a new node \n
4.Search node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2:
display();
break;
case 3: insert();
break;

case 4:
search();
break;
case 5:
exit(0);

}
}
while(ch!=5);
getch();
}

Slip16_2 : carete singly link list i) create ii) display iii)insert first position iv)
Delete node at any position

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

typedef struct node


{
int data;
struct node *next;
}NODE;

Page 37
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);

}
t->next=NULL;
}

void display()
{
struct node *t;

for(t=head;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}

void insert_first()
{
struct node *nw,*t;
nw=(NODE*)malloc(sizeof(NODE));
printf("\n Enter node for inserting: ");
scanf("%d",&nw->data);

nw->next=head;
Page 38
head=nw;;
}

void delete()
{
struct node *t,*temp;
int cnt=0,i,p;
printf("\n Enter position for delete Data : ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t->next)
cnt++;
if(p>cnt)
{
printf("\n Invalid position ");
}
else
{
if(p==1)
{
temp=head;
head=head->next;
free(temp);
}
else if(cnt==p)
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t- >next=NULL;
free(temp);
}
else
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next = temp->next;
free(temp);
}
}
Page 39
}

main()
{
int ch;
clrscr();

do
{
printf("\n MENU \n 1.Create \n 2.Display \n 3.Add at first position \n
4.Delete node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: insert_first();
break;

case 4: delete();
break;
case 5:break;

}
}
while(ch!=5);
getch();
}

Page 40
Slip 17_2 : Write a ‘C’ program to create a random array of n integers. Sort
the array using bubble sort. Accept a value x from user and use binary search
algorithm to check whether the number is present in array or not and output
the position if the number is present.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void bsort(int a[],int n)
{
int temp,i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
} for

int binsearch(int a[],int n,int lb,int ub,int num)


{
int mid;
while(lb<=ub)
{
mid=(lb+ub)/2;
if(num==a[mid])
return mid;
else if(num<a[mid])
ub=mid-1;
else
lb=mid+1;
}

Page 41
return -1;

Page 42
}

void main()
{
int a[20],i,n,p,lb,ub,num;
clrscr();
printf("\n How many number you want to enter : ");
scanf("%d",&n);

for(i=0;i<n;i++)
{ printf("\n Enter number : ");
scanf("%d",&a[i]);
}
printf("\n sorted array is :");
bsort(a,n);
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}

printf("\n Enter number you want to search : ");


scanf("%d",&num);
lb=0;
ub=n-1;
p=binsearch(a,n,lb,ub,num);
if(p==-1)
printf("\n Element not found");
else
printf("\n Element found");
getch();
}

Slip18_2 : carete singly link list i) create ii) display iii)insert at in between
position iv) Delete node at any position

#include<stdio.h>

Page 43
#include<stdlib.h>
#include<conio.h>

typedef struct node


{
int data;
struct node *next;
}NODE;
NODE * head=NULL;
void create()
{
int i,n;
NODE *t;
printf("\n How Many Nodes you Want to Create : ");
scanf("%d",&n);
head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter node 1: ");
scanf("%d",&head->data);
t=head;
for(i=1;i<n;i++)
{
t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);

}
t->next=NULL;
}

void display()
{
struct node *t;

for(t=head;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}
void insert()
Page 44
{
struct node *nw,*t;

int cnt=0,i,p;
printf("\n Enter position ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t->next)
cnt++;
if(p>=cnt || p==1)
{
printf("\n Invalid position ");
}
else
{
nw=(NODE*)malloc(sizeof(NODE));
printf("\n Enter node for inserting: ");
scanf("%d",&nw->data);

for(i=1,t=head;i<p-1;i++)
t=t->next;
nw->next = t->next;
t->next = nw;
}
}

void delete()
{
struct node *t,*temp;
int cnt=0,i,p;
printf("\n Enter position for delete Data : ");
scanf("%d",&p);
for(t=head;t!=NULL;t=t->next)
cnt++;
if(p>cnt)
{
printf("\n Invalid position ");
}
else
{
if(p==1)
Page 45
{
temp=head;
head=head->next;
free(temp);
}
else if(cnt==p)
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next=NULL;
free(temp);
}
else
{
for(i=1,t=head;i<p-1;i++)
t=t->next;
temp=t->next;
t->next = temp->next;
free(temp);
}
}
}

main()
{
int ch;
clrscr();

do
{
printf("\n MENU \n 1.Create \n 2.Display \n 3.Insert node In between
position \n 4.Delete node \n 5.Exit ");
printf("\n enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
Page 46
case 2: display();

Page 47
break;
case 3: insert();
break;

case 4: delete();
break;
case 5:break;

}
}
while(ch!=5);
getch();
}

Slip19_2 : Write a ‘C’ program to read n integers and create two lists such
that all positive numbers are in one list and negative numbers are in another
list. Display both the lists in sorted order.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};

struct node* insert(struct node *f,int no)


{
struct node *t;

if(f==NULL)
{ f=(struct node *)malloc(sizeof(struct node));
f->data=no;
f->next=NULL;
}
else
{

Page 48
for(t=f;t->next!=NULL;t=t->next);
t->next=(struct node *)malloc(sizeof(struct node));
t=t->next;
t->data=no;
t->next=NULL;
}

return f;}
void display(struct node* f)
{
struct node *t;

for(t=f;t!=NULL;t=t->next)
{
printf("\t %d ->",t->data);
}
}

void sort(struct node *f)


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

void main()
{
struct node* h1=NULL,*h2=NULL;
Page 49
int a[10],n,i;
printf("\n Enter no of nodes to be created : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter data : ");
scanf("%d",&a[i]);

}
for(i=0;i<n;i++)
{
if(a[i]>0)
{ printf("\n condition is true %d \n",a[i]);
h1=insert(h1,a[i]);
display(h1);
}
else
{
h2=insert(h2,a[i]);
}
}

printf("\n positive data link list is \n");


display(h1);
printf("\n Negative data link list is \n");
display(h2);
printf("\n Ater sorting postive data link list \n");
sort(h1);
display(h1);
printf("\n Ater sorting negative data link list \n");
sort(h2);
display(h2);
getch();
}

Slip23_2 :convert infix to prefix and evaluate it

#include<stdio.h>
#include<conio.h>
Page 50
#include<string.h>
#include<stdlib.h>
char st[100];
int top=-1;
int prec(char c)
{
switch(c)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
return 0;
}
void push(char c)
{
top++;
st[top]=c;
}
char pop()
{
char c;
c=st[top];
top--;
return c;
}
int main()
{
int i,j,p,no1,no2;
char ch,s[30],s1[30],s2[30];
clrscr();
printf("\nEnter the Infix Expr");
Page 51
gets(s);

s1=strrev(s);
printf("reverse %s",strrev(s));
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]=='(' )
s1[i]=')';
else if(s1[i]== ')' )
s1[i]='(';
}
j=0;

for(i=0;s1[i]!='\0';i++)
{
switch(s1[i])
{
case '(':
case '+':
case '-':
case '/':
case '*':
case '^':
p=prec(s1[i]);
while(top!=-1 && p<=prec(st[top]))
s2[j++]=pop();
push(s1[i]);
break;
case ')':
do
{
ch=pop();
if(ch!='(')
{
s2[j++]=ch;
}
}

while(ch!='(');
break;
Page 52
default:

Page 53
s2[j++]=s1[i];
}
}
while(top!=-1)
{
s2[j++]=pop();
}
s2[j]='\0';
s2=strrev(s2);
printf("\nPreFix Expr %s",strrev(s2));
printf("\n\nEval of PostFix Expr is \n\n");
j=0;
for(i=0;s2[i]!='\0';i++)
{
if(s2[i]<='9' && s2[i]>='0')
push(s2[i]-48);
else
{
no2=pop();
no1=pop();
switch(s2[i])
{
case '+':
push(no1+no2);
break;
case '-':
push(no1-no2);
break;

case '/': push(no1/no2);


break;

case '*':
push(no1*no2);
break;
case '^':
push(pow(no1,no2));
break;
case '%':
push(no1%no2);
Page 54
break;
}

} end of else
}
j=pop();
printf("Result is %d ",j);
getch();
}

Slip24_2 and Slip26_1: Doubly Circular link list and print it in reverse order

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

typedef struct node


{
int data;
struct node *prev;
struct node *next;
}NODE;
NODE *head=NULL;
void create()
{
NODE *t;
int i,no;
printf("enter no of nodes");
scanf("%d",&no);

head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter data : ");
scanf("%d",&head->data);
head->next=NULL;
t=head;
for(i=1;i<no;i++)
{
t->next=(struct node*)malloc(sizeof(struct node));

Page 55
t->next->prev=t;
t=t->next;
printf("\n Enter data : ");
scanf("%d",&t->data);
t->next=NULL;
}
t->next=head;
head->prev=t;

void display()
{
NODE *t;
t=head;
do
{
printf("\t%d",t->data);
t=t->next;
}while(t!=head);
}

void rev()
{
NODE *s,*t;

for(s=head;s->next!=head;s=s->next);
t=s;
do
{
printf("\t%d",t->data);
t=t->prev;
}while(t!=head);
printf("\t%d",t->data);
}

void main()
{
clrscr();
create();
Page 56
display();
printf("\n Reverse Linklist is ");
rev();
getch();
}

Slip25_2 :convert infix to postfix and evaluate it

#include<stdio.h>
#include<conio.h>
#include<string.h>
char st[100];
int top=-1;
int prec(char c)
{
switch(c)
{
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
case '%':
return 2;
case '^':
return 3;
}
return 0;
}
void push(char c)
{
top++;
st[top]=c;
}
char pop()
{

Page 57
char c;
c=st[top];
top--;
return c;
}
int main()
{
int i,j,p,no1,no2;
char ch,s1[30],s2[30];
clrscr();
printf("\nEnter the Infix Expr");
gets(s1);
for(i=0;s1[i]!='\0';i++)
{
if(s1[i]=='(' )
s1[i]=')';
else if if(s1[i]== ')' )
s1[i]='(';
}
j=0;

for(i=0;s1[i]!='\0';i++)
{
switch(s1[i])
{
case '(':
case '+':
case '-':
case '/':
case '*':
case '^':
p=prec(s1[i]);
while(top!=-1 && p<=prec(st[top]))
s2[j++]=pop();
push(s1[i]);
break;
case ')':
do
{
ch=pop();
Page 58
if(ch!='(')

Page 59
{
s2[j++]=ch;
}
}

while(ch!='(');
break;
default:
s2[j++]=s1[i];
}
}
while(top!=-1)
{
s2[j++]=pop();
}
s2[j]='\0';
printf("\nPreFix Expr %s",s2);
printf("\n\nEval of PostFix Expr is \n\n");
j=0;
for(i=0;s2[i]!='\0';i++)
{
if(s2[i]<='9' && s2[i]>='0')
push(s2[i]-48);
else
{
no2=pop();
no1=pop();
switch(s2[i])
{
case '+':
push(no1+no2);
break;
case '-':
push(no1-no2);
break;

case '/': push(no1/no2);


break;

case '*':
Page 60
push(no1*no2);
break;
/*case '^':
push(pow(no1,no2));
break;*/
case '%':
push(no1%no2);
break;
}

} end of else
}

j=pop();
printf("Result is %d ",j);
getch();
}

Page 61
Page 62

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy