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

DS Lab Manual

Data structure lab experiment list

Uploaded by

kanchana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DS Lab Manual

Data structure lab experiment list

Uploaded by

kanchana
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Data Structures using C – Lab Manual

Data Structure Lab Program List for 1 Year BCA, Davanagere


University.

1 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

2 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 1
#include<stdio.h>
#include<conio.h>
struct Employee
{
int Empno;
char name[20];
int age;
unsigned long salary;
};
void printEmployeeDetails();
void main()
{
clrscr();
printEmployeeDetails();
getch();
}
void printEmployeeDetails()
{
int n,i;
struct Employee empArray[50];
printf("Enter the number of Employee Details\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d employee Empno\n",(i+1));
scanf("%d",&empArray[i].Empno);
printf("Enter the %d employee Name\n",(i+1));
scanf("%s",&empArray[i].name);
printf("Enter the %d employee Age\n",(i+1));
scanf("%d",&empArray[i].age);
printf("Enter the %d employee Salary\n",(i+1));
scanf("%lu",&empArray[i].salary);
}
printf("___________________________________\n");
printf("Empno\tName\tAge\tSalary\n");
printf("___________________________________\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\t%lu\n",empArray[i].Empno,empArray[i].name,
empArray[i].age,empArray[i].salary);
}

3 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

OUTPUT:

4 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 2

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int *ptra,*ptrb;
//int* ptrb;
clrscr();
printf("Enter the value for a and b");
scanf("%d%d",&a,&b);
ptra = &a;
ptrb = &b;
printf("Sum is %d\n",(*ptra + *ptrb));
printf("Difference is %d\n",(*ptra - *ptrb));
printf("Product is %d\n",(*ptra * *ptrb));
printf("Quotient is %d\n",(*ptra / *ptrb));
getch();
}

OUTPUT:

5 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 3

#include<stdio.h>
void main()
{
FILE *fp;
int i,n;
int rollNo, mark1,mark2,total,avg;
char name[10],filename[10];
clrscr();
printf("Input file name\n");
scanf("%s",filename);
fp= fopen(filename, "w");
printf("Enter the no student details\n\n");
scanf("%d",&n);
printf("Enter the %d details of student\n",n);

for(i=1;i<=n;i++)
{
printf("Enter %d student details(Rollno,Name,Marks1,Marks2)\n", i);
// Readinng userinput from keyboard
scanf("%d %s %d %d",&rollNo,name,&mark1,&mark2);
//fscanf(stdin,"%d %s %d %d",&rollNo,name,&mark1,&mark2);
// Writing to the file and data for writing comes from stdin.
fprintf(fp,"%d %s %d %d\n",rollNo,name,mark1,mark2);
}
fclose(fp);
printf("\n\n");
//fprintf(stdout,"\n\n");
fp = fopen(filename,"r");
printf("RollNo\t Name\t Mark1\t Mark2\t Total\t Average\n");
for(i=1;i<=n;i++)
{
// Readinng data from file
fscanf(fp,"%d %s %d %d",&rollNo,name,&mark1,&mark2);
total = mark1 + mark2;
avg = (mark1 + mark2) / 2;
// Printing data on console, the data was red from file through scanf.

//fprintf(stdout,"%d\t%s\t%d\t%d\t%d\t%d\n",rollNo,name,mark1,mark2,total,a
vg);

printf("%d\t%s\t%d\t%d\t%d\t%d\n",rollNo,name,mark1,mark2,total,avg);
}
fclose(fp);
getch();
}

6 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

OUTPUT:

7 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 4

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int chars=0,words=1,spaces=0;
clrscr();
fp = fopen("demo.txt", "w");// Opening file in write mode.
printf("Enter text in to the file\n");
while( (ch = getchar()) != EOF)//Looping until reading EOF(End of file)
{
putc(ch,fp);
}
fclose(fp);// Closing the file.
fp = fopen("demo.txt", "r");//Opening file in read mode
while((ch = getc(fp))!= EOF)//Looping until EOF
{
if(ch == ' ') spaces++;
if(ch != ' ') chars++;
if(ch== ' ' || ch=='\n' || ch=='\t') words++;
}
printf("Space count %d",spaces);
printf("Character count %d",chars);
printf("Words count %d",words);

fclose(fp);//Closing the file after reading.


getch();
}

Note : Need to type ctrl + Z or F6 (windows) or ctrl + D(Unix) after giving the input
to specify the end of line.

OUTPUT:

8 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 5

#define MAXSIZE 10
#include<stdio.h>
#include<conio.h>
struct Stack
{
int top;
int a[MAXSIZE];
}s;
void main()
{
void push(int);
void display();
int pop();
int item,choice;
s.top = -1;
clrscr();
while(1)
{
printf("\n******Stack Operations*****");
printf("\n 1.Push");
printf("\n 2.Pop");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element to be push\n");
scanf("%d",&item);
push(item);
break;
case 2: item = pop();
if(item != (-1))
printf("Poped element is %d\n",item);
break;
case 3: printf("Elements of the Stack are\n");
display();
break;
case 4: exit();
default : printf("Error in choice\n");

}
}
}

9 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

void push(int item)


{
if(s.top == (MAXSIZE-1))
{
printf("Stack is full\n");
return;
}
else
{
s.top++;
s.a[s.top]= item;
}
return;
}
void display()
{
int localTop = s.top;
if(localTop == -1)
{
printf("Stack is empty, nothing to display");
return;
}
else
{
printf("Elements of array\n");
while(localTop != -1)
{
printf("%d\n",s.a[localTop]);
localTop--;
}
return;
}
}
int pop()
{
int item;
if(s.top == -1)
{
printf("No elements in stack,leads to underflow of stack");
return(-1);
}
else
{
item = s.a[s.top];
s.top--;
}
return(item);
}

10 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

OUTPUT 1:

For Push

OUTPUT 2:

For Pop:

OUTPUT 3:

For Display :

11 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 6

# include <string.h>
char postfix[50];
char infix[50];
char opstack[50]; /* operator stack */
int i, j, top = 0;
int lesspriority(char op, char op_at_stack)
{
int k;
int pv1; /* priority value of op */
int pv2; /* priority value of op_at_stack */
char operators[] = {'+', '-', '*', '/', '%', '^', '(' };
//int priority_value[] = {0,0,1,1,2,3,4};
int priority_value[] = {1,1,2,2,3,4,5};
if( op_at_stack == '(' )
return 0;
for(k = 0; k < 6; k ++)
{
if(op == operators[k])
pv1 = priority_value[k];
}
for(k = 0; k < 6; k ++)
{
if(op_at_stack== operators[k])
pv2 = priority_value[k];
}
if(pv1 < pv2)
return 1;
else
return 0;
}

void push(char op) /* op - operator */


{
/* before pushing the operator
'op' into the stack check priority
of op with top of opstack if less
then pop the operator from stack
then push into postfix string else
push op onto stack itself */
if(top == 0)
{
opstack[top] = op;
top++;
}
else
{
if(op != '(' )
{

12 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

while(lesspriority(op, opstack[top-1]) == 1 && top > 0)


{
postfix[j] = opstack[--top];
j++;
}
}
opstack[top] = op; /* pushing onto stack */
top++;
}
}
void pop()
{
while(opstack[--top] != '(' ) /* pop until '(' comes */
{
postfix[j] = opstack[top];
j++;
}
}
void main()
{
char ch;
clrscr();
printf("\n Enter Infix Expression : ");
gets(infix);
while( (ch=infix[i++]) != '\0')
{
switch(ch)
{
case ' ' : break;
case '(' :
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
case '%' :
push(ch); /* check priority and push */
break;
case ')' :
pop();
break;
default :
postfix[j] = ch;
j++;
}
}
while(top >= 0)
{
postfix[j] = opstack[--top];
j++;

13 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

}
postfix[j] = '\0';
printf("\n Infix Expression : %s ", infix);
printf("\n Postfix Expression : %s ", postfix);
getch();
}

OUTPUT 1:

OUTPUT 2:

14 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 7

#include<stdio.h>
#include<conio.h>
#define MAX 6
void insertQ();
void deleteQ();
void displayQ();
struct Queue
{
int Q[MAX];
int front,rear;
}s;
void main()
{
int choice;
s.front = -1;
s.rear = -1;
clrscr();
while(1)
{

printf("\n Queue operations using ARRAY..");


printf("\n -----------**********-------------\n");
printf("\n 1. Insert ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice:\n ");
scanf("%d",&choice);
switch(choice)
{
case 1:insertQ();break;
case 2:deleteQ();break;
case 3:displayQ();break;
case 4:exit();
default : printf("Error in choice\n");

}
}
}

void insertQ()
{
int data;
if(s.rear == MAX-1)
{
printf("\n Queue is full");
return;

15 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

}
else
{
if(s.front == -1) s.front++;
printf("\n Enter data: ");
scanf("%d", &data);
s.rear++;
s.Q[s.rear] = data;
}
}
void deleteQ()
{
if(s.front == -1)
{
printf("\nQueue is Underflow..");
return;
}
else
{

printf("\n Deleted element from Queue is %d", s.Q[s.front]);


if(s.front == s.rear)
s.front = s.rear = -1;
else
s.front++;
}
}
void displayQ()
{
int i;
if(s.front == -1)
{
printf("\n\n\t Queue is Empty");
}
else
{
printf("\n Elements in Queue are: ");
for(i = s.front; i <=s.rear; i++)
{
printf("%d\t", s.Q[i]);
}
}
}

16 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

OUTPUT :
For Insertion

For Display

For Delete

17 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 8

#include<stdio.h>
#include<conio.h>
struct CircularQueue
{
int cq[10],i,n,e,ch;
int f;
int r;
}s;
void cqInsert();
void cqDelete();
void cqDisplay();
void main()
{
s.f=-1;
s.r=-1;
clrscr();
printf("Enter the size of the queue:\n");
scanf("%d",&s.n);
while(1)
{
printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the choice:\n");
scanf("%d",&s.ch);
switch(s.ch)
{
case 1: cqInsert();
break;
case 2: cqDelete();
break;
case 3: cqDisplay();
break;
case 4: exit(0);break;
}
}
}
void cqInsert()
{
if(s.f==(s.r+1)%s.n)
{
printf("Queue is full,leads to overflow\n");
return;
}
printf("Enter the element to insert:\n");
scanf("%d",&s.e);
s.r=(s.r+1)%s.n;
s.cq[r] = s.e;
if(f==-1)
f++;

18 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

}
void cqDelete()
{
if(s.f==-1)
{
printf("Queue is empty,leads to underflow\n");
return;
}
printf("The deleted element is:%d\s.n",s.cq[f]);
if(s.f==s.r)
s.f=s.r=-1;
else
s.f=(s.f+1)%s.n;
}
void cqDisplay()
{
if(s.f==-1)
{
printf("Queue is empty,nothing to display\n");
return;
}
printf("Elements of queue:\n");
for(s.i=s.f;s.i<=s.r;s.i++)
printf("%d\t",s.cq[s.i]);
if(s.f>s.r)
{
for(s.i=s.f;s.i<s.n;s.i++)
printf("%d\t",s.cq[s.i]);
for(s.i=0;s.i<=r;s.i++)
printf("%d\t",s.cq[s.i]);
}
}

OUTPUT:

For Insert:

19 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

For Display:

For Delete:

20 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 9

#include<stdio.h>
#include<stdlib.h>
#define MAX 7
struct DQueue
{
int deque_arr[MAX];
int f;
int r;
}dq;

void insert_frontEnd(int item);


void insert_rearEnd(int item);
int delete_frontEnd();
int delete_rearEnd();
void display();
int isEmpty();
int isFull();

