Data Structures

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 128

DATA STRUCTURES

Definition

The method to store the information in computer's memory is collectively known as


Data structure.
(or)

A data may be organized in many ways such as logical or mathematical


representation of given data is called as Data Structure.

Classification of Data Structures :


Data Structures can be classified into 2 types.
1. Primitive Data Structures
2. Non-primitive Data Structures.

Primitive Data Structures :


The primitive Data Structures are directly supported by the computer. A set of
integers, characters, real number and some logical statements are examples of
primitive data structures.

Non primitive Data Structures :


These Data Structures has no specific instructions to manipulate the individual
data items are called as non primitive data structures.
These are again classified into 2 types.
1 Linear Data Structures
2 Non-Linear Data Structures.
Linear Data Structure :
A linear data structure is one in which the elements are arranged in a linear
sequence.
Eg: stacks, queues, arrays, and linked lists.

Non linear Data Structures :


A non linear data structures represents the hierarchical relationship between the
elements.
Eg: trees, and graphs.

Self referential structure :

A structure contains a pointer that points to the same structure is known as


self referential structure.

Syntax :

Struct struct_name
{
data item-1;
data item-2;
…………..
data item-n;
struct struct_name *identifier(s);
};

Eg :
struct num
{
int n;
struct num *p;
};

struct num nb1,nb2;

nb1 nb2

Here nb1 and nb2 are two individual data blocks.

To form a link between 2 nodes :


nb1.p = &nb2
nb2.p = NULL

nb1 nb2

Linked lists :
A linked list is a linear collection of data elements called nodes. Each node in a list
contains fields called link or pointer, which contains the address of next node in the list.
Thus the successor nodes in the list need not occupy the adjacent space in the
memory.

Types of Linked Lists


---------------------------
Linked list are 4 types.
1. Single linked
list
2. Circular
single linked list(circular linked list)
3. Double
linked list
4. Circular
double linked list.

Single linked list :


A linked list is a linear collection of data elements called nodes. To represents a linear
list, expand each node to a link to the next node. This representation is called single
linked list.

In the above diagram, the variable First contains the address of first node and the
variable Last contains the address of last node.
Each and every node in the list contains 2 fields.
1. First field
represents the information of the node and is known as info field.
2. Second field
contains the address of the next node and is know as link field.
The last node of the list doesnot have a successor node and hence points to NULL.
Program:
write a program to create a single linked list and display list nodes

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

