0% found this document useful (0 votes)
17 views35 pages

DS - 15 Marks Slip - 2

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)
17 views35 pages

DS - 15 Marks Slip - 2

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/ 35

DS 15 Marks

Slip1_1 :Write a ‘C’ program to reverse a string using Static implementation of Stack.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define SIZE 40
#include<stdio.h>
#include<conio.h>
#define SIZE 20
char stk[SIZE];
int top;
void init()
{ top=-1; }
int isfull()
{ if(top==SIZE-1)
return 1;
else
return 0;
}
int isEmpty()
{ if(top==-1)
return 1;
else
return 0;
}
void push(char c)
{ if(isfull())
{ printf("Stack is overflow"); }
else
{ top++;
stk[top]=c;
}
}
char pop()
{ if(isEmpty())
{ printf("Stack is empty"); }
else
{ char c;
c=stk[top];
top--;
return c;
}

Page 1
}
void main()
{ char s[30];
int i;
clrscr();
printf("\nEnter String : ");
gets(s);
init();
for(i=0;s[i]!='\0';i++)
{ push(s[i]); }
printf("\n Reverse String is : ");
while(!isEmpty())
{ printf("%c",pop());
}
getch();
}

Slip 2_1 : Evalution of polynomial

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

int eval(int a[],int n,int x)


{
int i;
double p,sum=0;
for(i=n;i>=0;i--)
{
p=pow(x,i);
sum=sum+a[i]*p;
}
return sum;
}

void main()
{
int a[10],n,c,i,e;
printf("Enter the Degree of polynomial : ");
scanf("%d",&n);
printf("\n Enter the coefficient : ");
for(i=n;i>=0;i--)
{
printf("\n Enter coefficient A[%d]",i);

Page 2
scanf("%d",&a[i]);
}
printf("\n Enter the polynomial : ");
for(i=n;i>=0;i--)
{
if(a[i]!=0)
printf("%dX^%d + ",a[i],i);
}
printf("%d",a[i]);

printf("\n Enter the value for X ");


scanf("%d",&x);
e=eval(a,n,x);
printf("\n Evalution of polynomaial is = %d",e);
getch();
}

Slip3_1 : program for implementing Linear Search method using function.

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

void search(int a[],int n,int k)


{ int flag=0,i;
for(i=0;i<n;i++)
{ if(a[i]==k)
{ flag=1;
break;
}
} if(flag==1)
printf("\nElement is found at %d location ",i+1);
else
printf("\nElement is NOT found");
}
void main()
{ int a[20],sr,i,n;
clrscr();
printf("\nEnter how many elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\nEnter element : ");
scanf("%d",&a[i]);
}

Page 3
printf("\nEnter element for search : ");
scanf("%d",&sr);
search(a,n,sr);
getch();
}

Slip no4_1 : 2- D mxn matrix using dynamic memory allocation

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

void main()
{
int **ptr; int row, col,i, j;

printf("\nEnter number of rows for matrix : ");


scanf("%d", &row);
printf("\nEnter number of columns for matrix : ");
scanf("%d", &col);

ptr = (int **) malloc(sizeof(int *) * row);


ptr[i] = (int *)malloc(sizeof(int) * col);

printf("\nEnter elements of matrix :\n");


for(i=0; i< row; i++)
{
for(j=0; j< col; j++)
{
printf("\tA[%d][%d] = ",i, j);
scanf("%d", &ptr[i][j]);
}
}
printf("\nMatrix is:\n");
for(i=0; i< row; i++)
{
for(j=0; j< col; j++)
{
printf("%d\t", ptr[i][j]);
}
printf("\n");
}
getch();
}

Page 4
Slip5_1 : Singly link list and coount no of nodes

#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("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}
void Count_node()
{ NODE*t;
int cnt=0;
for(t=head;t!=NULL;t=t->next)
{ cnt++;
}printf("\nNo of nodes are :%d \n ",cnt);
}

Page 5
void main()
{ int n;
clrscr();
create();
printf("\n Link list is : ");
display();
Count_node();
getch();
}

Slip6_1 :search given elements into the list using Non-Recursive Binary Search Method.

#include<stdio.h>
#include<conio.h>
int b_search(int a[],int lb,int ub,int x)
{ int mid;
while(lb<=ub)
{ mid =(lb+ub)/2;
if(x==a[mid])
return 1;
else if(x<a[mid])
ub=mid-1;
else
lb=mid+1;
}

return 0;
}
void main()
{ int a[20],sr,s,i,n;
clrscr();
printf("\n Enter how many elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\n Enter Data :");
scanf("%d",&a[i]);
}
printf("\n Enter element to be search : ");
scanf("%d",&sr);
s=b_search(a,0,n,sr);
if(s==1)
printf("\nElement is found");
else
printf("\nElement is NOT found");
getch();
Page 6
}

Slip7_1 : search given elements into the list using Recursive Binary Search Method.
#include<stdio.h>
#include<conio.h>
int b_search(int a[],int lb,int ub,int x)
{ int mid;
if(lb<=ub)
{ mid =(lb+ub)/2;
if(x==a[mid])
return 1;
else if(x<a[mid])
return b_search(a,lb,mid-1,x); search at 1st part
else
return b_search(a,mid+1,ub,x);
}

return 0;
}
void main()
{ int a[20],sr,s,i,n;
clrscr();
printf("\n Enter how many elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\n Enter Data :");
scanf("%d",&a[i]);
}
printf("\n Enter element to be search : ");
scanf("%d",&sr);
s=b_search(a,0,n,sr);
if(s==1)
printf("\nElement is found");
else
printf("\nElement is NOT found");
getch();
}

Slip8_1 : create 1-D table dynamically

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
Page 7
{ int n,i;

Page 8
int *ptr;
printf("\n Enter no of elements : ");
scanf("%d",&n);
ptr=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
{ printf("\n Enter no : ");
scanf("%d",&ptr[i]);
}
printf("\n");
for(i=0;i<n;i++)
{ printf("%d\t",ptr[i]);
}
getch();
}

Slip 9_1 : Bubble_sort

#include<stdio.h>
#include<conio.h>
void bubblesort(int a[20],int n)
{ int i,j,temp;
for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
main()
{ int a[100],i,n;
clrscr();
printf("Enter how many elements would you like to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter data:");
scanf("%d",&a[i]);
}
bubblesort(a,n);
printf("The array after sorting is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Page 9
Slip10_1 : Create a Singly link list and print it in reverse

#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("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}

void reverse()

Page 10
{
struct node *s,*t;
int cnt=0,i;
for(s=head;s!=NULL;s=s->next)
{cnt++; }
while(cnt!=0)
{ for(t=head,i=1;i<=cnt-1;i++)
{ t=t->next; }
printf("%d ->\t",t->data);
cnt--;
}
}
void main()
{ int n;
clrscr();
create();
printf("\n Link list is : ");
display();
printf("\n Reverse Link list is : ");
reverse();
getch();
}

Slip 11_1 :Accept a string and Reverse each word of String using stack

#include<stdio.h>
#include<conio.h>
#define SIZE 20
char stk[SIZE];
int top;
void init()
{ top=-1; }
int isfull()
{ if(top==SIZE-1)
return 1;
else
return 0;
}
int isEmpty()
{ if(top==-1)
return 1;
else
return 0;
}
void push(char c)

Page 11
{ if(isfull())
{ printf("Stack is overflow"); }
else
{ top++;
stk[top]=c;
}
}
char pop()
{ if(isEmpty())
{ printf("Stack is empty"); }
else
{ char c;
c=stk[top];
top--;
return c;
}
}
void main()
{ int i;
char s[20];
clrscr();
printf("\nEnter String : ");
gets(s);
init();
for(i=0;s[i]!='\0'; i++)
{ if(s[i]==' ')
{ while(!isEmpty())
{
printf("%c",pop());
}
printf(" ");
}
else
{ push(s[i]);
}
} end of for
while(!isEmpty())
{ printf("%c",pop());
}
getch();
}

Slip12_1 : create a circular singly link list and display it

Page 12
#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("\nHow 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=head;
}

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

void main()
{ int n;
clrscr();
printf("\n Circular Link list is : ");
create();

Page 13
display();
getch();
}

Slip13_1 : sort data using insertion sort

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

main()
{ int a[100],i,n;
clrscr();

printf("\n Enter how many elements would you like to enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter data:");
scanf("%d",&a[i]);
} insertion_sort(a,n);
printf("\n The array after sorting is : ");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Slip14_1 : sort data using selection sort

Page 14
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void selection_sort(int a[20],int n)
{ int i,j,max,temp,pos;
for(i=0;i<n;i++)
{ max=a[0];
pos=0;
for(j=1;j<n-i;j++)
{ if(a[j]>max)
{ max=a[j];
pos=j;
}
}
j--;
temp=a[pos];
a[pos]=a[j];
a[j]=max;
}
}
main()

{ int a[100],i,n;
clrscr();
printf("Enter how many elements would you like to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter data:");
scanf("%d",&a[i]);
}
selection_sort(a,n);

printf("The array after sorting is:\n");


for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Slip15_1 : Accept n student names from user and store it in an array. Write a function to
search given student name into the array using Linear search method.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
Page 15
#include<conio.h>
void Linear_search(char a[10][10],int n,char s[])
{ int i,flag=0;
for(i=0;i<n;i++)
{ if((strcmp(a[i],s))==0)
{ flag=1;
break;
}
}
if(flag==1)
printf("\n %s is found",s);
else
printf("\n %s is NOT found",s);
}

void main()
{ char a[10][10];char sr[10];
int i,n;
clrscr();
printf("\n Enter how many no of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\n Enter student name :");
scanf("%s",a[i]);
} printf("\n Array is:\n");
for(i=0;i<n;i++)
printf("%s\t",a[i]);
printf("\n Enter student name to be search : ");
scanf("%s",sr);
Linear_search(a,n,sr);
getch();
}

Slip16_1 : Write a ‘C’ program to accept two polynomials and add these two polynomials
using function. Display the result (Use array).

#include<stdio.h>
#include<conio.h>
void add(int m,int a[10],int n,int b[10])
{
int c[10],i,cnt=0;
if(m>=n)
{ for(i=m;i>=0;i--)
{ c[i]=a[i]+b[i];
cnt++; }
Page 16
}
else
{ for(i=n;i>=0;i--)
{ c[i]=a[i]+b[i];
}
}
printf("\nResultant polynomial is = ");
for(i=cnt-1;i>0;i--)
{ printf("%dX^%d + ",c[i],i);
}
printf("%d",c[i]);
}
void main()
{ int a[10],b[10],i,m,n;
clrscr();
for(i=0;i<=9;i++)
a[i]=0;
for(i=0;i<=9;i++)
b[i]=0;
printf("\nEnter the order of first Polynomial");
scanf("%d",&m);
printf("\nEnter the Co-efficient");
for(i=m;i>=0;i--)
{ scanf("%d",&a[i]);
}
printf("\nEnter the order of Second Polynomail");
scanf("%d",&n);
printf("\nEnter the Co-efficient");
for(i=n;i>=0;i--)
{ scanf("%d",&b[i]);
}
add(m,a,n,b);
getch();
}

Slip 17_1 : Write a ‘C’ program to sort elements of a singly linked list in ascending order
and display the sorted List.

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

Page 17
NODE *head=NULL;

void create()
{
int i,n;
NODE *t;
printf("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}
void sort()
{
NODE *p,*q;
int temp;
for(p=head;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;
}
}
}
}
main()
{ int n;
clrscr();

Page 18
create();
printf("\n Link list is : ");
display();
printf("\n After sorting Link list is = ");
sort();
display();
getch();
}

