data structure lab progrms

Post on 10-Apr-2018

231 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 1/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 1

Ex.No:1

MOVING NODES IN A LINKED LIST

AI M: 

Write a program such that there is a function “move” which moves a node forward by

n positions in a linked list.

PROGRAM:

# include<stdio.h>

# include<conio.h>

# include "malloc.h"

struct node

{

int data;

struct node *link;

};

void add(struct node **,int );

void display(struct node *);

void move(struct node **,int,int);

int count(struct node *);

void main()

{

int num,n,i,elem,posi;

struct node *ptr,*temp;

ptr=NULL;

clrscr();

printf("MOVE operation in Link list\n");

printf("---------------------------");

printf("\n");

printf("Enter no of elements:");

scanf("%d",&n);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 2/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 2

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

{

printf("Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr,num);

display(ptr);

printf("\n");

}

printf("Element to be moved:\n");

scanf("%d",&elem);

printf("Number of position to be moved forwardly:\n");

scanf("%d",&posi);

move(&ptr,elem,posi);

printf("\nList after MOVE operation:\n");

display(ptr);

getch();

}

void add(struct node **q,int num)

{

struct node *temp;

temp = *q;

if(*q==NULL)

{

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

temp = *q;

}

else

{

while((temp->link)!=NULL)

{

temp=temp->link;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 3/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 3

temp->link = malloc(sizeof(struct node));

temp=temp->link;

}

temp->data = num;

temp->link = NULL;

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

void move(struct node ** pt,int elem,int posi)

{

int pos=1,n=1,last;

struct node * temp,* temp1,*prev;

temp=*pt;

temp1=*pt;

while(temp->data!=elem)

{

prev=temp;

temp=temp->link;

++n;

}

while(pos!=(n-posi-1))

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 4/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 4

temp1=temp1->link;

++pos;

}

if((n-posi-1)==0)

{

prev->link=temp->link;

temp->link=*pt;

*pt=temp;

}

else

{

prev->link=temp->link;

temp->link=temp1->link;

temp1->link=temp;

}

}

int count(struct node * p)

{

int c=0;

while(p!=NULL)

{

p=p->link;

c++;

}

return c;

}

Output:

MOVE operation in Link list

-----------------------------------

Enter the number of elements : 6

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 5/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 5

Enter the element you want to add: 1

1

Enter the element you want to add: 4

1--> 4

Enter the element you want to add: 2

1--> 4--> 2

Enter the element you want to add: 6

1--> 4--> 2--> 6

Enter the element you want to add: 7

1--> 4--> 2--> 6--> 7

Enter the element you want to add: 3

1--> 4--> 2--> 6--> 7--> 3

Element to be moved:

7

Number of position to be moved forwardly:

3

List after MOVE operation:

1--> 7--> 4--> 2--> 6--> 3

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 6/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 6

Ex.No:2

GENERATE DOUBLY LINKED LIST FROM SINGLY LINKED LIST

AI M: 

Write a program to generate a doubly linked list from it‟s singly linked list

representation.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include"malloc.h"

struct node

{

int data;

struct node *prev,*link;

};

struct node1

{

int data;

struct node1 *link;

};

void add(struct node1 **,int);

void display(struct node *);

void generate(struct node **pt,struct node1 **pt1);

void display1(struct node1 *);

void main()

{

int num,n,i,elem,posi;

struct node *ptr,*temp;

struct node1 *ptr1;

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 7/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 7

ptr=NULL;

ptr1=NULL;

clrscr();

printf("Generation of Double link list from Single link list\n");

printf("----------------------------------------------------\n");

printf("\n");

printf("Enter the number of elements you want to insert in SINGLE link list:");

scanf("%d",&n);

printf("\n");

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

{

printf("Enter the element you want to add:");

scanf("%d",&num);

add(&ptr1,num);

display1(ptr1);

printf("\n");

}

generate(&ptr,&ptr1);

printf("\nAfter conversion from SINGLE to DOUBLE:\n");

printf("\n");

display(ptr);

getch();

}

void add(struct node1 ** pt,int num)

{

struct node1 *temp,*current,*previous;

current=malloc(sizeof(struct node));

current->data=num;

current->link=NULL;

temp=*pt;

if(temp==NULL)

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 8/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 8

*pt=current;

}

else

{

while(temp->link!=NULL)

{

temp=temp->link;

}

temp->link=current;

}

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

printf("<--> %d",pt->data);

pt=pt->link;

}

}