main()
{
int choice,item;
dq.f=-1;
dq.r=-1;
clrscr();
while(1)
{
printf("1.Insert at the front end\n");
printf("2.Insert at the rear end\n");
printf("3.Delete from front end\n");
printf("4.Delete from rear end\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Input the element for adding in queue : ");
scanf("%d",&item);
insert_frontEnd(item);
break;
case 2:
printf("Input the element for adding in queue : ");
scanf("%d",&item);
insert_rearEnd(item);
break;
case 3:

21 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

printf("Element deleted from front end is : %d\n",delete_frontEnd());


break;
case 4:
printf("Element deleted from rear end is : %d\n",delete_rearEnd());
break;
case 5:
display();
break;
case 6:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
printf("************************************\n");
//printf("front = %d, rear =%d\n", dq.f , dq.r);
display();
printf("************************************\n");
}/*End of while*/
}/*End of main()*/

void insert_frontEnd(int item)


{
if( isFull() )
{
printf("Queue Overflow\n");
return;
}
if( dq.f==-1 )/*If queue is initially empty*/
{
dq.f=0;
dq.r=0;
}
else if(dq.f==0)
dq.f=MAX-1;
else
dq.f=dq.f-1;
dq.deque_arr[dq.f]=item ;
}/*End of insert_frontEnd()*/

void insert_rearEnd(int item)


{
if( isFull() )
{
printf("Queue Overflow\n");
return;
}
if(dq.f==-1) /*if queue is initially empty*/
{
dq.f=0;
dq.r=0;

22 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

}
else if(dq.r==MAX-1) /*rear is at last position of queue */
dq.r=0;
else
dq.r=dq.r+1;
dq.deque_arr[dq.r]=item ;
}/*End of insert_rearEnd()*/

int delete_frontEnd()
{
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
item=dq.deque_arr[dq.f];
if(dq.f==dq.r) /*Queue has only one element */
{
dq.f=-1;
dq.r=-1;
}
else
if(dq.f==MAX-1)
dq.f=0;
else
dq.f=dq.f+1;
return item;
}/*End of delete_frontEnd()*/

int delete_rearEnd()
{
int item;
if( isEmpty() )
{
printf("Queue Underflow\n");
exit(1);
}
item=dq.deque_arr[dq.r];

if(dq.f==dq.r) /*queue has only one element*/


{
dq.f=-1;
dq.r=-1;
}
else if(dq.r==0)
dq.r=MAX-1;
else
dq.r=dq.r-1;
return item;

23 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

}/*End of delete_rearEnd() */

int isFull()
{
if ( (dq.f==0 && dq.r==MAX-1) || (dq.f==dq.r+1) )
return 1;
else
return 0;
}/*End of isFull()*/

int isEmpty()
{
if( dq.f == -1)
return 1;
else
return 0;
}/*End of isEmpty()*/

void display()
{
int i;
if( isEmpty() )
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
i=dq.f;
if( dq.f<=dq.r )
{
while(i<=dq.r)
printf("%d ",dq.deque_arr[i++]);
}
else
{
while(i<=MAX-1)
printf("%d ",dq.deque_arr[i++]);
i=0;
while(i<=dq.r)
printf("%d ",dq.deque_arr[i++]);
}
printf("\n");
}/*End of display() */

24 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

OUTPUT:

Insertion from front end:

Insertion from rear end:

Deletion from front end:

Deletion from rear end:

25 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 10

void main()
{
int n,r,ncr;
clrscr();
printf("Enter values of n and r\n");
scanf("%d%d",&n,&r);
ncr=recbicoff(n,r);
printf("Binomial coefficient of c(%d,%d) is %d",n,r,ncr);
getch();
}
int recbicoff(int x,int y)
{
if(y>x)
{
printf("The value of n can't be less than r");
getch();
exit(1);
}
if(y==0||y==x)
{
return(1);
}
else
{
return(recbicoff(x-1,y-1)+recbicoff(x-1,y));
}
}

OUTPUT:

26 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 11

#include<stdio.h>
int genFibSeries(int);
void main()
{
int n, i = 0, c;
clrscr();
printf("Enter the limit\n");
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c = 1 ; c <= n ; c++ )
{
printf("%d\n", genFibSeries(i));
i++;
}
getch();
}

int genFibSeries(int n)
{
if (n==0)
return 0;
else if (n==1)
return 1;
else
return (genFibSeries(n-1) + genFibSeries(n-2));
}

OUTPUT:

27 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 12

#include <stdio.h>
#include <conio.h>
void towers_of_hanoi (int n, char *a, char *b, char *c);
void main (void)
{
int n;
clrscr();
printf("Enter number of discs:\n");
scanf("%d",&n);
towers_of_hanoi (n, "Tower 1", "Tower 2", "Tower 3");
getch();
}
void towers_of_hanoi (int n, char *a, char *b, char *c)
{
if (n == 1)
{

printf ("\nMove disk 1 from %s to %s", a, c);


return;
}
else
{
towers_of_hanoi (n-1, a, c, b);
printf ("\nMove disk %d from %s to %s",n, a, c);
towers_of_hanoi (n-1, b, a, c);
return;
}
}

OUTPUT:

28 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 13

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int regno;
char name[30];
struct node *link;
}*start;
void insert(int regno,char name[30]);
void display();
void search(int regno);
void deleteL(int regno);
void main()
{
int choice,regno;
char name[30];
int searchStatus;
clrscr();
while(1)
{
printf("\nLinked List operations\n");
printf("1.Insert\n2.Delete\n3.Search\n4.Display\n5.Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the regno and name of the student\n");
scanf("%d",&regno);
scanf("%s",name);
insert(regno,name);break;
case 2:printf("Enter the student regno to delete\n");
scanf("%d",&regno);
deleteL(regno);
display();break;
case 3:printf("Enter the student regno to search\n");
scanf("%d",&regno);
search(regno);
break;
case 4: display();break;
case 5: exit(1);break;
default : printf("Enter the correct choice\n");break;
}

}
}

void insert(int regno,char name[])

29 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

{
struct node *newnode,*ptr;
newnode = (struct node *)malloc(sizeof(struct node));
if(start == NULL)
{
newnode->regno = regno;
strcpy(newnode->name,name);
newnode->link = NULL;
start = newnode;
}
else
{
ptr = start;
while(ptr->link != NULL)
{
ptr=ptr->link;
}
newnode->regno = regno;
strcpy(newnode->name,name);
newnode->link = NULL;
ptr->link = newnode;
}
printf("Node added to the List");
}

void display()
{
struct node *ptr;
ptr = start;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked list contents are\n");
while(ptr!=NULL)
{
printf("%d,%s->",ptr->regno,ptr->name);
ptr=ptr->link;
}
printf("End of List");
}
void search(int regno)
{
struct node *ptr;
ptr = start;
if(ptr != NULL)
{
while(ptr->link != NULL)
{

30 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

if(ptr->regno == regno)
{
printf("Student with regno %d is found",regno);
return;
}

else
ptr = ptr->link;
}
if(ptr->regno == regno)
printf("Student with regno %d is found",regno);
else
printf("Student with regno %d is not found",regno);

}
else
{
printf("Student List is empty,nothing to search");
}

}
void deleteL(int regno)
{
struct node *ptr,*prev;
ptr=start;
prev=NULL;
if(ptr==NULL)
{
printf("\n Student Linked list is empty,nothing to delete\n");
return;
}
else if(start->regno==regno)
{
start=start->link;
printf("Student with regno: %d is deleted\n",regno);
free(ptr);
return;
}
while((ptr->regno!=regno)&&(ptr!=NULL))
{
prev=ptr;
ptr=ptr->link;
}
if(ptr==NULL)
printf("Student with %d is not found in the linked list,delete is not
possible\n",regno);
else
{
prev->link=ptr->link;
printf("Student with regno: %d is deleted\n",regno);

31 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

free(ptr);
}
return;
}

OUTPUT:
For Inserting

For Display

For Search

For Display

32 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 14

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *Llink;
struct node *Rlink;
}*start;
void insert(int data);
void display();
void deleteL(int data);
void main()
{
int choice,data;
int searchStatus;
clrscr();
while(1)
{
printf("\nDoubly Linked List operations\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the data to insert\n");
scanf("%d",&data);
insert(data);break;
case 2:printf("Enter the data to delete\n");
scanf("%d",&data);
deleteL(data);break;
case 3: display();break;
case 4: exit(1);break;
default : printf("Enter the correct choice\n");break;
}

}
}

void insert(int data)


{
struct node *newnode,*ptr;
newnode = (struct node *)malloc(sizeof(struct node));
if(start == NULL)
{
newnode->info = data;
newnode->Llink = NULL;
newnode->Rlink = NULL;

33 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

start = newnode;
}
else
{
ptr = start;
while(ptr->Rlink != NULL)
{
ptr=ptr->Rlink;
}
newnode->info = data;
newnode->Rlink = NULL;
newnode->Llink = ptr;
ptr->Rlink = newnode;
}
printf("List created\n");
}

void display()
{
struct node *ptr;
ptr = start;
if(start == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked list contents are\n");
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr=ptr->Rlink;
}
printf("End of List");
}
void deleteL(int data)
{
struct node *ptr,*prev,*next;
ptr=start;
prev=NULL;
if(ptr==NULL)
{
printf("\n Linked list is empty,nothing to delete\n");
return;
}
else if(start->info==data)
{
start=start->Rlink;
start->Llink=NULL;
free(ptr);
printf("%d is deleted\n",data);

34 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

display();
return;
}
while((ptr->info!=data)&&(ptr!=NULL))
{
prev=ptr;
ptr=ptr->Rlink;
}
if(ptr==NULL)
printf("Data %d is not found in the linked list,delete is not possible\n",data);
else
{
//updating previous link
prev->Rlink=ptr->Rlink;
/*updating next link,
by storing next node addrees in next and updated next node Llink*/
next = ptr->Rlink;
next->Llink = ptr->Llink;
free(ptr);
printf("%d is deleted\n",data);
display();
}
return;
}

OUTPUT:

For Insertion:

For Display

35 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

For Delete

36 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

Program 15

/*implementation of tree traversal*/


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
typedef struct node btnode;
//btnode getnode();
btnode *tree;
btnode * insert(btnode *tree,int ele);
void main()
{
int choice;
int element;
clrscr();
tree=NULL;
printf("\n Enter the elements of tree(type 0 to stop)\n");
scanf("%d",&element);

while(element!=0)
{
tree=insert(tree,element);
scanf("%d",&element);
}
printf("\n Inorder traversal:");
inorder(tree);
printf("\n Preorder traversal:");
preorder(tree);
printf("\n Post order traversal:");
postorder(tree);
getch();
}
/*function to insert a node at front*/
btnode * insert(btnode *tree,int ele)
{
btnode *p,*q,*r;
p=(btnode*)malloc(sizeof(btnode));
p->data=ele;
p->lchild=p->rchild=NULL;
if(tree==NULL)
{
return(p);
}
else

37 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

{
q=NULL;
r=tree;
while(r!=NULL)
{
q=r;
if(ele==r->data)
{
printf("\n no duplicate elements");
free(p);
return(tree);
}
if(ele < r->data)
r=r->lchild;
else
r=r->rchild;
}
if(ele < q->data)
q->lchild=p;
else
q->rchild=p;
}
return(tree);
}
/*inorder traversal*/
inorder(btnode *tree)
{
if(tree!=NULL)
{
inorder(tree->lchild);
printf("\t%d",tree->data);
inorder(tree->rchild);
}
return;
}
/*preorder traversal*/
preorder(btnode *tree)
{
if(tree!=NULL)
{
printf("\t %d",tree->data);
preorder(tree->lchild);
preorder(tree->rchild);
}
return;
}
/*postorder traversal*/
postorder(btnode *tree)
{
if(tree!=NULL)

38 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)


Data Structures using C – Lab Manual

{
postorder(tree->lchild);
postorder(tree->rchild);
printf("\t %d",tree->data);
}
return;
}

OUTPUT 1:

OUTPUT 2:

39 Raghu Gurumurthy, MCA (Id : [email protected], Phone no: 9060130871)

You might also like