Introduction To Data Structures: What Is Data Structure?
Introduction To Data Structures: What Is Data Structure?
com
Note:
Example
1. Arrays
2. List (Linked List)
3. Stack
4. Queue
Example
1. Tree
2. Graph
3. Dictionaries
4. Heaps
5. Tries, Etc.,
Example
Operations
In a single linked list we perform the following operations...
1. Insertion
2. Deletion
3. Display
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 4: Implement the main method by displaying operations menu
and make suitable function calls in the main method to perform
user selected operation.
Insertion
In a single linked list, the insertion operation can be performed in three
ways. They are as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Deletion
In a single linked list, the deletion operation can be performed in three
ways. They are as follows...
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);
struct Node
{
int data;
struct Node *next;
}*head = NULL;
void main()
{
int choice,value,choice1,loc1,loc2;
clrscr();
while(1){
mainMenu: printf("\n\n********* MENU ************\n1. Insert\n2. Display\n3.
Delete\n4. Exit\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
while(1){
printf("Where you want to insert: \n1. At Beginning\n2. At End\n3.
Between\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the two values where you wanto
insert: ");
scanf("%d%d",&loc1,&loc2);
insertBetween(value,loc1,loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: \n1. From Beginning\n2. From
End\n3. Spesific\nEnter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd(value);
break;
case 3: printf("Enter the value which you wanto delete:
");
scanf("%d",&loc2);
removeSpecific(loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
break;
case 4: exit(0);
default: printf("\nWrong input!!! Try again!!\n\n");
}
}
}
void removeBeginning()
{
if(head == NULL)
printf("\n\nList is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
}
}
}
void removeEnd()
{
if(head == NULL)
{
printf("\nList is Empty!!!\n");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf("\nOne node deleted!!!\n\n");
}
}
void removeSpecific(int delValue)
{
struct Node *temp1 = head, *temp2;
while(temp1->data != delValue)
{
if(temp1 -> next == NULL){
printf("\nGiven node not found in the list!!!");
goto functionEnd;
}
temp2 = temp1;
temp1 = temp1 -> next;
}
temp2 -> next = temp1 -> next;
free(temp1);
printf("\nOne node deleted!!!\n\n");
functionEnd:
}
void display()
{
if(head == NULL)
{
printf("\nList is Empty\n");
}
else
{
struct Node *temp = head;
printf("\n\nList elements are - \n");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
Output
Circular Linked List
What is Circular Linked List?
In single linked list, every node points to its next node in the sequence
and the last node points NULL. But in circular linked list, every node
points to its next node in the sequence but the last node points to the
first node in the list.
That means circular linked list is similar to the single linked list except
that the last node points to the first node in the list
Example
Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 4: Implement the main method by displaying operations menu
and make suitable function calls in the main method to perform
user selected operation.
Insertion
In a circular linked list, the insertion operation can be performed in three
ways. They are as follows...
Deletion
In a circular linked list, the deletion operation can be performed in three
ways those are as follows...
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAtAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();
struct Node
{
int data;
struct Node *next;
}*head = NULL;
void main()
{
int choice1, choice2, value, location;
clrscr();
while(1)
{
printf("\n*********** MENU *************\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d",&choice1);
switch()
{
case 1: printf("Enter the value to be inserted: ");
scanf("%d",&value);
while(1)
{
printf("\nSelect from the following Inserting options\n");
printf("1. At Beginning\n2. At End\n3. After a Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the location after which you want
to insert: ");
scanf("%d",&location);
insertAfter(value,location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Inserting
option!!!\n");
}
}
case 2: while(1)
{
printf("\nSelect from the following Deleting options\n");
printf("1. At Beginning\n2. At End\n3. Specific Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: deleteBeginning();
break;
case 2: deleteEnd();
break;
case 3: printf("Enter the Node value to be deleted: ");
scanf("%d",&location);
deleteSpecic(location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Deleting
option!!!\n");
}
}
EndSwitch: break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select correct option!!!");
}
}
}
In double linked list, every node has link to its previous node and next
node. So, we can traverse forward by using next field and can traverse
backward by using previous field. Every node in a double linked list
contains three fields and they are shown in the following figure...
Here, 'link1' field is used to store the address of the previous node in the
sequence, 'link2' field is used to store the address of the next node in the
sequence and 'data' field is used to store the actual value of that node.
Example
Note:
☀ In double linked list, the first node must be always pointed by head.
☀ Always the previous field of the first node must be NULL.
☀ Always the next field of the last node must be NULL.
Operations
In a double linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three
ways as follows...
Deletion
In a double linked list, the deletion operation can be performed in three
ways as follows...
#include<conio.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertAtAfter(int,int);
void deleteBeginning();
void deleteEnd();
void deleteSpecific(int);
void display();
struct Node
{
int data;
struct Node *previous, *next;
}*head = NULL;
void main()
{
int choice1, choice2, value, location;
clrscr();
while(1)
{
printf("\n*********** MENU *************\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d",&choice1);
switch()
{
case 1: printf("Enter the value to be inserted: ");
scanf("%d",&value);
while(1)
{
printf("\nSelect from the following Inserting options\n");
printf("1. At Beginning\n2. At End\n3. After a Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: insertAtBeginning(value);
break;
case 2: insertAtEnd(value);
break;
case 3: printf("Enter the location after which
you want to insert: ");
scanf("%d",&location);
insertAfter(value,location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Inserting
option!!!\n");
}
}
case 2: while(1)
{
printf("\nSelect from the following Deleting options\n");
printf("1. At Beginning\n2. At End\n3. Specific Node\n4.
Cancel\nEnter your choice: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: deleteBeginning();
break;
case 2: deleteEnd();
break;
case 3: printf("Enter the Node value to be
deleted: ");
scanf("%d",&location);
deleteSpecic(location);
break;
case 4: goto EndSwitch;
default: printf("\nPlease select correct Deleting
option!!!\n");
}
}
EndSwitch: break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select correct option!!!");
}
}
}
Stack ADT
What is a Stack?
Stack is a linear data structure in which the insertion and deletion
operations are performed at only one end. In a stack, adding and
removing of elements are performed at single position which is known as
"top". That means, new element is added at top of the stack and an
element is removed from the top of the stack. In stack, the insertion and
deletion operations are performed based on LIFO (Last In First Out)
principle.
In a stack, the insertion operation is performed using a function
called "push" and deletion operation is performed using a function
called "pop".
In the figure, PUSH and POP operations are performed at top position in
the stack. That means, both the insertion and deletion operations are
performed at one end (i.e., at Top)
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10
becomes the bottom most element and 50 is the top most element. Top is
at 50 as shown in the image below...
Operations on a Stack
The following operations are performed on the stack...
1. Using Array
2. Using Linked List
When stack is implemented using array, that stack can organize only
limited number of elements. When stack is implemented using linked list,
that stack can organize unlimited number of elements.
Step 1: Include all the header files which are used in the program
and define a constant 'SIZE' with specific value.
Step 2: Declare all the functions used in stack implementation.
Step 3: Create a one dimensional array with fixed size (int
stack[SIZE])
Step 4: Define a integer variable 'top' and initialize with '-1'. (int
top = -1)
Step 5: In main method display menu with list of operations and
make suitable function calls to perform operation selected by the
user on the stack.
#define SIZE 10
void push(int);
void pop();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void push(int value){
if(top == SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else{
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop(){
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display(){
if(top == -1)
printf("\nStack is Empty!!!");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
Output
Stack using Linked List
The major problem with the stack implemented using array is, it works
only for fixed number of data values. That means the amount of data
must be specified at the beginning of the implementation itself. Stack
implemented using array is not suitable, when we don't know the size of
data which we are going to use. A stack data structure can be
implemented by using linked list data structure. The stack implemented
using linked list can work for unlimited number of values. That means,
stack implemented using linked list works for variable size of data. So,
there is no need to fix the size at the beginning of the implementation.
The Stack implemented using linked list can organize as many data values
as we want.
Example
In above example, the last inserted node is 99 and the first inserted node
is 25. The order of elements inserted is 25, 32,50 and 99.
Operations
To implement stack using linked list, we need to set the following things
before implementing actual operations.
Step 1: Include all the header files which are used in the program.
And declare all the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define a Node pointer 'top' and set it to NULL.
Step 4: Implement the main method by displaying Menu with list of
operations and make suitable function calls in the main method.
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else{
struct Node *temp = top;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
Output
Expressions
What is an Expression?
In any programming language, if we want to perform any calculation or to
frame a condition etc., we use a set of symbols to perform the task.
These set of symbols makes an expression.
Operands are the values on which the operators can perform the task.
Here operand can be a direct value or variable or address of memory
location.
Expression Types
Based on the operator position, expressions are divided into THREE types.
They are as follows...
1. Infix Expression
2. Postfix Expression
3. Prefix Expression
Infix Expression
In infix expression, operator is used in between operands.
Example
Postfix Expression
In postfix expression, operator is used after operands. We can say that
"Operator follows the Operands".
Example
Prefix Expression
In prefix expression, operator is used before operands. We can say that
"Operands follows the Operator".
Example
Any expression can be represented using the above three different types
of expressions. And we can convert an expression from one form to
another form like Infix to Postfix, Infix to Prefix, Prefix to Postfix and
vice versa.
Expression Conversion
Any expression can be represented using three types of expressions (Infix,
Postfix and Prefix). We can also convert one type of expression to
another type of expression like Infix to Postfix, Infix to Prefix, Postfix to
Prefix and vice versa.
Example
Consider the following Infix Expression to be converted into Postfix
Expression...
D=A+B*C
Step 1: The Operators in the given Infix Expression : = , + , *
Step 2: The Order of Operators according to their preference : * , +
,=
Step 3: Now, convert the first operator * ----- D = A + B C *
Step 4: Convert the next operator + ----- D = A BC* +
Step 5: Convert the next operator = ----- D ABC*+ =
DABC*+=
Infix to Postfix Conversion using Stack Data
Structure
To convert Infix Expression into Postfix Expression using a stack data
structure, We can use the following steps...
1. Read all the symbols one by one from left to right in the given
Infix Expression.
2. If the reading symbol is operand, then directly print it to the
result (Output).
3. If the reading symbol is left parenthesis '(', then Push it on to the
Stack.
4. If the reading symbol is right parenthesis ')', then Pop all the
contents of stack until respective left parenthesis is poped and
print each poped symbol to the result.
5. If the reading symbol is operator (+ , - , * , / etc.,), then Push it
on to the Stack. However, first pop the operators which are
already on the stack that have higher or equal precedence than
current operator and print them to the result.
Example
Consider the following Infix Expression...
(A+B)*(C-D)
The given infix expression can be converted into postfix expression using
Stack data Structure as follows...
The final Postfix Expression is as follows...
AB+CD-*
Postfix Expression Evaluation
A postfix expression is a collection of operators and operands in which the
operator is placed after the operands. That means, in a postfix expression
the operator follows the operands.
1. Read all the symbols one by one from left to right in the given Postfix
Expression
2. If the reading symbol is operand, then push it on to the Stack.
3. If the reading symbol is operator (+ , - , * , / etc.,), then perform TWO pop
operations and store the two popped oparands in two different variables
(operand1 and operand2). Then perform reading symbol operation using
operand1 and operand2 and push result back on to the Stack.
4. Finally! perform a pop operation and display the popped value as final
result.
Example
Consider the following Expression...
Queue ADT
What is a Queue?
Queue is a linear data structure in which the insertion and deletion
operations are performed at two different ends. In a queue data
structure, adding and removing of elements are performed at two
different positions. The insertion is performed at one end and deletion is
performed at other end. In a queue data structure, the insertion
operation is performed at a position which is known as 'rear' and the
deletion operation is performed at a position which is known as 'front'. In
queue data structure, the insertion and deletion operations are
performed based on FIFO (First In First Out) principle.
Example
Queue after inserting 25, 30, 51, 60 and 85.
Operations on a Queue
The following operations are performed on a queue data structure...
1. Using Array
2. Using Linked List
When a queue is implemented using array, that queue can organize only
limited number of elements. When a queue is implemented using linked
list, that queue can organize unlimited number of elements.
void enQueue(int);
void deQueue();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
}
Output
Queue using Linked List
The major problem with the queue implemented using array is, It will
work for only fixed number of data. That means, the amount of data
must be specified in the beginning itself. Queue using array is not
suitable when we don't know the size of data which we are going to use.
A queue data structure can be implemented using linked list data
structure. The queue which is implemented using linked list can work for
unlimited number of values. That means, queue using linked list can work
for variable size of data (No need to fix the size at beginning of the
implementation). The Queue implemented using linked list can organize
as many data values as we want.
Example
Step 1: Include all the header files which are used in the program.
And declare all the user defined functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both
to NULL.
Step 4: Implement the main method by displaying Menu of list of
operations and make suitable function calls in the main method to
perform user selected operation.
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
break;
case 2: delete(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void insert(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode -> next = NULL;
if(front == NULL)
front = rear = newNode;
else{
rear -> next = newNode;
rear = newNode;
}
printf("\nInsertion is Success!!!\n");
}
void delete()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else{
struct Node *temp = front;
while(temp->next != NULL){
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL\n",temp->data);
}
}
Output
Circular Queue
In a normal Queue Data Structure, we can insert elements until queue
becomes full. But once if queue becomes full, we can not insert the next
element until all the elements are deleted from the queue. For example
consider the queue below...
After inserting all the elements into the queue.
Now consider the following situation after deleting three elements from
the queue...
This situation also says that Queue is Full and we can not insert the new
element because, 'rear' is still at last position. In above situation, even
though we have empty positions in the queue we can not make use of
them to insert new element. This is the major problem in normal queue
data structure. To overcome this problem we use circular queue data
structure.
Step 1: Include all the header files which are used in the program
and define a constant 'SIZE' with specific value.
Step 2: Declare all user defined functions used in circular queue
implementation.
Step 3: Create a one dimensional array with above defined SIZE (int
cQueue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize
both with '-1'. (int front = -1, rear = -1)
Step 5: Implement main method by displaying menu of operations
list and make suitable function calls to perform operation selected
by the user on circular queue.
void enQueue(int);
void deQueue();
void display();
void main()
{
int choice, value;
clrscr();
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("\nEnter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nPlease select the correct choice!!!\n");
}
}
}
void enQueue(int value)
{
if((front == 0 && rear == SIZE - 1) || (front == rear+1))
printf("\nCircular Queue is Full! Insertion not possible!!!\n");
else{
if(rear == SIZE-1 && front != 0)
rear = -1;
cQueue[++rear] = value;
printf("\nInsertion Success!!!\n");
if(front == -1)
front = 0;
}
}
void deQueue()
{
if(front == -1 && rear == -1)
printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");
else{
printf("\nDeleted element : %d\n",cQueue[front++]);
if(front == SIZE)
front = 0;
if(front-1 == rear)
front = rear = -1;
}
}
void display()
{
if(front == -1)
printf("\nCircular Queue is Empty!!!\n");
else{
int i = front;
printf("\nCircular Queue Elements are : \n");
if(front <= rear){
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
else{
while(i <= SIZE - 1)
printf("%d\t", cQueue[i++]);
i = 0;
while(i <= rear)
printf("%d\t",cQueue[i++]);
}
}
}
Output
Double Ended Queue (Dequeue)
Double Ended Queue is also a Queue data structure in which the insertion
and deletion operations are performed at both the ends (frontand rear).
That means, we can insert at both front and rear positions and can delete
from both front and rear positions.
Double Ended Queue can be represented in TWO ways, those are as
follows...
void enQueue(int);
int deQueueFront();
int deQueueRear();
void enQueueRear(int);
void enQueueFront(int);
void display();
int queue[SIZE];
int rear = 0, front = 0;
int main()
{
char ch;
int choice1, choice2, value;
printf("\n******* Type of Double Ended Queue *******\n");
do
{
printf("\n1.Input-restricted deque \n");
printf("2.output-restricted deque \n");
printf("\nEnter your choice of Queue Type : ");
scanf("%d",&choice1);
switch(choice1)
{
case 1:
printf("\nSelect the Operation\n");
printf("1.Insert\n2.Delete from Rear\n3.Delete from Front\n4.
Display");
do
{
printf("\nEnter your choice for the operation in c deque:
");
scanf("%d",&choice2);
switch(choice2)
{
case 1: enQueueRear(value);
display();
break;
case 2: value = deQueueRear();
printf("\nThe value deleted is %d",value);
display();
break;
case 3: value=deQueueFront();
printf("\nThe value deleted is %d",value);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
}
printf("\nDo you want to perform another operation (Y/N):
");
ch=getch();
}while(ch=='y'||ch=='Y');
getch();
break;
case 2 :
printf("\n---- Select the Operation ----\n");
printf("1. Insert at Rear\n2. Insert at Front\n3. Delete\n4.
Display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice2);
switch(choice2)
{
case 1: enQueueRear(value);
display();
break;
case 2: enQueueFront(value);
display();
break;
case 3: value = deQueueFront();
printf("\nThe value deleted is %d",value);
display();
break;
case 4: display();
break;
default:printf("Wrong choice");
}
printf("\nDo you want to perform another operation (Y/N):
");
ch=getch();
} while(ch=='y'||ch=='Y');
getch();
break ;
}
printf("\nDo you want to continue(y/n):");
ch=getch();
}while(ch=='y'||ch=='Y');
}
void display()
{
int i;
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!")
else{
printf("\nThe Queue elements are:");
for(i=rear; i < front; i++)
{
printf("%d\t ",queue[i]);
}
}
}
Linear Search Algorithm
(Sequential Search)
What is Search?
Search is a process of finding a value in a list of values. In other words,
searching is the process of locating given value position in a list of values.
Example
Consider the following list of element and search element...
Linear Search Program in C Programming
Language
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i,sElement;
Example
Consider the following list of element and search element...
Binary Search Program in C Programming
Language
#include<stdio.h>
#include<conio.h>
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
first = 0;
last = size - 1;
middle = (first+last)/2;
Insertion Sort
Sorting is the process of arranging a list of elements in a particular order
(Ascending or Descending).
Example
Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need to
make (1+2+3+......+n-1) = (n (n-1))/2 number of comparisions in the
worst case. If the list already sorted, then it requires 'n' number of
comparisions.
Worst Case : O(n2)
Best Case : Ω(n)
Average Case : Θ(n2)
void main(){
getch();
}
Selection Sort
Selection Sort algorithm is used to arrange a list of elements in a
particular order (Ascending or Descending). In selection sort, the first
element in the list is selected and it is compared repeatedly with
remaining all the elements in the list. If any element is smaller than the
selected element (for Ascending order), then both are swapped. Then we
select the element at second position in the list and it is compared with
remaining all elements in the list. If any element is smaller than the
selected element, then both are swapped. This procedure is repeated till
the entire list is sorted.
Step 1: Select the first element of the list (i.e., Element at first
position in the list).
Step 2: Compare the selected element with all other elements in
the list.
Step 3: For every comparision, if any element is smaller than
selected element (for Ascending order), then these two are
swapped.
Step 4: Repeat the same procedure with next position in the list till
the entire list is sorted.
Sorting Logic
Following is the sample code for selection sort...
Example
Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need to make ((n-
1)+(n-2)+(n-3)+......+1) = (n (n-1))/2 number of comparisions in the
worst case. If the list already sorted, then it requires 'n' number of
comparisions.
void main(){
int size,i,j,temp,list[100];
clrscr();
getch();
}
Quick Sort
Quick Sort Program in C Programming
Language
#include<stdio.h>
#include<conio.h>
void main(){
int list[20],size,i;
quicksort(list,0,size-1);
getch();
}
temp = list[pivot];
list[pivot] = list[j];
list[j] = temp;
quickSort(list,first,j-1);
quickSort(list,j+1,last);
}
}