void display1(struct node1 *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 9/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 9

void generate(struct node **pt,struct node1 **pt1)

{

int ele;

struct node *temp,*current;

struct node1 *temp1;

current=malloc(sizeof(struct node1));

current->link=NULL;

current->prev=NULL;

temp=*pt;

temp1=*pt1;

if(*pt1==NULL)

{

printf("Empty ");

}

else

{

ele=temp1->data;

current->data=ele;

temp=current;

temp1=temp1->link;

*pt=temp;

while(temp1)

{

ele=temp1->data;

temp->link=malloc(sizeof(struct node));

temp->link->prev=temp;

temp=temp->link;

temp->data=ele;

temp->link=NULL;

temp1=temp1->link;

}

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 10/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 10

Output:

Generation of Double link list from Single link list

-------------------------------------------------------------

Enter the number of elements you want to insert in SINGLE link list:5

Enter the element you want to add:34

34

Enter the element you want to add:56

34--> 56

Enter the element you want to add:11

34--> 56--> 11

Enter the element you want to add:75

34--> 56--> 11--> 75

Enter the element you want to add:22

34--> 56--> 11--> 75--> 22

After conversion from SINGLE to DOUBLE:

34<--> 56<--> 11<--> 75<--> 22

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 11/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 11

Ex.No:3

SEARCHING IN BST

AI M: 

Write a program to search for an element using the BST.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct bnode

{

int data;

struct bnode *lchild,*rchild;

};

void insert(struct bnode **,int);

void search(struct bnode **,int);

void inorder(struct bnode *);

void main()

{

int i,will,num,n;

struct bnode *root;

root=NULL;

clrscr();

will=1;

printf("\nElement search in BST");

printf("\n---------------------");

printf("\n");

printf("Enter the number of nodes in BST: ");

scanf("%d",&n);

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

{

printf(" Enter the element you want to add: ");

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 12/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 12

scanf("%d",&num);

insert(&root,num);

printf("\n");

}

printf("Nodes displayed in INORDER:\n");

inorder(root);

while(will)

{

printf("\nEnter the element you wants to find: ");

scanf("%d",&num);

search(&root,num);

printf("\n");

printf("\nDO you want to continue (press 1 for YES or 0 for quit):");

scanf("%d",&will);

}

getch();

}

void insert(struct bnode ** rt,int ele)

{

struct bnode *temp,*current;

current=malloc(sizeof(struct bnode));

current->data=ele;

current->lchild=NULL;

current->rchild=NULL;

temp=*rt;

if(*rt==NULL)

{

*rt=current;

}

else

{

while(temp!=NULL)

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 13/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 13

{

if(temp->data<ele)

{

if(temp->rchild==NULL)

{

temp->rchild=current;

temp=NULL;

}

else

{

temp=temp->rchild;

}

}

else

{

if(temp->lchild==NULL)

{

temp->lchild=current;

temp=NULL;

}

else

{

temp=temp->lchild;

}

}

}

}

}

void search(struct bnode **pt,int ele)

{

int flag=1;

struct bnode *temp,*prev;

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 14/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 14

temp=*pt;

while(temp!=NULL)

{

if(temp->data==ele)

{

if(temp==*pt)

{

printf("Element %d is found at root ",ele);

temp=NULL;

flag=0;

}

else

{

printf("\nElement %d is found\n",ele);

printf("\nParent e lement is:%d",prev->data);

temp=NULL;

flag=0;

}

}

else if(temp->data>ele)

{

prev=temp;

temp=temp->lchild;

}

else if(temp->data<ele)

{

prev=temp;

temp=temp->rchild;

}

}

if(flag==1)

{

printf("\nElement is not found");

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 15/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 15

}

return;

}

void inorder(struct bnode *rt)

{

if(rt!=NULL)

{

inorder(rt->lchild);

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

inorder(rt->rchild);

}

else

return;

}

Output:

Element search in BST

----------------------------

Enter the number of nodes in BST: 6

Enter the element you want to add: 5

Enter the element you want to add: 4

Enter the element you want to add: 6

Enter the element you want to add: 2

Enter the element you want to add: 7

Enter the element you want to add: 8

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 16/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 16

Nodes displayed in INORDER:

2 4 5 6 7 8

Enter the element you wants to find: 4

Element 4 is found

Parent element is: 5

DO you want to continue (press 1 for YES or 0 for quit):1

Enter the element you wants to find: 5

Element 5 is found at Root

DO you want to continue (press 1 for YES or 0 for quit):1

Enter the element you wants to find: 16

Sorry the element 16 was not found

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 17/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 17

Ex.No:4

INSERTION SORT USING LINKED LISTS

AI M: 

Write a program to implement insertion sort using linked lists.

PROGRAM:

# include<stdio.h>

# include<conio.h>

# include "malloc.h"

struct node

{

int data;

struct node *link;

};

void add(struct node **,int );

void display(struct node *);

void main()

{

int will,num;

clrscr();

struct node *ptr,*temp;

ptr=NULL;

will=1;

printf("Insertion Sort using link list\n");

printf("------------------------------");

printf("\n");

while(will==1)

{

printf(" Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr,num);

display(ptr);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 18/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 18

printf("\nDo you want to continue ( press 1 for YES or 0 for quit):");

scanf("%d",&will);

}

getch();

}

void add(struct node **q,int num)

{

struct node *temp,*prev,*current;

current=malloc(sizeof(struct node));

current->data=num;

current->link=NULL;

temp = *q;

if(*q==NULL)

{

*q=current;

temp = *q;

}

else

{

if(temp->data>num)

{

if(temp->link!=NULL)

{

current->link=temp;

*q=current;

}

else{

current->link=temp;

*q=current;

temp->link=NULL;

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 19/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 19

else

{

while((temp->link)!=NULL&&temp->data<num)

{

prev=temp;

temp=temp->link;

}

if(temp->link==NULL)

{

if(temp->data<num){

temp->link = current;

current->link=NULL;

}

else

{

current->link=temp;

prev->link=current;

}

}

else

{

current->link=temp;

prev->link=current;

}

}

}

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 20/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 20

{

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

pt=pt->link;

}

}

Output:

Insertion Sort using link list

---------------------------------

Enter the element you want to add: 5

5

Do you want to continue ( press 1 for YES or 0 for quit):1

Enter the element you want to add: 3

3--> 5

Do you want to continue ( press 1 for YES or 0 for quit):1

Enter the element you want to add: 2

2--> 3--> 5

Do you want to continue ( press 1 for YES or 0 for quit):1

Enter the element you want to add: 7

2--> 3--> 5--> 7

Do you want to continue ( press 1 for YES or 0 for quit):1

Enter the element you want to add: 6

2--> 3--> 5--> 6--> 7

Do you want to continue ( press 1 for YES or 0 for quit):1

Enter the element you want to add: 1

1--> 2--> 3--> 5--> 6--> 7

Do you want to continue ( press 1 for YES or 0 for quit):0

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 21/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 21

Ex.No:5

MANIPULATION IN A BINARY TREE

AI M: 

Write a program to do the following on a binary tree

i.  Count the number of nodes in the tree

ii.  Sum the contents of the nodes

PROGRAM:

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#include <math.h>

int count(int *a, int n1);

int sum(int *a,int n1);

void insert( int *a ,int ele,int pos);

void main()

{

int n,i,n1,pos,ele;

clrscr();

printf(" Manipulation in Binary Tree");

printf("\n------------------------");

printf("\n");

printf(" \nEnter the number of nodes in binary tree: ");

scanf(" %d",&n);

n1= pow(2,n)-1;

printf("\nThe maximum number of possible positions: %d\n",n1);

int *a = (int *) malloc (n1 * sizeof(int));

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

{

a[i]=0;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 22/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 22

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

{

printf("\nNodes position:");

scanf(" %d",&pos);

printf("\nElement:");

scanf(" %d",&ele);

insert(a,ele,pos);

}

printf("\nThe elements in the Binary Tree:\n");

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

{

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

}

printf("\nTotal number of nodes:%d\n",count(a, n1));

printf("\nThe SUM of the elements in the Binary tree: %d",sum(a, n1));

getch();

}

void insert( int *a ,int ele,int pos)

{

a[pos] = ele ;

}

int count(int *a,int n1)

{

int i;

int count=0;

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

{

if(a[i] != 0)

{

count++;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 23/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 23

}

return count;

}

int sum(int *a,int n1)

{

int i;

int count=0;

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

{

if(a[i] != 0)

{

count +=a[i];

}

}

return count;

}

Output:

Manipulation in Binary Tree

-----------------------------------

Enter the number of nodes in binary tree: 3

The maximum number of possible positions: 7

Nodes position: 1

Element: 5

Nodes position: 6

Element: 3

Nodes position: 3

Element: 7

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 24/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 24

The elements in the Binary Tree:

0 5 0 7 0 0 3

Total number of nodes: 3

The SUM of the elements in the Binary tree: 15

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 25/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 25

Ex.No:6 

LINEAR SEARCH IN DOUBLY LINKED LIST

AI M: 

Write a program to create a doubly linked list and perform search for an element „x‟

using linear search.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include"malloc.h"

struct node

{

int data;

struct node *prev,*link;

};

void add(struct node **,int);

void display(struct node *);

void search(struct node *,int);

void main()

{

int num,n,i,elem,posi;

struct node *ptr,*temp;

ptr=NULL;

clrscr();

printf("Linear search in Doubly link list\n");

printf("----------------------------------------");

printf("\n");

printf("Enter the number ofelements in list:");

scanf("%d",&n);

printf("\n");

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

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 26/69

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 27/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 27

}

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

void search(struct node *pt,int elem)

{

struct node * temp;

int count=1;

temp=pt;

while(temp->data!=elem && temp->link!=NULL)

{

temp= temp->link;

++count;

}

if(temp->data==elem)

{

printf("Element %d found at the position %d",elem,count);

}

else

{

printf("Sorry Element Not found");

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 28/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 28

Output:

Linear search in Doubly link list

---------------------------------------

Enter the number of elements in list:6

Enter the element you want to add:43

43

Enter the element you want to add:65

43--> 65

Enter the element you want to add:23

43--> 65--> 23

Enter the element you want to add:12

43--> 65--> 23--> 12

Enter the element you want to add:75

43--> 65--> 23--> 12--> 75

Enter the element you want to add:34

43--> 65--> 23--> 12--> 75--> 34

Enter the element to be searched: 75

Element 75 found at the position: 5

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 29/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 29

Ex.No:7 

REVERSING A SINGLY LINKED LIST

AI M: 

Write a program to create a singly linked list. Then using a function „reverse‟, reverse

the contents of the list.

PROGRAM:

# include<stdio.h>

# include<conio.h>

# include "malloc.h"

struct node

{

int data;

struct node *link;

};

void main()

{

int will,num,i;

struct node *ptr,*temp;

void add(struct node **,int );

void display(struct node *);

void reverse(struct node **);

ptr=NULL;

clrscr();

printf("Reverse the Link list\n");

printf("---------------------\n");

printf("\n");

printf("Enter the number of elements for list:");

scanf("%d",&will);

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

{

printf(" Enter the element you want to add: ");

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 30/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 30

scanf("%d",&num);

add(&ptr,num);

display(ptr);

printf("\n");

}

printf("\nAfter Reverse operation:\n");

reverse(&ptr);

printf("\n");

display(ptr);

getch();

}

void add(struct node **q,int num)

{

struct node *temp;

temp = *q;

if(*q==NULL)

{

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

temp = *q;

}

else

{

while((temp->link)!=NULL)

{

temp=temp->link;

}

temp->link = malloc(sizeof(struct node));

temp=temp->link;

}

temp->data = num;

temp->link = NULL;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 31/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 31

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

void reverse(struct node ** pt)

{

struct node *temp1=*pt;

struct node *next=NULL,*prev=NULL;

while(temp1)

{

*pt=temp1;

next=temp1->link;

temp1->link=prev;

prev=temp1;

temp1=next;

}

}

Output:

Reverse the Link list

-------------------------

Enter the number of elements for list:6

Enter the element you want to add: 34

34

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 32/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 32

Enter the element you want to add: 45

34--> 45

Enter the element you want to add: 12

34--> 45--> 12

Enter the element you want to add: 48

34--> 45--> 12--> 48

Enter the element you want to add: 63

34--> 45--> 12--> 48--> 63

Enter the element you want to add: 74

34--> 45--> 12--> 48--> 63--> 74

After Reverse operation:

74--> 63--> 48--> 12--> 45--> 34

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 33/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 33

Ex.No:8 

INORDER TRAVERSAL ON A BST

AI M: 

Write a program to insert elements on a binary search tree and display the result of an

inorder traversal on the tree.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct bnode

{

int data;

struct bnode *lchild,*rchild;

};

void insert(struct bnode **,int);

void inorder(struct bnode *);

void main()

{

int i,will,num,n;

struct bnode *root;

root=NULL;

clrscr();

printf("Inorder traversal in BST\n");

printf("------------------------\n");

printf("\n");

printf("Enter number of nodes for BST:");

scanf("%d",&n);

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

{

printf(" Enter the element you want to add:");

scanf("%d",&num);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 34/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 34

insert(&root,num);

printf("\n");

}

printf("Nodes displayed in INORDER traversal:\n");

printf("\n");

inorder(root);

getch();

}

void insert(struct bnode ** rt,int ele)

{

struct bnode *temp,*current;

current=malloc(sizeof(struct bnode));

current->data=ele;

current->lchild=NULL;

current->rchild=NULL;

temp=*rt;

if(*rt==NULL)

{

*rt=current;

}

else

{

while(temp!=NULL)

{

if(temp->data<ele)

{

if(temp->rchild==NULL)

{

temp->rchild=current;

temp=NULL;

}

else

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 35/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 35

{

temp=temp->rchild;

}

}

else

{

if(temp->lchild==NULL)

{

temp->lchild=current;

temp=NULL;

}

else

{

temp=temp->lchild;

}

}

}

}

}

void inorder(struct bnode *rt)

{

if(rt!=NULL)

{

inorder(rt->lchild);

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

inorder(rt->rchild);

}

else

return;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 36/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 36

Output:

Inorder traversal in BST

------------------------------

Enter the number of nodes for BST: 8

Enter the element you want to add: 35

Enter the element you want to add: 23

Enter the element you want to add: 60

Enter the element you want to add: 12

Enter the element you want to add: 80

Enter the element you want to add: 34

Enter the element you want to add: 24

Enter the element you want to add: 50

Nodes displayed in INORDER traversal:

12 23 24 34 35 50 60 80

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 37/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 37

Ex.No:9 

PREORDER TRAVERSAL ON A BST

AI M: 

Write a program to insert elements on a binary search tree and display the result of a

preorder traversal on the tree.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct bnode

{

int data;

struct bnode *lchild,*rchild;

};

void insert(struct bnode **,int);

void preorder(struct bnode *);

void main()

{

int i,will,num,n;

struct bnode *root;

root=NULL;

clrscr();

printf("Preorder traversal in BST\n");

printf("------------------------\n");

printf("\n");

printf("Enter number of nodes for BST:");

scanf("%d",&n);

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

{

printf(" Enter the element you want to add:");

scanf("%d",&num);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 38/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 38

insert(&root,num);

printf("\n");

}

printf("Nodes diaplayed in PREORDER traversal:\n");

printf("\n");

preorder(root);

getch();

}

void insert(struct bnode ** rt,int ele)

{

struct bnode *temp,*current;

current=malloc(sizeof(struct bnode));

current->data=ele;

current->lchild=NULL;

current->rchild=NULL;

temp=*rt;

if(*rt==NULL)

{

*rt=current;

}

else

{

while(temp!=NULL)

{

if(temp->data<ele)

{

if(temp->rchild==NULL)

{

temp->rchild=current;

temp=NULL;

}

else

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 39/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 39

{

temp=temp->rchild;

}

}

else

{

if(temp->lchild==NULL)

{

temp->lchild=current;

temp=NULL;

}

else

{

temp=temp->lchild;

}

}

}

}

}

void preorder(struct bnode *rt)

{

if(rt!=NULL)

{

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

preorder(rt->lchild);

preorder(rt->rchild);

}

else

return;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 40/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 40

Output:

Preorder traversal in BST

-------------------------------

Enter the number of nodes for BST: 8

Enter the element you want to add: 12

Enter the element you want to add: 45

Enter the element you want to add: 67

Enter the element you want to add: 34

Enter the element you want to add: 23

Enter the element you want to add: 11

Enter the element you want to add: 5

Enter the element you want to add: 1

Nodes displayed in PREORDER traversal:

12 11 5 1 45 34 23 67

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 41/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 41

Ex.No:10 

POSTORDER TRAVERSAL ON A BST

AI M: 

Write a program to insert elements on a binary search tree and display the result of a

postorder traversal on the tree.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct bnode

{

int data;

struct bnode *lchild,*rchild;

};

void insert(struct bnode **,int);

void postorder(struct bnode *);

void main()

{

int i,will,num,n;

struct bnode *root;

root=NULL;

clrscr();

printf("Postorder traversal in BST\n");

printf("------------------------\n");

printf("\n");

printf("Enter number of nodes for BST:");

scanf("%d",&n);

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

{

printf(" Enter the element you want to add:");

scanf("%d",&num);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 42/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 42

insert(&root,num);

printf("\n");

}

printf("Nodes displayed in POSTORDER travesal:\n");

printf("\n");

postorder(root);

getch();

}

void insert(struct bnode ** rt,int ele)

{

struct bnode *temp,*current;

current=malloc(sizeof(struct bnode));

current->data=ele;

current->lchild=NULL;

current->rchild=NULL;

temp=*rt;

if(*rt==NULL)

{

*rt=current;

}

else

{

while(temp!=NULL)

{

if(temp->data<ele)

{

if(temp->rchild==NULL)

{

temp->rchild=current;

temp=NULL;

}

else

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 43/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 43

{

temp=temp->rchild;

}

}

else

{

if(temp->lchild==NULL)

{

temp->lchild=current;

temp=NULL;

}

else

{

temp=temp->lchild;

}

}

}

}

}

void postorder(struct bnode *rt)

{

if(rt!=NULL)

{

postorder(rt->lchild);

postorder(rt->rchild);

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

}

else

return;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 44/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 44

Output:

Postorder traversal in BST

---------------------------------

Enter the number of nodes for BST: 8

Enter the element you want to add: 45

Enter the element you want to add: 12

Enter the element you want to add: 34

Enter the element you want to add: 50

Enter the element you want to add: 67

Enter the element you want to add: 58

Enter the element you want to add: 23

Enter the element you want to add: 90

Nodes displayed in POSTORDER traversal:

23 34 12 58 90 67 50 45

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 45/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 45

Ex.No:11 

MERGING LINKED LISTS

AI M: 

Write a program to combine two singly linked lists such that suppose one is {l1, l2,

l3, … ln} and the other is {M1, m2, … mn}, then the combined list is {l1, m1, l2, m2, …ln,

mn}.

PROGRAM:

# include<stdio.h>

# include<conio.h>

# include "malloc.h"

struct node

{

int data;

struct node *link;

};

void add(struct node **,int );

void display(struct node *);

void merge(struct node ** list1,struct node ** list2);

int count(struct node *);

void main()

{

int num,n,n2,i,j,elem,posi;

struct node *ptr,*ptr1,*temp;

ptr=NULL;

ptr1=NULL;

clrscr();

printf("Merge two Link lists\n");

printf("--------------------\n");

printf("\n");

printf("Enter no ofelements of List 1:");

scanf("%d",&n)

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 46/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 46

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

{

printf(" Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr,num);

display(ptr);

printf("\n");

}

printf("Enter no ofelements of List 2:");

scanf("%d",&n2);

for(j=1;j<=n2;j++)

{

printf(" Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr1,num);

display(ptr1);

printf("\n");

}

printf("\nAfter Merging of List 1 & List 2:\n");

printf("\n");

merge(&ptr,&ptr1);

display(ptr);

getch();

}

void add(struct node **q,int num)

{

struct node *temp;

temp = *q;

if(*q==NULL)

{

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

temp = *q;

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 47/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 47

}

else

{

while((temp->link)!=NULL)

{

temp=temp->link;

}

temp->link = malloc(sizeof(struct node));

temp=temp->link;

}

temp->data = num;

temp->link = NULL;

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

void merge(struct node ** list1,struct node ** list2)

{

int last;

struct node * temp1,* temp2,* temp1nxt,*temp2nxt,*prev;

temp1=*list1;

temp2=*list2;

prev=temp1;

while(temp1->link!=NULL && temp2->link!=NULL)

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 48/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 48

temp1nxt=temp1->link;

temp2nxt=temp2->link;

temp1->link=temp2;

temp2->link=temp1nxt;

temp1=temp1nxt;

temp2=temp2nxt;

}

if(temp1->link==NULL)

{

temp1->link=temp2;

}

else

{

temp1nxt=temp1->link;

temp1->link=temp2;

temp2->link=temp1nxt;

}

*list1=prev;

}

Output:

Merge two Link lists

--------------------------

Enter the number of elements of List 1: 5

Enter the element you want to add: 1

1

Enter the element you want to add: 3

1--> 3

Enter the element you want to add: 5

1--> 3--> 5

Enter the element you want to add: 7

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 49/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 49

1--> 3--> 5--> 7

Enter the element you want to add: 9

1--> 3--> 5--> 7--> 9

Enter the number of elements of List 2: 5

Enter the element you want to add: 2

2

Enter the element you want to add: 4

2--> 4

Enter the element you want to add: 6

2--> 4--> 6

Enter the element you want to add: 8

2--> 4--> 6--> 8

Enter the element you want to add: 10

2--> 4--> 6--> 8--> 10

After Merging of List 1 & List 2:

1--> 2--> 3--> 4--> 5--> 6--> 7--> 8--> 9--> 10

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 50/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 50

Ex.No:12 

DATABASE USING LINKED LISTS

AI M: 

Write a program that reads the name, age and salary of 10 persons and maintains them

in a linked list sorted by name.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

#include<string.h>

struct node

{

char name[20];

int age;

int salary;

struct node *next;

}*current,*temp,*start,*prev;

void insertnode(char [],int,int);

void main()

{

int i,*nam,a,s;

clrscr();

start=temp=prev=current=NULL;

printf("Database using Link List\n");

printf("------------------------\n");

printf("\n");

printf("\nEnter the Details");

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

{

printf("\nName of the employee: ");

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 51/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 51

scanf("%s",nam);

printf("\nAge: ");

scanf("%d",&a);

printf("\nSalary: ");

scanf("%d",&s);

insertnode(nam,a,s);

}

temp=start;

printf("\nNAME AGE SALARY");

printf("\n------------------");

while(temp!=NULL)

{

printf("\n%s %d %d",temp->name,temp->age,temp->salary);

temp=temp->next;

}

getch();

}

void insertnode(char nam[],int a,int s)

{

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

strcpy(current->name,nam);

current->age=a;

current->salary=s;

current->next=NULL;

if(start==NULL)

{

start=current;

}

else

{

temp=start;

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 52/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 52

while((strcmp(temp->name,current->name))<0)

{

if(temp->next==NULL)

{

temp->next=current;

break;

}

else

{

prev=temp;

temp=temp->next;

}

}

if(strcmp(temp->name,current->name)>0)

{

if(temp==start)

{

current->next=start;

start=current;

}

else

{

current->next=temp;

prev->next=current;

}

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 53/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 53

Output:

Database using Link List

------------------------------

Name of the employee: suresh

Age: 23

Salary: 4500

Name of the employee: jeffe

Age: 22

Salary: 4200

Name of the employee: arul

Age: 24

Salary: 5000

Name of the employee: zamil

Age: 23

Salary: 4500

Name of the employee: berry

Age: 24

Salary: 5000

Name of the employee: sachin

Age: 35

Salary: 4580

Name of the employee: david

Age: 23

Salary: 6000

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 54/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 54

Name of the employee: ruban

Age: 23

Salary: 3565

Name of the employee: viki

Age: 23

Salary: 5342

Name of the employee: jose

Age: 34

Salary: 6543

NAME AGE SALARY

---------- ------- -------------

arul 24 5000

berry 24 5000

david 23 6000

  jeffe 22 4200

  jose 34 6543

ruban 23 3565

sachin 35 4580

suresh 23 4500

viki 23 5342

zamil 23 4500

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 55/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 55

Ex.No:13 

SIMILAR BINARY TREES

AI M: 

Write a program to determine whether or not one binary tree is similar to some other

binary tree.

PROGRAM:

#include <stdio.h>

#include <conio.h>

#include <malloc.h>

#include <math.h>

void similar(int *a,int n1,int *b,int n2);

void insert( int *a ,int ele,int pos);

void main()

{

int n,i,n1,pos,ele;

int bin,bin2;

clrscr();

printf("\n Checking of similar Binary Trees");

printf("\n---------------------------------");

printf("\n");

printf("\n Enter the number of nodes in binary tree 1:");

scanf(" %d",&n);

n1= pow(2,n)-1;

printf(" The max number of possible position is %d\n",n1);

int *a = (int *) malloc (n1 * sizeof(int));

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

{

a[i]=0;

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 56/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 56

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

{

printf("\nPosition and Element: \n");

scanf(" %d %d",&pos,&ele);

insert(a,ele,pos);

}

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

{

printf("\nNodes of Tree 1: \t%d",a[i]);

}

printf(" \nEnter the number of nodes in binary tree 2:");

scanf(" %d",&bin);

bin2= pow(2,bin)-1;

printf("\nThe max number of possible position i %d\n",bin2);

int *b = (int *) malloc (bin2 * sizeof(int));

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

{

b[i]=0;

}

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

{

printf(" \nThe position and Element:\n");

scanf(" %d %d",&pos,&ele);

insert(b,ele,pos);

}

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

{

printf("\nNodes of Tree 2 \t%d",b[i]);

}

similar(a,n1,b,bin2);

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 57/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 57

getch();

}

void insert( int *a ,int ele,int pos)

{

a[pos] = ele ;

}

void similar(int *a,int n1,int *b,int n2)

{

int i=0;

if(n1 != n2)

{

printf("\n\nRESULT:Not a Similar tree\n");

return ;

}

else

{

while((a[i]==b[i])&&i!=n1-1)

{

++i;

}

if(i!=n1-1)

{

printf("\n\nRESULT: Not a similar tree\n ");

return ;

}

else

{

printf("\n\nRESULT: Similar Tree");

}

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 58/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 58

Output:

Checking of similar Binary Trees

----------------------------------------

Enter the number of nodes in binary tree 1: 3

The max number of possible position is 7

Position and Element:

0

4

Position and Element:

3

6

Position and Element:

6

2

Nodes of Tree 1:

4 0 0 6 0 0 2

Enter the number of nodes in binary tree 2: 3

The max number of possible position is 7

The position and Element:

0

4

The position and Element:

3

6

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 59/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 59

The position and Element:

6

2

Nodes of Tree 2 :

4 0 0 6 0 0 2

RESULT: Similar Tree

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 60/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 60

Ex.No:14 

INTERSECT DOUBLY LINKED LISTS

AI M: 

Write a program to intersect two doubly linked lists.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include"malloc.h"

struct node

{

int data;

struct node *prev,*link;

};

void add(struct node **,int);

void display(struct node *);

void intersect(struct node **,struct node **,struct node **);

void main()

{

int num,n,i,j,elem,posi;

struct node *ptr,*ptr1,*tempy;

ptr=NULL;

ptr1=NULL;

tempy=NULL;

clrscr();

printf("Intersection on two Link lists\n");

printf("-----------------------------------\n");

printf("\n");

printf("\nEnter no ofelements in list 1:");

scanf("%d",&n);

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

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 61/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 61

printf(" Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr,num);

display(ptr);

printf("\n");

}

printf("Enter the no of elements in list 2:");

scanf("%d",&elem);

for(j=1;j<=elem;j++)

{

printf(" Enter the element you want to add: ");

scanf("%d",&num);

add(&ptr1,num);

display(ptr1);

printf("\n");

}

intersect(&ptr,&ptr1,&tempy);

printf("\nIntersected link list:\n");

printf("\n");

display(tempy);

getch();

}

void add(struct node ** pt,int num)

{

struct node *temp,*current,*previous;

current=malloc(sizeof(struct node));

current->data=num;

current->prev=NULL;

current->link=NULL;

temp=*pt;

if(temp==NULL)

{

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 62/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 62

*pt=current;

}

else

{

while(temp->link!=NULL)

{

previous=temp;

temp=temp->link;

}

temp->link=current;

current->prev=previous;

}

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

void intersect(struct node **pt1,struct node **pt2,struct node **result)

{

struct node *temp1,*temp2,*current,*tem,*previous;

current=malloc(sizeof(struct node));

current->link=NULL;

current->prev=NULL;

temp1=*pt1;

temp2=*pt2;

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 63/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 63

while(temp1)

{

while(temp1->data!=temp2->data&&temp2!=NULL)

{

temp2=temp2->link;

}

if(temp1->data==temp2->data)

{

if(*result==NULL)

{

current->data=temp1->data;

*result=current;

tem=*result;

previous=tem;

}

else

{

while(tem!=NULL)

{

previous=tem;

tem=tem->link;

}

previous->link=malloc(sizeof(struct node));

previous->link->data=temp1->data;

previous->link->prev=previous;

previous->link->link=NULL;

}

}

tem=*result;

temp2=*pt2;

temp1=temp1->link;

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 64/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 64

Output:

Intersection on two Link lists

-----------------------------------

Enter no ofelements in list 1: 5

Enter the element you want to add: 4

4

Enter the element you want to add: 7

4--> 7

Enter the element you want to add: 2

4--> 7--> 2

Enter the element you want to add: 8

4--> 7--> 2--> 8

Enter the element you want to add: 1

4--> 7--> 2--> 8--> 1

Enter the no of elements in list 2: 5

Enter the element you want to add: 1

1

Enter the element you want to add: 3

1--> 3

Enter the element you want to add: 8

1--> 3--> 8

Enter the element you want to add: 9

1--> 3--> 8--> 9

Enter the element you want to add: 4

1--> 3--> 8--> 9--> 4

Intersected link list:

4--> 8--> 1

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 65/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 65

Ex.No:15 

SINGLY LINKED LIST FROM DOUBLY LINKED LIST

AI M: 

Write a program to generate a singly linked list from it‟s doubly linked list

representation.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include"malloc.h"

struct node

{

int data;

struct node *prev,*link;

};

struct node1

{

int data;

struct node1 *link;

};

void add(struct node **,int);

void display(struct node *);

void generate(struct node **pt,struct node1 **pt1);

void display1(struct node1 *);

void main()

{

int num,n,i,elem,posi;

struct node *ptr,*temp;

struct node1 *ptr1;

ptr=NULL;

ptr1=NULL;

clrscr();

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 66/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 66

printf("Generation of Single link list from Double link list\n");

printf("----------------------------------------------------\n");

printf("\n");

printf("Enter the number of elements you want to insert in DOUBLE link list:");

scanf("%d",&n);

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

{

printf(" Enter the element you want to add :");

scanf("%d",&num);

add(&ptr,num);

display(ptr);

printf("\n");

}

generate(&ptr,&ptr1);

printf("\nAfter conversion from Double link list to Single link list:\n");

printf("\n");

display1(ptr1);

getch();

}

void add(struct node ** pt,int num)

{

struct node *temp,*current,*previous;

current=malloc(sizeof(struct node));

current->data=num;

current->prev=NULL;

current->link=NULL;

temp=*pt;

if(temp==NULL)

{

*pt=current;

}

else

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 67/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 67

{

while(temp->link!=NULL)

{

previous=temp;

temp=temp->link;

}

temp->link=current;

current->prev=previous;

}

}

void display(struct node *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

printf("<--> %d",pt->data);

pt=pt->link;

}

}

void display1(struct node1 *pt)

{

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

pt=pt->link;

while(pt!=NULL)

{

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

pt=pt->link;

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 68/69

Name : A. Arul Jefferson

Roll No: 09MA01 Page 68

void generate(struct node **pt,struct node1 **pt1)

{

int ele;

struct node *temp;

struct node1 *temp1,*current;

current=malloc(sizeof(struct node1));

current->link=NULL;

temp=*pt;

temp1=*pt1;

if(*pt==NULL)

{

printf("List is empty");

}

else

{

ele=temp->data;

current->data=ele;

temp1=current;

temp=temp->link;

*pt1=temp1;

while(temp)

{

ele=temp->data;

temp1->link=malloc(sizeof(struct node1));

temp1=temp1->link;

temp1->data=ele;

temp1->link=NULL;

temp=temp->link;

}

}

}

8/8/2019 Data structure Lab progrms

http://slidepdf.com/reader/full/data-structure-lab-progrms 69/69

Output:

Generation of Single link list from Double link list

------------------------------------------------------------

Enter the number of elements you want to insert in DOUBLE link list:4

Enter the element you want to add : 4

4

Enter the element you want to add : 7

4<--> 7

Enter the element you want to add : 2

4<--> 7<--> 2

Enter the element you want to add : 8

4<--> 7<--> 2<--> 8

After conversion from Double link list to Single link list:

4--> 7--> 2--> 8

top related