Data Structures
Data Structures
Data Structures
Definition
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;
};
nb1 nb2
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.
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;
};
}
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;
}
}
nd=nd->next;
}
return 0;
}
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;
}
sl *insertbefore(sl *first)
{
sl *nd,*before,*node;
int n;
if(first==NULL)
{
printf("List is Empty");
return first;
}
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;
}
printf("Node is Inserted");
}
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();
}
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;
}
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;
}
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();
}
}
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;
}
}
printf("\nList Nodes from Last to First :");
nd=getlast(first);
while(nd!=NULL)
{
printf("\t%d",nd->data);
nd=nd->prev;
}
}
nd=nd->next;
}
return 0;
}
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;
}
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;
}
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();
}
}
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;
}
nd=nd->next;
}while(nd!=first);
return 0;
}
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;
}
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
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:
#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:
case 2:
n=pop();
if(n!=0)
printf("%d is Deleted",n);
break;
case 3:
display();
break;
case 4:
exit(0);
}
getch();
}
}
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;
};
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:
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.
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.
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();
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct queue
{
int data;
struct queue *next;
};
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:
case 2:
n=dequeue();
if(n!=0)
printf("%d is Deleted",n);
break;
case 3:
display();
break;
case 4:
exit(0);
}
getch();
}
}
Circular Queue
#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:
case 2:
n=decqueue();
if(n!=0)
printf("%d is Deleted",n);
break;
case 3:
display();
break;
case 4:
exit(0);
}
getch();
}
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct cqueue
{
int data;
struct cqueue *next;
};
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:
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();
}
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]);
}
}
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]);
}
}
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]);
}
}
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.
#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]);
}
}
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.
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]);
}
}
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
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.
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
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:-
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;
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 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();
}