Slip 1(2)
Write a ‘C’ program to reverse a string using Static implementation of Stack.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 50
typedef struct stack
{
char data[MAX];
int top;
}stack;
int empty(stack *p)
{
if(p->top==-1)
return (1);
else
return(0);
}
int full(stack *p)
{
if(p->top==MAX-1)
return(1);
else
return(0);
}
void push (stack *p,char x)
{
if(!full(p))
{
p->top=p->top+1;
p->data[p->top]=x;
}
else
{
printf("\n stack is full");
}
}
char pop(stack *p)
{
char x;
if(!empty(p))
{
x=p->data[p->top];
p->top=p->top-1;
return(x);
}
else
{
printf("\n stack is empty");
return(0);
}
}
void main()
{
stack s;
char ch;
s.top=-1;
clrscr();
printf("\Enter a string");
while((ch=getchar())!='\n')
{
if(!full(&s))
{
push(&s,ch);
}
else
{
printf("\n stack is full");
exit(0);
}
}
printf("\n Reversed string");
while(!empty(&s))
{
ch=pop(&s);
printf("%c",ch);
}
getch();
}
********************Out put********************
Enter a string Foresight
Reversed string thgiseroF
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 20
struct stack
{
int item[MAX];
int top;
}stk;
void initialise(struct stack *ss)
{
ss->top=-1;
}
int isfull(struct stack *ss)
{
return (ss->top==MAX-1);
}
void push(struct stack *ss,int ch)
{
if(!isfull(ss))
{
ss->top++;
ss->item[ss->top]=ch;
}
else
printf("\n The stack is full");
}
int pop(struct stack *ss)
{
int item=ss->item[ss->top];
ss->top--;
return item;
}
int isempty(struct stack *ss)
{
return(ss->top==-1);
}
void convertItoP(char infix[],char postfix[])
{
int i,j;
char ch;
struct stack s;
intialize(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case'+':case'-':
case'*':case'/':
case'%':case'(':push(&s,infix[i]);
break;
case ')':while((ch=pop(&s))!='(')
postfix[++j]=ch;
break;
default:postfix[j++]=infix[i];
}
}
while(!isempty(&s))
postfix[j++]=pop(&s);
postfix[j]='/0';
printf("/n postfix string:%s",postfix);
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("\n Enter Fully parenthesis infix string:");
scanf("%s",infix);
convertItoP(infix,postfix);
getch();
}
int a[10],n,x,i,e;
clrscr();
printf("\n Enter the degree of polynomiL:");
scanf("%d",&n);
printf("\n Enter coefficient ");
for(i=n;i>=0;i--)
{
printf("\n\nCoefficient of a[%d]",i);
scanf("%d",&a[i]);
}
printf("\n Given polynomial is");
for(i=n;i>0;i--)
{
if(a[i]!=0)
{
printf("%dx^%d+",a[i],i);
}
}
printf("%d",a[i]);
printf("\n Enter value of x:");
scanf("%d",&x);
e=evaluate(a,n,x);
printf("Evaluation of polynomial=\t %d",e);
getch();
}
int evaluate(int a[],int n,int x)
{
int sum=0,i;
for(i=n
;i>=0;i--)
{
sum=sum+a[i]*pow(x,i);
}
return sum;
}
********************* out put****************************
Enter the degree of polynomiL:3
Enter coefficient
Coefficient of a[3]3
Coefficient of a[2]6
Coefficient of a[1]9
Coefficient of a[0]1
Given polynomial is3x^3+6x^2+9x^1+1
Enter value of x:2
Evaluation of polynomial= 67
Write a ‘C’ program to reverse a string using Static implementation of Stack.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 50
typedef struct stack
{
char data[MAX];
int top;
}stack;
int empty(stack *p)
{
if(p->top==-1)
return (1);
else
return(0);
}
int full(stack *p)
{
if(p->top==MAX-1)
return(1);
else
return(0);
}
void push (stack *p,char x)
{
if(!full(p))
{
p->top=p->top+1;
p->data[p->top]=x;
}
else
{
printf("\n stack is full");
}
}
char pop(stack *p)
{
char x;
if(!empty(p))
{
x=p->data[p->top];
p->top=p->top-1;
return(x);
}
else
{
printf("\n stack is empty");
return(0);
}
}
void main()
{
stack s;
char ch;
s.top=-1;
clrscr();
printf("\Enter a string");
while((ch=getchar())!='\n')
{
if(!full(&s))
{
push(&s,ch);
}
else
{
printf("\n stack is full");
exit(0);
}
}
printf("\n Reversed string");
while(!empty(&s))
{
ch=pop(&s);
printf("%c",ch);
}
getch();
}
********************Out put********************
Enter a string Foresight
Reversed string thgiseroF
Slip1(3)
Write a menu driven program using ‘C’ for singly linked
list-
-
To create linked list.
-
To display linked list
-
To insert node at last position of linked list.
- To delete node from specific position #include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 20
struct stack
{
int item[MAX];
int top;
}stk;
void initialise(struct stack *ss)
{
ss->top=-1;
}
int isfull(struct stack *ss)
{
return (ss->top==MAX-1);
}
void push(struct stack *ss,int ch)
{
if(!isfull(ss))
{
ss->top++;
ss->item[ss->top]=ch;
}
else
printf("\n The stack is full");
}
int pop(struct stack *ss)
{
int item=ss->item[ss->top];
ss->top--;
return item;
}
int isempty(struct stack *ss)
{
return(ss->top==-1);
}
void convertItoP(char infix[],char postfix[])
{
int i,j;
char ch;
struct stack s;
intialize(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case'+':case'-':
case'*':case'/':
case'%':case'(':push(&s,infix[i]);
break;
case ')':while((ch=pop(&s))!='(')
postfix[++j]=ch;
break;
default:postfix[j++]=infix[i];
}
}
while(!isempty(&s))
postfix[j++]=pop(&s);
postfix[j]='/0';
printf("/n postfix string:%s",postfix);
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("\n Enter Fully parenthesis infix string:");
scanf("%s",infix);
convertItoP(infix,postfix);
getch();
}
Slip 2(2)
Write a ‘C’
program to evaluate a given polynomial using function. (Use array).
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10],n,x,i,e;
clrscr();
printf("\n Enter the degree of polynomiL:");
scanf("%d",&n);
printf("\n Enter coefficient ");
for(i=n;i>=0;i--)
{
printf("\n\nCoefficient of a[%d]",i);
scanf("%d",&a[i]);
}
printf("\n Given polynomial is");
for(i=n;i>0;i--)
{
if(a[i]!=0)
{
printf("%dx^%d+",a[i],i);
}
}
printf("%d",a[i]);
printf("\n Enter value of x:");
scanf("%d",&x);
e=evaluate(a,n,x);
printf("Evaluation of polynomial=\t %d",e);
getch();
}
int evaluate(int a[],int n,int x)
{
int sum=0,i;
for(i=n
;i>=0;i--)
{
sum=sum+a[i]*pow(x,i);
}
return sum;
}
********************* out put****************************
Enter the degree of polynomiL:3
Enter coefficient
Coefficient of a[3]3
Coefficient of a[2]6
Coefficient of a[1]9
Coefficient of a[0]1
Given polynomial is3x^3+6x^2+9x^1+1
Enter value of x:2
Evaluation of polynomial= 67
Slip 2(3)
Write a ‘C’ program to accept an infix expression, convert it into its equivalent postfix expression and display the result.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define max 20
#include<process.h>
#define max 20
struct stack
{
int item[max];
int top;
}stk;
void initialise(struct stack *ss)
{
ss->top=-1;
}
int isfull(struct stack *ss)
{
return(ss->top==max-1);
}
void push(struct stack *ss,int ch)
{
if(!isfull(ss))
{
ss->top++;
ss->item[ss->top]=ch;
}
else
printf("\n The stack is full");
}
int pop(struct stack *ss)
{
int item=ss->item[ss->top];
ss->top--;
return item;
}
int isempty(struct stack *ss)
{
return(ss->top==-1);
}
void convertItop(char infix[],char postfix[])
{
int i,j;
char ch;
struct stack s;
initialise(&s);
j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '+':case'-':
case '*':case'/':
case'%':case '(':push(&s,infix[i]);
break;
case')':while((ch=pop(&s))!='(')
postfix[j++]=ch;
break;
default:postfix[j++]=infix[i];
}
}
while(!isempty(&s))
postfix[j++]=pop(&s);
postfix[j]='\0';
printf("\n postfix string:%s",postfix);
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("\n Enter fully parenthesis infix string..");
scanf("%s",infix);
convertItop(infix,postfix);
getch();
}
********************OUTPUT****************************
Enter fully parenthesis infix string..
A*(B+C*D)-E/F*(G+D)
postfix string:ABCD*+EFGD+*/-*
Slip 3(2)
Write a ‘C’ program for implementing
Linear Search method using function.
#include<stdio.h>
#include<conio.h>
int lsearch(int arr[],int sk,int s)
{
int i;
for(i=0;i<s;i++)
{
if(arr[i]==sk)
{
return i;
}
}
return -1;
}
void main()
{
int *arr,i,found,s,key;
clrscr();
printf("\n\nHow many numbers :- ");
scanf("%d",&s);
arr=(int*)malloc(s*sizeof(int));
printf("\nEnter %d numbers :- ",s);
for(i=0;i<s;i++)
scanf("%d",&arr[i]);
printf("{");
for(i=0;i<s;i++)
printf("%d ,",arr[i]);
printf("}");
printf("\nEnter search key :- ");
scanf("%d",&key);
found = lsearch(arr,key,s);
if(found == -1)
printf("\nNumber not found ...");
else
printf("\nNumber found at position %d ",found+1);
getch();
}
*************************OUTPUT*******************************
How many numbers :- 5
Enter 5 numbers :- 11 22 33 44 55
{11 ,22 ,33 ,44 ,55 ,}
Enter search key :- 33
Number found at position 3
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf("\n\nMENU\n\n");
printf("\n1.CREATE\n");
printf("\n2.DISPLAY\n");
printf("\n3.COUNT\n");
printf("\n4.EXIT\n");
printf("\nENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nHOW MANY NODES U WANT TO CREATE\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE DATA");
scanf("%d",&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf("\n\nELEMENTS ARE %d",cnt);
}
void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf("\n\nLIST IS EMPTY");
}
else
{
q=start;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->link;
}
printf("NULL");
}
}
*******************output**************************
ENTER UR CHOICE
1
HOW MANY NODES U WANT TO CREATE
5
ENTER THE DATA6
ENTER THE DATA8
ENTER THE DATA5
ENTER THE DATA7
ENTER THE DATA0
MENU
1.CREATE
2.DISPLAY
3.COUNT
4.EXIT
ENTER UR CHOICE
3
ELEMENTS ARE 5
MENU
1.CREATE
2.DISPLAY
3.COUNT
4.EXIT
ENTER UR CHOICE
4
Write a ‘C’ program to create two singly linked lists and perform the union of two lists and display it.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *unionlist(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
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);
getch();
}
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)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;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=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
NODE *unionlist(NODE *list1,NODE *list2)
{
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);
}
void display(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf("%d->",ptr->data);
printf("NULL");
}
*********************output********************************
Create first list.
Enter how many nodes : 5
Enter the data: 6
Enter the data: 8
Enter the data: 9
Enter the data: 4
Enter the data: 3
Create second list.
Enter how many nodes : 1
Enter the data: 2
Data in first list: 6->8->9->4->3->NULL
Data in second list: 2->NULL
Union of two list is: 6->8->9->4->3->2->NULL
Slip 3(3)
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<conio.h>
#include<alloc.h>
typedef struct node
{
int data;
struct node *next;
}node;
node* createsl(node *list)
{
int n,i,div,rem;
struct node *new_node,*temp,*null;
list=null;
printf("\n Enter Number");
scanf("%d",&n);
do
{
rem=n%10;
div=n/10;
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=rem;
new_node->next=null;
if(list==null)
list=temp=new_node;
else
{
temp->next=new_node;
temp=new_node;
}
n=div;
}
while(div!=0);
return list;
}
node *reverselst(node *list)
{
node *temp1,*temp2,*temp3,*null;
temp1=list;
temp2=temp1->next;
temp3=temp2->next;
temp1->next=null;
while(temp3)
{
temp2->next=temp1;
temp1=temp2;
temp2=temp3;
temp3=temp3->next;
}
temp2->next=temp1;
return temp2;
}
void display(node *list)
{
struct node *new_node,*null;
printf("\n The linked list:");
new_node=list;
while(new_node!=null)
{
printf("|%d|%u|--->",new_node->data,new_node->next);
new_node=new_node->next;
}
printf("NULL");
}
void main()
{
node *null,*list=null;
clrscr();
list=createsl(list);
list=reverselst(list);
display(list);
getch();
}
*********************output********************************
Enter Number815
The linked list:|8|1886|--->|1|1878|--->|5|0|--->NULL
Slip 4(2)
Write a ‘C’ program that create a 2-D table of integers whose size will be specified at run time. (Dynamic Memory Allocation)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,**ip,sum;
float avg;
clrscr();
printf("\n Enter how many rows and columns:");
scanf("%d%d",&m,&n);
ip=(int**)malloc(sizeof(int)*m);
for(i=0;i<m;i++)
ip[i]=(int *)malloc(sizeof(int)*n);
printf("\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("Elements[%d][%d]\t",i+1,j+1);
scanf("\t%d",&ip[i][j]);
sum+=ip[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",ip[i][j]);
printf("\n");
}
avg=(float)sum/(m*n);
printf("\n average=%f",avg);
}
*******************output*************************
Enter how many rows and columns:
3
3
Elements[1][1] 11
Elements[1][2] 22
Elements[1][3] 33
Elements[2][1] 44
Elements[2][2] 55
Elements[2][3] 66
Elements[3][1] 77
Elements[3][2] 88
Elements[3][3] 99
11 22 33
44 55 66
77 88 99
average=601.000000
Slip 4(3)
Write a menu driven program in ‘C’ for static implementation of Circular Queue for integers. The menu includes
Insert
Delete
Display
Exit
#include<stdio.h>
#include<conio.h>
struct queue
{
struct queue *next;
int data,null;
}*front,*rear;
void initcq()
{
rear==NULL;
}
int isemptycq()
{
return rear==NULL;
}
void addcq(int element)
{
struct queue *newnode;
newnode=(struct queue*)malloc(sizeof(struct queue));
newnode->data=element;
if(rear==NULL)
{
rear=newnode;
rear->next=rear;
}
else
{
newnode->next=rear->next;
rear->next=newnode;
rear=newnode;
}
}
int deletecq()
{
struct queue *front=rear->next;
int n=front->data;
if(rear->next==rear)
free(rear);
else
{
rear->next=front->next;
free(front);
}
return n;
}
void main()
{
int n,choice,null;
struct queue cq;
clrscr();
initcq();
do
{
printf("\n1.Add\n2.Delete\n3.Exit");
printf("\n Enter Your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter Add element");
scanf("%d",&n);
addcq(n);
break;
case 2: if(isemptycq())
printf("\n Queue is empty");
else
printf("\n Deleted element is %d",deletecq());
break;
case 3: exit(0);
}
}
while(choice!=3);
getch();
}
*************************output******************************
1.Add
2.Delete
3.Exit
Enter Your choice 1
Enter Add element 11
1.Add
2.Delete
3.Exit
Enter Your choice 1
Enter Add element 22
1.Add
2.Delete
3.Exit
Enter Your choice 2
Deleted element is 11
1.Add
2.Delete
3.Exit
Enter Your choice3
slip 5(3)
Write a ‘C’ program to create a singly linked list and count total number of nodes in it and display the result.
#include<stdio.h>#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf("\n\nMENU\n\n");
printf("\n1.CREATE\n");
printf("\n2.DISPLAY\n");
printf("\n3.COUNT\n");
printf("\n4.EXIT\n");
printf("\nENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nHOW MANY NODES U WANT TO CREATE\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE DATA");
scanf("%d",&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf("\n\nELEMENTS ARE %d",cnt);
}
void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf("\n\nLIST IS EMPTY");
}
else
{
q=start;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->link;
}
printf("NULL");
}
}
*******************output**************************
ENTER UR CHOICE
1
HOW MANY NODES U WANT TO CREATE
5
ENTER THE DATA6
ENTER THE DATA8
ENTER THE DATA5
ENTER THE DATA7
ENTER THE DATA0
MENU
1.CREATE
2.DISPLAY
3.COUNT
4.EXIT
ENTER UR CHOICE
3
ELEMENTS ARE 5
MENU
1.CREATE
2.DISPLAY
3.COUNT
4.EXIT
ENTER UR CHOICE
4
Slip 7(3)
Write a ‘C’ program to create two singly linked lists and perform the union of two lists and display it.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *unionlist(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
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);
getch();
}
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)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;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=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
NODE *unionlist(NODE *list1,NODE *list2)
{
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);
}
void display(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next)
printf("%d->",ptr->data);
printf("NULL");
}
*********************output********************************
Create first list.
Enter how many nodes : 5
Enter the data: 6
Enter the data: 8
Enter the data: 9
Enter the data: 4
Enter the data: 3
Create second list.
Enter how many nodes : 1
Enter the data: 2
Data in first list: 6->8->9->4->3->NULL
Data in second list: 2->NULL
Union of two list is: 6->8->9->4->3->2->NULL
Slip 8(3)
Write a ‘C’ program to create a singly linked list, reverse it and display both the list.
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start;
void createlist(int);
void disp();
void main()
{
struct list *p1,*p2,*p3;
int i,n,m;
clrscr();
printf("\nHow many nodes u Want to Created");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter the Data");
scanf("%d",&m);
createlist(m);
}
printf("\n\nCreated List Is\n\n");
disp();
printf("\n\nReverses List is\n\n");
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
disp();
getch();
}
void createlist(int m)
{
struct list *tmp,*q;
tmp=(struct list *) malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
q=start;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->link;
}
printf("NULL");
}
**********************out put********************
How many nodes u Want to Created 5
Enter the Data 6
Enter the Data 4
Enter the Data 9
Enter the Data 10
Enter the Data7
Created List Is
6->4->9->10->7->NULL
Reverses List is
7->10->9->4->6->NULL
Slip 9(3)
Write a ‘C’ program to create two singly linked
lists and perform the intersection
operations on two lists and display the
resultant list
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *intersect(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
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 Intersection of two list:");
list3=intersect(list1,list2);
if(list3==NULL)
printf("\nThere is no common elements in list1 and list2");
else
display(list3);
getch();
}
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)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;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=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
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;
else
{
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");
}
***************output**************************
Create first list.
Enter how many nodes :3
Enter the data:9
Enter the data:8
Enter the data:6
Create second list.
Enter how many nodes :3
Enter the data:6
Enter the data:5
Enter the data:3
Data in first list:9->8->6->NULL
Data in second list: 6->5->3->NULL
Intersection of two list:6->NULL
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *alloc(int);
NODE *create();
NODE *findlast(NODE *);
NODE *intersect(NODE *,NODE *);
NODE *search(NODE *,int);
void display(NODE *);
NODE *getnode();
void main()
{
NODE *list1=NULL,*list2=NULL,*list3=NULL;
clrscr();
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 Intersection of two list:");
list3=intersect(list1,list2);
if(list3==NULL)
printf("\nThere is no common elements in list1 and list2");
else
display(list3);
getch();
}
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)
{
NODE *ptr;
for(ptr=list;ptr!=NULL && ptr->data!=val;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=getnode();
if(list==NULL)
{
list=temp;
}
else
{
ptr=findlast(list);
ptr->next=temp;
}
}
return(list);
}
NODE *findlast(NODE *list)
{
NODE *ptr;
for(ptr=list;ptr->next!=NULL;ptr=ptr->next);
return(ptr);
}
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;
else
{
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");
}
***************output**************************
Create first list.
Enter how many nodes :3
Enter the data:9
Enter the data:8
Enter the data:6
Create second list.
Enter how many nodes :3
Enter the data:6
Enter the data:5
Enter the data:3
Data in first list:9->8->6->NULL
Data in second list: 6->5->3->NULL
Intersection of two list:6->NULL
Slip 10(3)
Write a menu driven program using ‘C’ for
Dynamic implementation of Queue for integers. The menu includes
-
Insert
-
Delete
-
Display
-
Exit
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*front=NULL,*rear=NULL;
void create(int m)
{
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(front==NULL)
front=tmp;
else
rear->link=tmp;
rear=tmp;
}
void del()
{
struct list *tmp;
if(front==NULL)
printf("\n\nQUEUE IS FULL");
else
{
tmp=front;
printf("\n\nDELETED IS ELEMENT %d",tmp->data);
front=front->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(front==NULL)
printf("\n\nQUEUE IS EMPTY");
else
{
q=front;
while(q!=NULL)
{
printf("%d==>",q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
clrscr();
do
{
printf("\n\nMENU");
printf("\n\n1.INSERT ");
printf("\n\n2.DELETE ");
printf("\n\n3.DISPLAT ");
printf("\n\n4.EXIT");
printf("\n\nENTER UR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE NUMBER");
scanf("%d",&n);
create(n);
break;
case 2:
del();
break;
case 3:
printf("\n\nQUEUE ELEMENTS ARE \n\n");
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
*********************out put***********************
ENTER UR CHOICE 1
ENTER THE NUMBER 5
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE 2
DELETED IS ELEMENT 5
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE 3
QUEUE ELEMENTS ARE
5==>
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE4
#include<conio.h>
struct list
{
int data;
struct list *link;
}*front=NULL,*rear=NULL;
void create(int m)
{
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(front==NULL)
front=tmp;
else
rear->link=tmp;
rear=tmp;
}
void del()
{
struct list *tmp;
if(front==NULL)
printf("\n\nQUEUE IS FULL");
else
{
tmp=front;
printf("\n\nDELETED IS ELEMENT %d",tmp->data);
front=front->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(front==NULL)
printf("\n\nQUEUE IS EMPTY");
else
{
q=front;
while(q!=NULL)
{
printf("%d==>",q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
clrscr();
do
{
printf("\n\nMENU");
printf("\n\n1.INSERT ");
printf("\n\n2.DELETE ");
printf("\n\n3.DISPLAT ");
printf("\n\n4.EXIT");
printf("\n\nENTER UR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE NUMBER");
scanf("%d",&n);
create(n);
break;
case 2:
del();
break;
case 3:
printf("\n\nQUEUE ELEMENTS ARE \n\n");
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
*********************out put***********************
ENTER UR CHOICE 1
ENTER THE NUMBER 5
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE 2
DELETED IS ELEMENT 5
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE 3
QUEUE ELEMENTS ARE
5==>
MENU
1.INSERT
2.DELETE
3.DISPLAT
4.EXIT
ENTER UR CHOICE4