Slip18_1 and Slip25_1 : sort data using Quick sort

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int split(int a[],int lower,int upper)
{ int pivot,i,j,temp;
i=lower+1;
j=upper;
pivot=a[lower];
while(j>=i)
{ while(pivot>a[i])
i++;
while(pivot<a[j])
j--;
if(i<j)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[lower];
a[lower]=a[j];
a[j]=temp;
return j;
}
void quicksort(int z[],int left,int right)
{ int i;
if(right>left)
{ i=split(z,left,right);
quicksort(z,left,i-1);
quicksort(z,i+1,right);

Page 19
}
}
void main()
{ int a[20],i,n;
clrscr();
printf("\n Enter how many elements would you like to enter : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\n Enter data : ");
scanf("%d",&a[i]);
}
quicksort(a,0,n-1);
printf("\n The array after sorting is :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Slip19_1 : sort data using Merge sort

#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
void merge(int a[10],int l,int m,int u)
{ int c[10],i,j,k;
i=l;
j=m+1;
k=0;
while(i<=m && j<=u)
{ if(a[i]<a[j])
{ c[k]=a[i];
k++;
i++;
}
else
{ c[k]=a[j];
k++;j++;
}
}
while(i<=m)
{ c[k]=a[i];
i++;k++;
}

Page 20
while(j<=u)
{ c[k]=a[j];
k++;j++;
}
for(i=l,j=0;i<=u;i++,j++)
a[i]=c[j];
}
void merge_sort(int a[10],int i,int j)
{ int k=0;
if(i<j)
{ k=(i+j)/2;
merge_sort(a,i,k);
merge_sort(a,k+1,j);
merge(a,i,k,j);
}
}
void main()
{ int a[100],i,n;
clrscr();
printf("\n Enter how many elements would you like to enter:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("\n Enter data:");
scanf("%d",&a[i]);
}
merge_sort(a,0,n-1);
printf("\n The array after sorting is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Slip 20_1Write a ‘C’ program to read a postfix expression, evaluate it and display the
result.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
#define SIZE 20
int stk[SIZE];
int top;
void init()
{ top=-1;

}
int isfull()
{ if(top==SIZE-1)

Page 21
return 1;
else
return 0;
}
int isEmpty()
{ if(top==-1)
return 1;
else
return 0;
}
void push(int c)
{ if(isfull())
{ printf("Stack is overflow"); }
else
{ top++;
stk[top]=c;
}
}
int pop()
{ if(isEmpty())
{ printf("Stack is empty"); }
else
{ int c;
c=stk[top];
top--;
return c;
}
}
void main()
{ int i,op1,op2,ans;
char s[30];
clrscr();
printf("\nEnter the Postfix Expr");
gets(s);
init();
for(i=0;s[i]!='\0';i++)
{
if(s[i]<='9' && s[i]>='0')
push(s[i]-48); push(atoi(s[i]));
else
{ op2=pop();
op1=pop();
switch(s[i])
{
case '+': push(op1+op2);
break;

Page 22
case '-': push(op1-op2);
break;
case '/': push(op1/op2);
break;
case '*': push(op1*op2);
break;
case '^': push(pow(op1^op2));
break;
case '%':push(op1%op2);
break;
}
}
} end of for
ans=pop();
printf("\n Result is %d ",ans);
getch();

}
Slip 21_1create two singly linked lists and concatenate one list at the end of another list.

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

struct node* create(int n)


{ int i;
struct node *f,*t;
f=(struct node *)malloc(sizeof(struct node));
printf("\n Enter node 1: ");
scanf("%d",&f->data);
t=f;
for(i=1;i<n;i++)
{
t->next=(struct node *)malloc(sizeof(struct node));
t=t->next;
printf("\n Enter node %d: ",i);
scanf("%d",&t->data);
} t->next=NULL;
return f;
}
void display(struct node* f)

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

struct node* concat(struct node* f1,struct node* f2)


{ struct node *t;
for(t=f1;t->next!=NULL;t=t->next);
t->next=f2;
return f1;
}
void main()
{ int no,num;
struct node *h1,*h2,*h3;
clrscr();
printf("\n Enter no of nodes for 1st link");
scanf("%d",&no);
h1=create(no);
display(h1);
printf("\n Enter no of nods for 2nd link");
scanf("%d",&num);
h2=create(num);
printf("\n Concatination is ");
h3=concat(h1,h2);
display(h3);
getch();
}

Slip22_1 : create a singly link list and delete last node and insert it into first 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;

Page 24
printf("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}
void delins()
{ int pos=0,i,no;
struct node *t,*s,*nw;
for(s=head;s!=NULL;s=s->next)
{ pos++;
}
for(t=head,i=1;i<pos-1;t=t->next,i++);
s=t->next;
no=s->data;
t->next=NULL;
free(s);
nw=(NODE *)malloc(sizeof(NODE));
nw->data=no;
nw->next=head;
head=nw;

}
void main()
{ clrscr();
create();
display();
delins();
printf("\n After performing operation \n");

Page 25
display();
getch();
}

Slip23_1 : Write menu driven program using ‘C’ for Static implementation of Stack. The
menu includes i)Insert ii) Delete iii)Display

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define SIZE 40
int stk[SIZE];
int top;
void init()
{ top=-1;
}

int isfull()
{ if(top==SIZE-1)
return 1;
else
return 0;
}
int isempty()
{ if(top==-1)
return 1;
else
return 0;
}
void push(int c)
{ if(isfull())
{ printf("Stack is overflow");
} else
{ top++;
stk[top]=c;
}
}
int pop()
{ if(isempty())
{ printf("Stack is empty");
} else
{ int c;
c=stk[top];

Page 26
top--;
return c;
}
}
void main()
{ int ch,i,n;
init();
do
{ printf("\n 1.Insert \n 2.Delete \n 3.Display \n 4.Exit");
printf("\n Enter choice ");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("\n Enter element ");
scanf("%d",&n);
push(n);
break;

case 2: printf("\n Deleted data is =%d",pop());


break;
case 3: for(i=top;i>=0;i--)
printf("%d\t",stk[i]);
break;
case 4:break;
}
} while(ch!=4);
}
Slip24_1 :count all non-zero elements, odd numbers and even numbers in the singly
linked list.

#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("\nHow Many Nodes you Want to Create: ");
scanf("%d",&n);

Page 27
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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}

void count()
{ NODE *t;
int nz=0,ec=0,oc=0;
for(t=head;t!=NULL;t=t->next)
{ if(t->data>0)
nz++;
if(t->data%2==0)
ec++;
else
oc++;
}

printf("\n Non-zero elements =%d, \n odd numbers =%d \n even numbers


=%d",nz,ec,oc);
}
void main()
{ int n;
clrscr();
create();
printf("\n Link list is : ");
display();
count();
getch();
}

Page 28
Slip26_1 : Circular Doubly link list

#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=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data");
scanf("%d",&head->data);
t=head;
for(i=2;i<=no;i++)
{ t->next=(struct node*)malloc(sizeof(struct node));
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("%d\t",t->data);
t=t->next;
}while(t!=head);
}
void main()
{
clrscr();

create();
display();

Page 29
getch();
}

Slip27_1 :swap mth and nth element of singly linked list.

#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("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}

void swap()

Page 30
{
int m,n,i,tmp;
struct node *q,*ptr,*ptr1;
printf("\nEnter the mth and nth position");
scanf("%d%d",&m,&n);
for(i=1,ptr=head;i<m && ptr!=NULL;i++);
ptr=ptr->next;
for(i=1,ptr1=head;i<n && ptr1!=NULL;ptr1=ptr1->next,i++);
if(ptr!=NULL && ptr1!=NULL)
{ tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{ printf("\nPosition Not Found");
}
}

void main()
{ int no,num;
struct node *h,*h1;
clrscr();
create();
display();
swap();
display();
getch();
}

Slip28_1 : Doubly link list

#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);

Page 31
head=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data");
scanf("%d",&head->data);
head->prev=NULL;
t=head;
for(i=2;i<=no;i++)
{ t->next=(struct node*)malloc(sizeof(struct node));
t->next->prev=t;
t=t->next;
printf("\n Enter data");
scanf("%d",&t->data);
t->next=NULL;
}
t->next=NULL;
}
void display()
{ NODE *t;
for(t=head;t!=NULL;t=t->next)
printf("%d\t",t->data);
}
void main()
{
clrscr();

create();
display();
getch();
}

Slip 29_1 :Write a ‘C’ program to create to a Singly linked list. Accept the number from
user, search the number in the list .If the number is present delete the node from the list
and add it at the beginning and display the list .If node not present print the message
“Node not Found”.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *head=NULL;

Page 32
void create()
{
int i,n;
NODE *t;
printf("\nHow 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()
{
NODE *t;
for(t=head;t!=NULL;t=t->next)
{ printf("\t %d ->",t->data); }
}

void delete()
{
struct node *s,*temp,*nw;
int cnt=0,i,sr,no,p=0,flag=0;
printf("\n Node to be Search : ");
scanf("%d",&sr);
for(s=head;s!=NULL;s=s->next)
{cnt++;}
for(s=head;s!=NULL;s=s->next)
{ p++;
if(sr==s->data)
{flag=1;
break;}
}
if(flag==0)
{
printf("Data Not Found = %d ",sr);

Page 33
}
else
{
if(p==1)
{}
else
{
for(i=1,s=head;i<p-1;i++,s=s->next);
temp=s->next;
no=temp->data;
s->next=temp->next;
nw=(NODE *)malloc(sizeof(NODE));
nw->data=no;
nw->next=head;
head=nw;
}
}
display();
/*while(cnt!=0)
{ for(t=head,i=1;i<=cnt-1;i++)
{ t=t->next; }
printf("%d ->\t",t->data);
cnt--;
}*/
}
void main()
{ int n;
clrscr();
create();
printf("\n Link list is : ");
display();
delete();
getch();
}

Slip30_1 : accept the details of employees from user and display it on the screen using
Dynamic Memory Allocation.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{ int eno;
char ename[20],eaddr[20];

Page 34
struct node *next;
}NODE;
NODE *head=NULL;

void create()
{ NODE *t;
int i,no;
printf("enter total no of employees");
scanf("%d",&no);

head=(NODE *)malloc(sizeof(NODE));
printf("\n Enter eno, ename and eaddr");
scanf("%d %s %s",&head->eno,head->ename,head->eaddr);
t=head;
for(i=2;i<=no;i++)
{ t->next=(NODE*)malloc(sizeof(NODE));
t=t->next;
printf("\n Enter eno, ename and eaddr");
scanf("%d %s %s",&t->eno,t->ename,t->eaddr);
}
t->next=NULL;
}
void display()
{ NODE *t;
for(t=head;t!=NULL;t=t->next)
{
printf("\n");
printf("%d\t%s\t%s",t->eno,t->ename,t->eaddr);
}
}
void main()
{
clrscr();
create();
display();
getch();
}

Page 35

You might also like