void main()
{
struct slist *first=NULL,*last,*node,*nd;
char ch;
clrscr();
printf("Create a Single Linked List\n");
do
{
node=(struct slist *)malloc(sizeof(struct slist));
printf("Enter the Node:");
scanf("%d",&node->data);
if(first==NULL)
{
first=last=node;
}
else
{
last->next=node;
last=node;

}
last->next=NULL;
printf("Enter Another Node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("List is Created\n");
printf("List Nodes :");
nd=first;
while(nd!=NULL)
{
printf("%d\t",nd->data);
nd=nd->next;
}
getch();
}
program :
write a program to create a single linked list and perform the following menu options
MENU
-----------------
1.Create List
2.Display
3.Search Node
4.Delete Node
5.Insert First
6.Insert Last
7.Insert Before
8.Insert After
9.Update Node
10.Exit
-------------------

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

struct slist
{
int data;
struct slist *next;
};

typedef struct slist sl;


sl * createlist()
{
sl *first=NULL,*last,*node;
char ch;
do
{
node=(sl *)malloc(sizeof(sl));
printf("Enter the Node:");
scanf("%d",&node->data);
if(first==NULL)
{
first=last=node;
}
else
{
last->next=node;
last=node;

}
last->next=NULL;
printf("Enter Another Node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("List is Created\n");
return first;
}
void display(sl *first)
{
sl *nd;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("List Nodes :");
nd=first;
while(nd!=NULL)
{
printf("\t%d",nd->data);
nd=nd->next;
}
}

int search(sl *first,int n)


{
sl *nd;
nd=first;
while(nd!=NULL)
{
if(nd->data==n)
return 1;

nd=nd->next;
}
return 0;
}

void searchnode(sl *first)


{
int n;
if(first==NULL)
{
printf("List is Empty");
return;
}
printf("Enter Node to Search :");
scanf("%d",&n);
if(search(first,n)==1)
printf("Node Found in the List");
else
printf("Node Not Found");
}

sl *getlast(sl *first)
{
sl *nd;
nd=first;
while(nd->next!=NULL)
{
nd=nd->next;
}
return nd;
}

sl * deletenode(sl *first)
{
sl *nd,*last,*before;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Enter node to Delete :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}

last=getlast(first);

if(first->data==n)
{
nd=first->next;
free(first);
first=nd;
}
else
{
before=first;
nd=first->next;
while(nd!=NULL)
{
if(nd->data==n)
{
if(nd==last)
{
last=before;
free(nd);
last->next=NULL;
}
else
{
before->next=nd->next;
free(nd);
break;
}
}
before=nd;
nd=nd->next;
}
}
printf("Node is Deleted");
return first;
}
sl * insertfirst(sl *first)
{
sl *node;
if(first==NULL)
{
printf("List is Empty");
return first;
}
node=(sl *)malloc(sizeof(sl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
node->next=first;
first=node;
printf("Node is Inserted");
return first;
}

void insertlast(sl *first)


{
sl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return ;
}
node=(sl *)malloc(sizeof(sl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
last->next=node;
last=node;
last->next=NULL;
printf("Node is Inserted");
}

sl *insertbefore(sl *first)
{
sl *nd,*before,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}

printf("Before which Node do you want to Insert :");


scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}
node=(sl *)malloc(sizeof(sl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);

if(first->data==n)
{
node->next=first;
first=node;
}
else
{
before=first;
nd=first->next;
while(nd!=NULL)
{
if(nd->data==n)
{
node->next=nd;
before->next=node;
break;
}
before=nd;
nd=nd->next;
}
}
printf("Node is Inserted");
return first;
}

void insertafter(sl *first)


{
sl *nd,*last,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("After which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}
node=(sl *)malloc(sizeof(sl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
nd=first;
last=getlast(first);
while(nd!=NULL)
{
if(nd->data==n)
{
if(nd==last)
{
last->next=node;
last=node;
last->next=NULL;
}
else
{
node->next=nd->next;
nd->next=node;
break;
}
}
nd=nd->next;
}

printf("Node is Inserted");
}

void updatenode(sl *first)


{
sl *nd;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("Enter Node to Update :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}

nd=first;
while(nd!=NULL)
{
if(nd->data==n)
{
printf("Enter New for Updation\n");
printf("Enter the node :");
scanf("%d",&nd->data);
break;
}
nd=nd->next;
}
printf("Node is Updated");
}

void main()
{
sl *first=NULL;
int opt;
while(1)
{
clrscr();
printf(" MENU");
printf("\n--------------");
printf("\n1.Create List");
printf("\n2.Display");
printf("\n3.Search Node");
printf("\n4.Delete Node");
printf("\n5.Insert First");
printf("\n6.Insert Last");
printf("\n7.Insert Before");
printf("\n8.Insert After");
printf("\n9.Update Node");
printf("\n10.Exit");
printf("\n--------------");
printf("\nEnter Your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:
printf("Create a Single Linked List\n");
first=createlist();
break;

case 2:
display(first);
break;
case 3:
searchnode(first);
break;

case 4:
first=deletenode(first);
break;

case 5:
first=insertfirst(first);
break;

case 6:
insertlast(first);
break;

case 7:
first=insertbefore(first);
break;

case 8:
insertafter(first);
break;

case 9:
updatenode(first);
break;
case 10:
exit(0);
}
getch();
}

Circular single linked list :

Sometimes it is necessary for an application the linked list structure is circular. That
means each node will have a pointer to the next node. But the last node of the list
shall point to the first node of the list instead of pointing to NULL.

Diagram

program :
write a program to create a circular single linked list and perform the following menu
options
MENU
-----------------
1.Create List
2.Display
3.Search Node
4.Delete Node
5.Insert First
6.Insert Last
7.Insert Before
8.Insert After
9.Update Node
10.Exit
-------------------

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

struct cslist
{
int data;
struct cslist *next;
};
typedef struct cslist csl;

csl * createlist()
{
csl *first=NULL,*last,*node;
char ch;
do
{
node=(csl *)malloc(sizeof(csl));
printf("Enter the Node:");
scanf("%d",&node->data);
if(first==NULL)
{
first=last=node;
}
else
{
last->next=node;
last=node;

}
last->next=first;
printf("Enter Another Node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("List is Created\n");
return first;
}

void display(csl *first)


{
csl *nd;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("List Nodes :");
nd=first;
do
{
printf("\t%d",nd->data);
nd=nd->next;
} while(nd!=first);
}

int search(csl *first,int n)


{
csl *nd;
nd=first;
do
{
if(nd->data==n)
return 1;
nd=nd->next;
}while(nd!=first);
return 0;
}

void searchnode(csl *first)


{
int n;
if(first==NULL)
{
printf("List is Empty");
return;
}
printf("Enter Node to Search :");
scanf("%d",&n);
if(search(first,n)==1)
printf("Node Found in the List");
else
printf("Node Not Found");
}

csl *getlast(csl *first)


{
csl *nd;
nd=first;
while(nd->next!=first)
{
nd=nd->next;
}
return nd;
}

csl * deletenode(csl *first)


{
csl *nd,*last,*before;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Enter node to Delete :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}

last=getlast(first);

if(first->data==n)
{
if(first==last)
{
free(first);
first=NULL;
}
else
{
nd=first->next;
free(first);
first=nd;
last->next=first;
}
}
else
{
before=first;
nd=first->next;
do
{
if(nd->data==n)
{
if(nd==last)
{
last=before;
free(nd);
last->next=first;
}
else
{
before->next=nd->next;
free(nd);
break;
}
}
before=nd;
nd=nd->next;
}while(nd!=first);
}
printf("Node is Deleted");
return first;
}

csl * insertfirst(csl *first)


{
csl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return first;
}
node=(csl *)malloc(sizeof(csl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
node->next=first;
first=node;
last->next=first;
printf("Node is Inserted");
return first;
}

void insertlast(csl *first)


{
csl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return ;
}
node=(csl *)malloc(sizeof(csl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
last->next=node;
last=node;
last->next=first;
printf("Node is Inserted");
}

csl *insertbefore(csl *first)


{
csl *nd,*before,*node,*last;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Before which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}
node=(csl *)malloc(sizeof(csl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
if(first->data==n)
{
node->next=first;
first=node;
last->next=first;
}
else
{
before=first;
nd=first->next;
do
{
if(nd->data==n)
{
node->next=nd;
before->next=node;
break;
}
before=nd;
nd=nd->next;
}while(nd!=first);
}
printf("Node is Inserted");
return first;
}

void insertafter(csl *first)


{
csl *nd,*last,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("After which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}
node=(csl *)malloc(sizeof(csl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
nd=first;
last=getlast(first);
do
{
if(nd->data==n)
{
if(nd==last)
{
last->next=node;
last=node;
last->next=first;
}
else
{
node->next=nd->next;
nd->next=node;
break;
}
}
nd=nd->next;
}while(nd!=first);

printf("Node is Inserted");
}
void updatenode(csl *first)
{
csl *nd;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("Enter Node to Update :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}

nd=first;
do
{
if(nd->data==n)
{
printf("Enter New for Updation\n");
printf("Enter the node :");
scanf("%d",&nd->data);
break;
}
nd=nd->next;
}while(nd!=first);

printf("Node is Updated");
}

void main()
{
csl *first=NULL;
int opt;
while(1)
{
clrscr();
printf(" MENU");
printf("\n--------------");
printf("\n1.Create List");
printf("\n2.Display");
printf("\n3.Search Node");
printf("\n4.Delete Node");
printf("\n5.Insert First");
printf("\n6.Insert Last");
printf("\n7.Insert Before");
printf("\n8.Insert After");
printf("\n9.Update Node");
printf("\n10.Exit");
printf("\n--------------");
printf("\nEnter Your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:
printf("Create a Circular Single Linked List\n");
first=createlist();
break;

case 2:
display(first);
break;

case 3:
searchnode(first);
break;

case 4:
first=deletenode(first);
break;

case 5:
first=insertfirst(first);
break;
case 6:
insertlast(first);
break;

case 7:
first=insertbefore(first);
break;

case 8:
insertafter(first);
break;

case 9:
updatenode(first);
break;

case 10:
exit(0);
}
getch();
}
}

Double linked list:


In double linked list one can move in either direction. It requires more memory than
single linked list. Since each node contains two link components instead of one. A
general solution of the one way single linked list is to include two links in each node.

Diagram

The link denoting the previous node is called “Left link” And the link denoting
successive node is called the “Right link”. The first node of the left link and the last
node of the right link point to NULL.

program :
write a program to create a Double linked list and perform the following menu options

MENU
-----------------
1.Create List
2.Display
3.Search Node
4.Delete Node
5.Insert First
6.Insert Last
7.Insert Before
8.Insert After
9.Update Node
10.Exit
-------------------

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

struct dlist
{
int data;
struct dlist *prev,*next;
};
typedef struct dlist dl;

dl * createlist()
{
dl *first=NULL,*last,*node;
char ch;
do
{
node=(dl *)malloc(sizeof(dl));
printf("Enter the Node:");
scanf("%d",&node->data);
if(first==NULL)
{
first=last=node;
first->prev=NULL;
}
else
{
last->next=node;
node->prev=last;
last=node;
}
last->next=NULL;
printf("Enter Another Node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("List is Created\n");
return first;
}

dl *getlast(dl *first)
{
dl *nd;
nd=first;
while(nd->next!=NULL)
{
nd=nd->next;
}
return nd;
}

void display(dl *first)


{
dl *nd;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("List Nodes from First to Last :");
nd=first;
while(nd!=NULL)
{
printf("\t%d",nd->data);
nd=nd->next;

}
printf("\nList Nodes from Last to First :");
nd=getlast(first);
while(nd!=NULL)
{
printf("\t%d",nd->data);
nd=nd->prev;
}
}

int search(dl *first,int n)


{
dl *nd;
nd=first;
while(nd!=NULL)
{
if(nd->data==n)
return 1;

nd=nd->next;
}

return 0;
}

void searchnode(dl *first)


{
int n;
if(first==NULL)
{
printf("List is Empty");
return;
}
printf("Enter Node to Search :");
scanf("%d",&n);
if(search(first,n)==1)
printf("Node Found in the List");
else
printf("Node Not Found");
}

dl * deletenode(dl *first)
{
dl *nd,*last;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Enter node to Delete :");
scanf("%d",&n);

if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}

last=getlast(first);

if(first->data==n)
{
nd=first->next;
free(first);
first=nd;
first->prev=NULL;
}
else
{
nd=first->next;
while(nd!=NULL)
{
if(nd->data==n)
{
if(nd==last)
{
last=last->prev;
free(nd);
last->next=NULL;
}
else
{
nd->prev->next=nd->next;
nd->next->prev=nd->prev;
free(nd);
break;
}
}
nd=nd->next;
}
}
printf("Node is Deleted");
return first;
}

dl * insertfirst(dl *first)
{
dl *node;
if(first==NULL)
{
printf("List is Empty");
return first;
}
node=(dl *)malloc(sizeof(dl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
node->next=first;
first->prev=node;
first=node;
first->prev=NULL;
printf("Node is Inserted");
return first;
}

void insertlast(dl *first)


{
dl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return ;
}
node=(dl *)malloc(sizeof(dl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
last->next=node;
node->prev=last;
last=node;
last->next=NULL;
printf("Node is Inserted");
}

dl *insertbefore(dl *first)
{
dl *nd,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Before which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}
node=(dl *)malloc(sizeof(dl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);

if(first->data==n)
{
node->next=first;
first->prev=node;
first=node;
first->prev=NULL;
}
else
{
nd=first->next;
while(nd!=NULL)
{
if(nd->data==n)
{
node->next=nd;
node->prev=nd->prev;
nd->prev->next=node;
nd->prev=node;
break;
}
nd=nd->next;
}
}
printf("Node is Inserted");
return first;
}

void insertafter(dl *first)


{
dl *nd,*last,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("After which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}
node=(dl *)malloc(sizeof(dl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
nd=first;
while(nd!=NULL)
{
if(nd->data==n)
{
if(nd==last)
{
last->next=node;
node->prev=last;
last=node;
last->next=NULL;
}
else
{
node->next=nd->next;
node->prev=nd;
nd->next->prev=node;
nd->next=node;
break;
}
}
nd=nd->next;
}
printf("Node is Inserted");
}

void updatenode(dl *first)


{
dl *nd;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("Enter Node to Update :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}

nd=first;
while(nd!=NULL)
{
if(nd->data==n)
{
printf("Enter New for Updation\n");
printf("Enter the node :");
scanf("%d",&nd->data);
break;
}
nd=nd->next;
}
printf("Node is Updated");
}

void main()
{
dl *first=NULL;
int opt;
while(1)
{
clrscr();
printf(" MENU");
printf("\n--------------");
printf("\n1.Create List");
printf("\n2.Display");
printf("\n3.Search Node");
printf("\n4.Delete Node");
printf("\n5.Insert First");
printf("\n6.Insert Last");
printf("\n7.Insert Before");
printf("\n8.Insert After");
printf("\n9.Update Node");
printf("\n10.Exit");
printf("\n--------------");
printf("\nEnter Your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:
printf("Create a Double Linked List\n");
first=createlist();
break;

case 2:
display(first);
break;
case 3:
searchnode(first);
break;

case 4:
first=deletenode(first);
break;
case 5:
first=insertfirst(first);
break;

case 6:
insertlast(first);
break;

case 7:
first=insertbefore(first);
break;

case 8:
insertafter(first);
break;

case 9:
updatenode(first);
break;

case 10:
exit(0);
}
getch();
}
}

Circular double linked list:

Sometimes it is necessary for an application that the linked list structure is circular as
well as double sided. That means each node have a pointer to the next node in the list.
But the last node of the list shall point to the first node of the list instead of pointing to
NULL. And also each node will have one extra pointer that points to the previous node
in the list. But the first node of the list shall point to the last node of the list instead of
pointing to NULL.

Diagram

program :
write a program to create a Circular Double linked list and perform the following menu
options

MENU
-----------------
1.Create List
2.Display
3.Search Node
4.Delete Node
5.Insert First
6.Insert Last
7.Insert Before
8.Insert After
9.Update Node
10.Exit
-------------------

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

struct cdlist
{
int data;
struct cdlist *prev,*next;
};
typedef struct cdlist cdl;

cdl * createlist()
{
cdl *first=NULL,*last,*node;
char ch;
do
{
node=(cdl *)malloc(sizeof(cdl));
printf("Enter the Node:");
scanf("%d",&node->data);
if(first==NULL)
{
first=last=node;
}
else
{
last->next=node;
node->prev=last;
last=node;
}
first->prev=last;
last->next=first;
printf("Enter Another Node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("List is Created\n");
return first;
}

cdl *getlast(cdl *first)


{
cdl *nd;
nd=first;
while(nd->next!=first)
{
nd=nd->next;
}
return nd;
}

void display(cdl *first)


{
cdl *nd,*last;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("List Nodes from First to Last :");
nd=first;
do
{
printf("\t%d",nd->data);
nd=nd->next;
}while(nd!=first);

printf("\nList Nodes from Last to First :");


last=getlast(first);
nd=last;
do
{
printf("\t%d",nd->data);
nd=nd->prev;
} while(nd!=last);
}
int search(cdl *first,int n)
{
cdl *nd;
nd=first;
do
{
if(nd->data==n)
return 1;

nd=nd->next;
}while(nd!=first);
return 0;
}

void searchnode(cdl *first)


{
int n;
if(first==NULL)
{
printf("List is Empty");
return;
}
printf("Enter Node to Search :");
scanf("%d",&n);
if(search(first,n)==1)
printf("Node Found in the List");
else
printf("Node Not Found");
}

cdl * deletenode(cdl *first)


{
cdl *nd,*last;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Enter node to Delete :");
scanf("%d",&n);

if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}

last=getlast(first);

if(first->data==n)
{
if(first==last)
{
free(first);
first=NULL;
}
else
{
nd=first->next;
free(first);
first=nd;
first->prev=last;
last->next=first;
}
}
else
{
nd=first->next;
do
{
if(nd->data==n)
{
if(nd==last)
{
last=last->prev;
free(nd);
last->next=first;
first->prev=last;
}
else
{
nd->prev->next=nd->next;
nd->next->prev=nd->prev;
free(nd);
break;
}
}
nd=nd->next;
}while(nd!=first);
}
printf("Node is Deleted");
return first;
}

cdl * insertfirst(cdl *first)


{
cdl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return first;
}
node=(cdl *)malloc(sizeof(cdl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
node->next=first;
first->prev=node;
first=node;
first->prev=last;
last->next=first;
printf("Node is Inserted");
return first;
}

void insertlast(cdl *first)


{
cdl *node,*last;
if(first==NULL)
{
printf("List is Empty");
return ;
}
node=(cdl *)malloc(sizeof(cdl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
last->next=node;
node->prev=last;
last=node;
last->next=first;
first->prev=last;
printf("Node is Inserted");
}

cdl *insertbefore(cdl *first)


{
cdl *nd,*node,*last;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
printf("Before which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return first;
}
node=(cdl *)malloc(sizeof(cdl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
if(first->data==n)
{
node->next=first;
first->prev=node;
first=node;
first->prev=last;
last->next=first;
}
else
{
nd=first->next;
do
{
if(nd->data==n)
{
node->next=nd;
node->prev=nd->prev;
nd->prev->next=node;
nd->prev=node;
break;
}
nd=nd->next;
}while(nd!=first);
}
printf("Node is Inserted");
return first;
}

void insertafter(cdl *first)


{
cdl *nd,*last,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("After which Node do you want to Insert :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}
node=(cdl *)malloc(sizeof(cdl));
printf("Enter Node to Insert :");
scanf("%d",&node->data);
last=getlast(first);
nd=first;
do
{
if(nd->data==n)
{
if(nd==last)
{
last->next=node;
node->prev=last;
last=node;
last->next=first;
first->prev=last;
}
else
{
node->next=nd->next;
node->prev=nd;
nd->next->prev=node;
nd->next=node;
break;
}
}
nd=nd->next;
}while(nd!=first);
printf("Node is Inserted");
}

void updatenode(cdl *first)


{
cdl *nd;
int n;
if(first==NULL)
{
printf("List is Empty");
return ;
}
printf("Enter Node to Update :");
scanf("%d",&n);
if(search(first,n)==0)
{
printf("Node Not Found");
return ;
}
nd=first;
do
{
if(nd->data==n)
{
printf("Enter New for Updation\n");
printf("Enter the node :");
scanf("%d",&nd->data);
break;
}
nd=nd->next;
}while(nd!=first);
printf("Node is Updated");
}

void main()
{
cdl *first=NULL;
int opt;
while(1)
{
clrscr();
printf(" MENU");
printf("\n--------------");
printf("\n1.Create List");
printf("\n2.Display");
printf("\n3.Search Node");
printf("\n4.Delete Node");
printf("\n5.Insert First");
printf("\n6.Insert Last");
printf("\n7.Insert Before");
printf("\n8.Insert After");
printf("\n9.Update Node");
printf("\n10.Exit");
printf("\n--------------");
printf("\nEnter Your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:
printf("Create a Circular Double Linked List\n");
first=createlist();
break;

case 2:
display(first);
break;

case 3:
searchnode(first);
break;
case 4:
first=deletenode(first);
break;
case 5:
first=insertfirst(first);
break;
case 6:
insertlast(first);
break;

case 7:

first=insertbefore(first);
break;
case 8:
insertafter(first);
break;

case 9:
updatenode(first);
break;

case 10:
exit(0);
}
getch();
}
}
STACKS and QUEUES

STACK

A stack is a liner list in which addition of new elements (or)


deletion of existing elements always takes place at the same end. This
end is known as "top" of the stack. Because the elements are added
and removed only from the "top" of the stack. The Last element to be
added is the first to be removed. Accordingly stacks are called last-in-
first- out((LIFO )structure.

Basic operations associated on stacks:


1. Push:-
It is the operation of inserting an element on to the stack.
2. Pop:-
It is the operation of deleting an element from the stack.

Stack Representation
A stack can be represented in 2 ways.
1. Sequential representation:-
The sequential representation of stacks uses arrays.
2. Linked representation:-
The linked representation of stacks uses linked list.

sequential Representation:

Initially when stack is empty and top= -1

Push:-It is an operation of inserting element on to the stack. While


inserting an element first of all we have to check whether the stack is
full or not. If stack is not full, increment the top by 1 and insert element
at the stack[top].
Pop:-It is the operation of deleting an element from the stack. While
deleting an element we have to check whether the stack is empty (or)
not . If stack is not empty, delete the element at stack[top] and then
decrement top by 1.
Program :

/* Stack Using Arrays */

#include<stdio.h>
#include<conio.h>
#define size 5

int stack[size],top=-1;

void push(int x)
{
if(top==size-1)
{
printf("Stack is Full");
}
else
{
top++;
stack[top]=x;
printf("Element is Inserted");
}
}

int pop()
{
int x;
if(top==-1)
{
printf("Stack is Empty");
return 0;
}
else
{
x=stack[top];
top--;
return x;
}
}

void display()
{
int i;
if(top==-1)
{
printf("Stack is Empty");
}
else
{
printf("Stack Elements : ");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Stack Menu");
printf("\n-------------");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:

printf("Enter Element to insert :");


scanf("%d",&n);
push(n);
break;

case 2:
n=pop();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;
case 4:
exit(0);
}
getch();
}
}

Linked Representation of stack

Array representation is used when the stack size is fixed, If the stack size is varying during the
program execution then linked representation is used. Single linked list structure is sufficient to
represent any stack.

Diagram :

Program :
/* Stack Using Linked List */

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

struct stack
{
int data;
struct stack *next;
};

struct stack *top=NULL;


void push(int item)
{
struct stack *node;
node=(struct stack *)malloc(sizeof(struct stack));
node->data=item;
if(top==NULL)
{
top=node;
top->next=NULL;
}
else
{
node->next=top;
top=node;
}

printf("Node is Inserted");
}

int pop()
{
int item;
struct stack *nd;
if(top==NULL)
{
printf("Stack is Empty");
return 0;
}
else
{
item=top->data;
nd=top->next;
free(top);
top=nd;
return item;
}
}

void display()
{
struct stack *nd;
if(top==NULL)
{
printf("Stack is Empty");
}
else
{
printf("Stack Nodes : ");
nd=top;
while(nd!=NULL)
{
printf("\t%d",nd->data);
nd=nd->next;
}
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Stack Menu");
printf("\n-------------");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:

printf("Enter Node to insert :");


scanf("%d",&n);
push(n);
break;

case 2:
n=pop();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;

case 4:
exit(0);
}
getch();
}
}
QUEUE
A Queue is linear list in which all insertions takes place at one
end and called "Rear end" while deletion takes place at the other
end called "front end". Accordingly Queues are called
first-in- first- out(FIFO) structure.

A Queue is represented as follows :


Diagram:

Queue can implemented by either using one dimensional array or using


Linked list. However, the simplest way to represent a Queue is by using
one dimensional array.

Associated with Queues there are two variables called front and
rear. The first element of the Queue is indicated by the front and the
last element is indicated by the rear.

Queue Operations:-
A Queue has two basic operations
1)Enqueue(Insertion):- Adding new items to the Queue.
2) Dequeue(Deletion):- Removing items from the Queue.

Queue Using Arrays:-


Initially when Queue is empty front = rear = -1
Equeue:-
To insert an Element to the Queue, first we increment a rear by one
and then insert a new element at Queue [rear]. However, if rear=size-1
then new element can't be inserted. Since Queue is full i.e.., rear most
location is occupied and hence insertion can not be performed.
Note:

Dequeue:-
While removing the element of Queue, we have to check whether
front = rear (or) not. Deletion is possible Only when front is not equal
to rear. If front equal to rear Queue is empty otherwise the front is
incremented by one and delete element at Queue [ front].

Program :
/* Queue Using Arrays */

#include<stdio.h>
#include<conio.h>
#define size 5
int queue[size],front=-1,rear=-1;

void enqueue(int x)
{
if(rear==size-1)
{
printf("Queue is Full");
}
else
{
rear++;
queue[rear]=x;
printf("Element is Inserted");
}
}

int dequeue()
{
int x;
if(front==rear)
{
printf("Queue is Empty");
return 0;
}
else
{
front++;
x=queue[front];
return x;
}
}

void display()
{
int i;
if(front==rear)
{
printf("Queue is Empty");
}
else
{
printf("Queue Elements :");
for(i=front+1;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Queue Menu");
printf("\n-------------");
printf("\n1.Enqueue");
printf("\n2.Dequeue");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:
printf(" Enter Element to insert :");
scanf("%d",&n);
enqueue(n);
break;

case 2:
n=dequeue();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;

case 4:
exit(0);
}
getch();
}
}

Queue using linked list

Array representation is used when the queue size is fixed, If the


queue size is varying during the program execution then linked
representation is used. Single linked list structure is sufficient to
represent a queue.
Diagram :
Program:
/* Queue Using Linked List */

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

struct queue
{
int data;
struct queue *next;
};

struct queue *front=NULL,*rear;

void enqueue(int x)
{
struct queue *node;
node=(struct queue *)malloc(sizeof(struct queue));
node->data=x;
if(front==NULL)
{
front=rear=node;
}
else
{
rear->next=node;
rear=node;
}
rear->next=NULL;
printf("Node is Inserted");
}

int dequeue()
{
struct queue *nd;
int x;
if(front==NULL)
{
printf("Queue is Empty");
return 0;
}
else
{
x=front->data;
nd=front->next;
free(front);
front=nd;
return x;
}
}

void display()
{
struct queue *nd;
if(front==NULL)
{
printf("Queue is Empty");
}
else
{
printf("Queue Nodes :");
nd=front;
while(nd!=NULL)
{
printf("%d\t",nd->data);
nd=nd->next;
}
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Queue Menu");
printf("\n-------------");
printf("\n1.Enqueue");
printf("\n2.Dequeue");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:

printf("Enter Node to insert :");


scanf("%d",&n);
enqueue(n);
break;

case 2:
n=dequeue();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;

case 4:
exit(0);
}
getch();
}
}

Circular Queue

It is also a Queue is invented to overcome the drawback of


ordinary Queue. In case of ordinary Queues the delete elements
remains unused. But in circular Queue the rear and front Moves from
nth location to Ist location.
Diagram:
/* Circular queue Using Arrays */

#include<stdio.h>
#include<conio.h>
#define size 5
int cqueue[size],front=-1,rear=-1,empty=1;

void encqueue(int x)
{
if(front==rear && empty==0)
{
printf("Cqueue is Full");
}
else
{
rear++;
cqueue[rear]=x;
if(rear==size-1)
rear=-1;
if(front==rear)
empty=0;
printf("Element is Inserted");
}
}

int decqueue()
{
int x;
if(front==rear && empty==1)
{
printf("Cqueue is Empty");
return 0;
}
else
{
front++;
x=cqueue[front];
if(front==size-1)
front=-1;
empty=1;
return x;
}
}

void display()
{
int i;
if(front==rear && empty==1)
{
printf("Cqueue is Empty");
}
else
{
printf("Cqueue Elements :");
i=front;
do
{
i++;
printf("%d\t",cqueue[1]);

if(i==size-1)
i=-1;
}while(i!=rear);
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Cqueue Menu");
printf("\n-------------");
printf("\n1.Encqueue");
printf("\n2.Decqueue");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:

printf(" Enter Element to insert :");


scanf("%d",&n);
encqueue(n);
break;

case 2:
n=decqueue();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;

case 4:
exit(0);
}
getch();
}
}

/* Circular queue Using Linked List */

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

struct cqueue
{
int data;
struct cqueue *next;
};

struct cqueue *front=NULL,*rear;


void encqueue(int x)
{
struct cqueue *node;
node=(struct cqueue *)malloc(sizeof(struct cqueue));
node->data=x;
if(front==NULL)
{
front=rear=node;
}
else
{
rear->next=node;
rear=node;
}
rear->next=front;
printf("Node is Inserted");
}

int decqueue()
{
struct cqueue *nd;
int x;
if(front==NULL)
{
printf("Cqueue is Empty");
return 0;
}
else
{
x=front->data;
if(front==rear)
{
free(front);
front=NULL;
}
else
{
nd=front->next;
free(front);
front=nd;
rear->next=front;
}
return x;
}
}

void display()
{
struct cqueue *nd;
if(front==NULL)
{
printf("Cqueue is Empty");
}
else
{
printf("Cqueue Nodes :");
nd=front;
do
{
printf("\t%d",nd->data);
nd=nd->next;
}while(nd!=front);
}
}

void main()
{
int opt,n;
while(1)
{
clrscr();
printf("Cqueue Menu");
printf("\n-------------");
printf("\n1.Encqueue");
printf("\n2.Decqueue");
printf("\n3.Di splay");
printf("\n4.Exit");
printf("\n-------------");
printf("\nEnter your Option :");
scanf("%d",&opt);
clrscr();
switch(opt)
{
case 1:

printf("Enter Node to insert :");


scanf("%d",&n);
encqueue(n);
break;

case 2:
n=decqueue();
if(n!=0)
printf("%d is Deleted",n);

break;

case 3:
display();
break;
case 4:
exit(0);
}
getch();
}
}

SORTINGS
"SORT" is the concept of arranging data in ascending (or) descending
order with character (or) numeric data.
BUBBLE SORT:
In this technique it compares two consecutive elements in the list. If
they are not in order the two elements will be interchanged. Otherwise
they are in order, the two elements will not be interchanged .This process
will continue n-1 times of an array size of 'n'.

Example:-

3 7 9 4 1

1) 3 7 4 1 9

2) 3 4 1 7 9

3) 3 1 4 7 9

4) 1 3 4 7 9

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
void accept(int [],int);
void disp(int [],int);
void bubblesort(int [],int);
int a[20],n;
clrscr();
printf("Enter No of Elements :");
scanf("%d",&n);
printf("Enter Elements\n");
accept(a,n);
printf("Given Elements before sorting :");
disp(a,n);
bubblesort(a,n);
printf("\nGiven Elements After sorting :");
disp(a,n);

getch();
}

void accept(int a[],int n)


{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void disp(int a[],int n)


{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void bubblesort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j] > a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}

SELECTION SORT:

In this method first find the smallest element in the list and put in the
first position and then find the second smallest in the list and put it in
the second position and so on. This process will continue (n-1) times of
an array size 'n'.
Example:

3 7 9 1 4

1. 1 7 9 3 4

2. 1 3 9 7 4

3. 1 3 4 7 9

4. 1 3 4 7 9

Program :

#include<stdio.h>
#include<conio.h>
void main()
{
void accept(int [],int);
void disp(int [],int);
void selectionsort(int [],int);
int a[20],n;
clrscr();
printf("Enter No of Elements :");
scanf("%d",&n);
printf("Enter Elements\n");
accept(a,n);
printf("Given Elements before sorting :");
disp(a,n);
selectionsort(a,n);
printf("\nGiven Elements After sorting :");
disp(a,n);
getch();
}
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void disp(int a[],int n)


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

void selectionsort(int a[],int n)


{
int i,j,t,loc;
for(i=0;i<n-1;i++)
{
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j] < a[loc])
loc=j;
}
if(i!=loc)
{
t=a[i];
a[i]=a[loc];
a[loc]=t;
}
}
}

INSERTION SORT:-
Insertion sort is an idea of inserting elements into an existing sorted
array. To insert an element we must find the proper place where
insertion is to be made. To find this place we need to search. Once we
have find the correct place, we need to move the elements to make a
place for the new element. We may reduce our work by combining the
two operations of "searching" and "shifting". We start searching from
the end of the array and keep on moving the elements till we find its
proper place.

Example:
5 3 7 1 9 4

1. 3 5 7 1 9 4
2. 3 5 7 1 9 4

3. 1 3 5 7 9 4

4. 1 3 5 7 9 4

5. 1 3 4 5 7 9

Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n;
void accept(int [],int);
void disp(int [],int);
void insertionsort(int [],int);
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter Elements \n");
accept(a,n);
printf("\nGiven Elements before sorting :");
disp(a,n);
insertionsort(a,n);
printf("\nGiven Elements After sorting :");
disp(a,n);
getch();
}
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void disp(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}

void insertionsort(int a[],int n)


{
int i,j,t;
for(i=1;i<n;i++)
{
for(j=i ; j>0 && a[j]<a[j-1] ; j--)
{
t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}

QUICK SORT:
This method was developed by C.A.R.HOARE. It is a partition
method, using the particular element (pivot element). The
given table is partitioned into two sub tables, so that first the
original key(pivot element) will be proper position in the sorted
sequence and secondly all keys to the left of this key will be less
in value and all keys to the right of it will be greater in value
.The same process is is applied for each of subtables ( The
sub tables contains at least 2 elements).
Example :

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n;
void accept(int [],int);
void disp(int [],int);
void quicksort(int [],int,int);
clrscr();
printf("Enter no of Elements:");
scanf("%d",&n);
printf("Enter Elements\n");
accept(a,n);
printf("\nGiven Elements before sorting :");
disp(a,n);
quicksort(a,0,n-1);
printf("\nGiven Elements After sorting :");
disp(a,n);
getch();
}
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

void disp(int a[],int n)


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

void quicksort(int a[],int lb,int ub)


{
int i,j,p,t;
if(lb<ub)
{
i=lb;
j=ub;
p=a[lb];
while(i<j)
{
while(a[i]<p)
i++;

while(a[j]>p)
j--;

if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}

/*
if(a[i]==a[j])
i++; (or) j--;
*/

}
quicksort(a,lb,j-1);
quicksort(a,j+1,ub);
}
}

SEARCHINGS
Search is the concept of searching’s for an item in the list of
data stored. The application of search occurs when a particular data is
required from a set of stored data.

1) Linear search: - (sequential search)


Suppose "A" is a linear array with "n" elements.Search for
a given element "x". x is compare with the element of A one by
one . That is first we test whether A[0]==x and then test
whether A[1]==x and so on.
This method which transverses array A sequentially to
allocate x is called Linear search.
Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,p,x;
void accept(int[],int n);
int linearsearch(int[],int,int);
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:\n");
accept(a,n);
printf("Enter elements to search:");
scanf("%d",&x);
p=linearsearch(a,n,x);
if(p==-1)
printf("Element not Found:");
else
printf("%d is Found at position:%d",x,p);
getch();
}
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

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


{
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
return i;
}
return -1;
}

2) Binary search:-
A binary search algorithm search for an element by trying to isolate
smaller partitions in which the element can be found. In this method
the search element is compared with the middle element of the table. If
they are equal the search end successfully. Otherwise either the upper
(or) lower half of the table must be searched in a similar manner.

Given an array A with ‘n’ elements (Array should be in ascending


order).The appropriate middle entry say mid of the array is located
and a[mid] is compared with the elements to be search.
Case 1:-If a[mid]>key(key=search element)
The search interval for next to pass will be first half of the array that is
elements a[0] to a[mid-1].
Case 2:-If a[mid]<key
The search interval for next to pass will be second half of the array that
is elements from a[mid+1] to a [n-1].

Case 3:-A[mid]==key then search is successful process is stopped.


The case 1, case 2 process continues untill the desired
element is found or search element interval becomes empty.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,p,x;
void accept(int[],int);
int binarysearch(int[],int,int);
clrscr();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements in sorted order:\n");
accept(a,n);
printf("\nEnter elements to search:");
scanf("%d",&x);
p=binarysearch(a,n,x);
if(p==-1)
printf("Element not found:");
else
printf("Element is found at position:%d",p);
getch();
}
void accept(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}

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


{
int l,u,mid;
l=0;
u=n-1;
while(l<=u)
{
mid=(l+u)/2;
if(a[mid]==x)
return mid;
else if(a[mid]<x)
l=mid+1;
else
u=mid-1;
}
return -1;
}

TREES
So far we have learned about arrays, stacks, queues and linked list
which are known as linear data structures. These are termed as ‘linear’
because the elements are arranged in a linear fashion.
Another very useful data structure is “Tree” in which the elements
appeared in a non-linear fashion.

Tree:- A tree is a finite set of one (or) more nodes such that
1.There is a specially designated node called ‘Root’.
2. Remaining nodes are partitioned into n (n>0) sets t1, t2, t3… tn
Where each ti (i=1, 2, 3 ...n) is a tree. Where t1, t2, t3…….tn are sub
trees of the root.
To illustrate to the above definition let us consider the sample tree as
given below
T2 T3
T1 A

NODES
B C D

E F G H I J

K L leaves

In the sample tree T, there is a set of twelve nodes. Here A is


special node being the root of the tree. Remaining nodes are partition
into three sets T1,T2 and T3. They are sub-trees of the root node A,
by the definition each sub-tree is again a tree.

Basic Terminology
Node: This is the main component of any tree structure. The concept of
node is same as in linked list. Node of a tree stores the actual data and
the links to the other node.
Branches: The lines connecting together nodes in a tree structure are
called branches.
Root: The top most node of a tree is called Root.
Leaves: The bottom most of the nodes of a tree are called leaves .
Child: All immediate successors of a node are known as child nodes.
Parent : The immediate pre-decessor of a node is called as parent.

BINARY TREE:
A Binary tree is a finite collection of elements. When the binary tree
is not empty, it has a root element, and the remaining elements are
partitioned into two binary trees, which are called left and right sub
trees to the original tree.

Binary tree representation:


we can represents binary tree T in two ways.
 Sequential representation( using arrays).
 Linked representation (using Linked list).
Sequential representation:
n
Where level is n. The Maximum nodes in nth level are 2 -1. The above tree structure level is 4 i.e.., it
requires 15 locations.

Position 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Information A B C - D E F - - G - - H - -
TREE

This representation uses a single linear array “TREE” as follows.


The Root node is stored in Tree[0].
If any node in occupies Tree[k].Then its left child is stored in
Tree[2*k+1]. And it right child is stored in Tree[2*k+2].

Above diagram shows the binary tree . and its sequential


representation. Observed that we used 15 locations in the array ’TREE’
Even though it has only 8 nodes.
The disadvantage of sequential representation is lot of memory
wastage. Because the nodes are stored sequentially. However for a
complete binary tree the array representation is efficient.

Complete Binary Tree:


If the node numbers of a binary tree are in complete sequence
without any missing number in between the sequence. Such a binary
tree is called complete binary tree.

Linked representation of binary tree:


Linked representation of Binary Tree uses linked list having 3 fields
namely info field, left and right pointers. The left and right pointers are
necessary for Binary trees because nodes of a tree might branch to left
(or) right child.
Diagram:
Binary Tree: T
Linked Representation:- T

Binary search tree:-


It is also a binary tree that may be empty. A non empty binary search
tree satisfies the following properties.

 Every element has a key (value). And no two elements have


the same key.
 The keys (if any) in the left sub tree of the root are smaller
than the key in the root.
 The keys (if any) in the right sub tree of the root are longer
than the key in the root.
 The left and right sub trees of the root are also binary search
trees.
Operations on a binary Tree or Binary Tree traversals

Traversing a Tree means visiting each node in a tree. There are three
standard ways of traversing a binary tree with root.
 Pre-order traversal
 In order traversal
 Post order traversal
1. Pre-order traversal:
A binary tree can be traversed in pre-order as follows.
1. Process the root node
2. Traverse the left sub tree in pre-order
3. Traverse the right sub tree in pre-order
Ex:-1. Diagram:
Ex:-2 Diagram:
20

12 40

6 15 35

13
38

14
2. In-order tree traversal:-

Binary tree can be traversal in-order as follows.


 Travese the left sub tree in in-order
 Process the root node
 Traverse the right sub tree in in-order
Ex:-1:
Diagram:
Ex:-2:
Diagram:

20

12 40

6 15 35

13 38

14
3.Post order tree traversal:-
Binary tree can be traversal post order as follows.
Traverse the left sub tree in post order
Traverse the right sub tree in post order
Process the root node.
Ex:-1:
Diagram:

ex:-2:
Diagram: 20

12 40

6 15 35

13 38

14
Program: Write a program to crate a binary search tree and performs the following three operations
1. pre Order 2. In order 3.Pre Order

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

struct bstree
{
struct bstree *left;
int data;
struct bstree *right;
};
typedef struct bstree bst;

bst * insert(bst *root,int n)


{
if(root==NULL)
{
root=(bst *)malloc(sizeof(bst));
root->data=n;
root->left=root->right=NULL;
}
else if(root->data > n)
root->left=insert(root->left,n);
else if(root->data < n)
root->right=insert(root->right,n);
else
printf("\nNode Already Exists");

return root;
}
bst * createtree()
{
bst *root=NULL;
int n;
char ch;
do
{
printf("Enter the node :");
scanf("%d",&n);
root=insert(root,n);
printf("\nEnter Another node(y/n) :");
fflush(stdin);
scanf("%c",&ch);
}while(ch!='n');
printf("Tree is Created");
return root;
}
void preorder(bst *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}

void inorder(bst *root)


{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
void postorder(bst *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d\t",root->data);
}
}

void main()
{
bst *root=NULL;
clrscr();
printf("Create a Binary Search tree\n");
root=createtree();
printf("\nPre-Order :");
preorder(root);
printf("\nIn-Order :");
inorder(root);
printf("\nPost-Order :");
postorder(root);
getch();
}

You might also like

pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy