$(document).ready(function() { $('pre code').each(function(i, block) { hljs.highlightBlock(block); }); });

Data Structure Programs

Data Structure Programs for CS/IT (III rd Sem) of UPTU affiliated colleges
Program no. 1
Object: Write a ‘C’ program to implement a ‘1-D Array’ and perform following operations on
it:
 Insertion (At beginning, last and specific position)
 Deletion (Of beginning, last and specific Node)
 Traversing
Program:
/*C Program to implement a
-----------1D Array---------------*/
#include"stdio.h"
#include"conio.h"
#define MAXS 25
void Add(int []);
void Delete(int []);
void Print(int []);
static int counter=0;
void main()
{
int ARRAY[MAXS];
int ch;
ARRAY[counter]=NULL;
while(1)
{
clrscr();
printf("-------C Program for operations on ARRAY--------\n");
printf("\t1.Insert an element into array,\n");
printf("\t2.Delete an element from the array,\n");
printf("\t3.Print the array,\n");
printf("\t4.Exit.\n");
printf("--------------------------------------------------\n");
printf("\nCurrently, the number of elements in the Array is: %d", counter);
printf("\nEnter you choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: Add(ARRAY);
break;
case 2: Delete(ARRAY);
break;
case 3: Print(ARRAY);
break;
case 4: printf("\nThanks for using the program, press any key to exit...");
getch();
exit();
default:printf("\nSorry, Wrong choice! Try again please...");
getch();
}
}
}
void Add(int Arr[MAXS])
{
int ch, loc, num, i;
do
{
clrscr();
printf("\n-------------INSERTION Menu--------------\n");
printf("\t1.Insert at begining;\n");
printf("\t2.Insert at last;\n");
printf("\t3.Insert at specific location;\n");
printf("\t4.Exit.\n");
printf("\nEnter you choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("Enter the number to be inserted:");
scanf("%d", &num);
if(Arr[0]!=NULL)
{
counter++;
if(counter<=MAXS) { for(i=counter; i>0; i--)
Arr[i]=Arr[i-1];
}
else
printf("\nSorry, Overflow occuring!");
}
else
counter++;
Arr[0]=num;
Print(Arr);
break;
case 2: printf("Enter the number to be inserted:");
scanf("%d", &num);
Arr[counter++]=num;
Print(Arr);
break;
case 3: printf("Enter the location number:");
scanf("%d", &loc);
printf("Enter the number to be inserted:");
scanf("%d", &num);
if(counter<=MAXS) { ++counter; for(i=counter; i>loc; i--)
Arr[i]=Arr[i-1];
Arr[loc-1]=num;
}
else
printf("\nOverflow occuring!");
Print(Arr);
break;
case 4: break;
default:printf("Sorry, wrong choice. Try again please...");
getch();
break;
}
}while(ch!=4);
}
void Delete(int Arr[MAXS])
{
int ch, loc, num, i;
do
{
clrscr();
printf("\n-------------DELETION Menu--------------\n");
printf("\t1.DELETE at begining;\n");
printf("\t2.DELETE at last;\n");
printf("\t3.DELETE at specific location;\n");
printf("\t4.Exit.\n");
printf("\nEnter you choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: if(Arr[0]!=NULL)
{
for(i=0; Arr[i]!=00; i++)
Arr[i]=Arr[i+1];
counter--;
}
Print(Arr);
break;
case 2: if(Arr[counter]!=NULL)
Arr[counter--]=NULL;
Print(Arr);
break;
case 3: if(Arr[0]!=NULL)
{
printf("Enter the location number:");
scanf("%d", &loc);
for(i=loc-1; i<=counter; i++) Arr[i]=Arr[i+1]; } counter--; Print(Arr); break; case 4: break; default:printf("Sorry, wrong choice. Try again please..."); getch(); break; } }while(ch!=4); } void Print(int Arr[MAXS]) { int i; if(Arr[0]==NULL) printf("\nSorry, the array is empty."); else { printf("\nThe array curently contains:\n"); for(i=0; i
#include
#include"SLL.C"
void main()
{
int opt, loc;
START=NULL;
while(1)
{
clrscr();
printf("----C program implementation of Singly Lined List----\n");
printf("Main Menu:\n");
printf("------------------------------------------------------\n");
printf("1.Insert Node at begining;\n");
printf("2.Insert Node at last;\n");
printf("3.Insert Node at specific location;\n");
printf("4.Delete Node from begining;\n");
printf("5.Delete Node from last;\n");
printf("6.Delete Node from specific location;\n");
printf("7.Print the linked list;\n");
printf("8.Exit...\n");
printf("\nCurrently, the number of nodes is %d\n", LAST);
printf("Choose an option:");
scanf("%d", &opt);
switch(opt)
{
case 1: InsertNode(0);
PrintLL();
getch();
break;
case 2: InsertNode(LAST);
PrintLL();
getch();
break;
case 3: printf("\nInsert the location AFTER which you want to insert the node:");
scanf("%d", &loc);
InsertNode(loc);
PrintLL();
getch();
break;
case 4: PrintLL();
DeleteNode(0);
PrintLL();
getch();
break;
case 5: PrintLL();
DeleteNode(LAST);
PrintLL();
getch();
break;
case 6: PrintLL();
printf("\nWhich Node do you want to erase?(Node number):");
scanf("%d", &loc);
DeleteNode(loc);
PrintLL();
getch();
break;
case 7: printf("\nYou have chosen to view the list...\n");
PrintLL();
getch();
break;
case 8: printf("\nThanks for using the program.");
printf("\nPress any key to exit...");
getch();
exit();
}
}
}
SLL.C
#include
#include
void PrintLL();
void NodeCounter();
void InsertNode(int loc);
void DeleteNode(int loc);
struct LL
{
int item;
struct LL *next;
}*START;
static int LAST=0;
void InsertNode(int loc)
{
int data;
printf("\nEnter the Node item to be inserted:");
scanf("%d", &data);
if(loc==0)
{
InsertBegNode(data, loc);
printf("Node inserted succesfully at the begining...");
}
else if(loc==LAST)
{
InsertLastNode(data);
printf("Node inserted successfully at the last...");
}
else
{
InsertLocNode(data, loc);
printf("Node inserted successfully after location %d...", loc);
}
NodeCounter();
}
InsertBegNode(int data, int loc)
{
struct LL *Node;
Node= (struct LL *)malloc(sizeof(struct LL));
if(START==NULL)
{
printf("\nThe list is empty currently...\n");
printf("So, inserting data at begining of the list...\n");
}
Node->item=data;
if(loc==0)
Node->next=START;
else
Node->next=NULL;
START=Node;
return;
}
InsertLastNode(int data)
{
struct LL *Node, *TempPtr, *TempNode;
Node=(struct LL *)malloc(sizeof(struct LL));
TempPtr=(struct LL *)malloc(sizeof(struct LL));
TempPtr=START;
if(TempPtr==NULL)
InsertBegNode(data,0);
else
{
Node->item=data;
Node->next=NULL;
while(TempPtr!=NULL)
{
TempNode= TempPtr;
TempPtr= TempPtr->next;
}
TempNode->next=Node;
TempPtr=TempNode;
}
return;
}
InsertLocNode(int data, int loc)
{
struct LL *Node, *TempPtr, *TempNode;
int count=0;
Node= (struct LL *)malloc(sizeof(struct LL));
TempPtr= (struct LL *)malloc(sizeof(struct LL));
TempPtr= START;
if(TempPtr==NULL)
InsertBegNode(data, 0);
else
{
while(countnext;
count++;
}
Node->item= data;
Node->next= TempNode->next;
TempNode->next=Node;
}
return;
}
void DeleteNode(int loc)
{
int item;
printf("\nEnter the item to be deleted:");
scanf("%d", &item);
if(loc==0)
{
if(DeleteBegNode(item)==1)
{
printf("\nBegining Node of the list deleted!..");
--LAST;
}
else
printf("\nSorry, %d isn\'t at the begining of the list!..", item);
}
else if(loc==LAST)
{
if(DeleteLastNode(item)==1)
{
printf("\nLast Node of the list deleted!..");
--LAST;
}
else
printf("\nSorry, %d isn\'t at the last of the list!..", item);
}
else
{
if(DeleteLocNode(loc,item)==1)
{
printf("\nNode at location %d of the list deleted!..", loc);
--LAST;
}
else
printf("\nSorry, %d isn\'t at location %d in the list!..", item, loc);
}
}
DeleteBegNode(int item)
{
struct LL *TempNode;
TempNode= (struct LL *)malloc(sizeof(struct LL));
TempNode= START;
if(item==TempNode->item)
{
START=TempNode->next;
free(TempNode);
return(1);
}
else
return(0);
}
DeleteLastNode(int item)
{
struct LL *TempNode, *TempPtr;
TempPtr= (struct LL *)malloc(sizeof(struct LL));
TempNode= (struct LL *)malloc(sizeof(struct LL));
TempPtr= START;
TempNode= NULL;
if(START==NULL)
return(0);
else
{
while(TempPtr->next!=NULL)
{
TempNode=TempPtr;
TempPtr=TempPtr->next;
}
if(item==TempPtr->item)
{
if(TempNode==NULL)
{
START=NULL;
free(TempNode, TempPtr);
return(1);
}
else
{
TempNode->next=NULL;
free(TempPtr);
return(1);
}
}
else
return(0);
}
}
DeleteLocNode(int loc, int item)
{
struct LL *TempNode, *TempPtr;
int counter=1;
if(loc==1)
{
DeleteBegNode(item);
return(1);
}
else if(loc==LAST)
{
DeleteBegNode(item);
return(1);
}
else
{
TempPtr=(struct LL *)malloc(sizeof(struct LL));
TempNode=(struct LL *)malloc(sizeof(struct LL));
TempPtr=START;
while(counternext;
counter++;
}
if(item==TempPtr->item)
{
TempNode->next=TempPtr->next;
TempPtr=TempNode;
free(TempNode);
return(1);
}
else
return(0);
}
}
void PrintLL()
{
int i;
struct LL *TempPtr;
TempPtr=(struct LL *)malloc(sizeof(struct LL));
TempPtr=START;
if(TempPtr==NULL)
printf("\nThe list is EMPTY currently..");
else
{
printf("\nThe items currently in the list are:\n");
while(TempPtr!=NULL)
{
printf("%d ", TempPtr->item);
TempPtr=TempPtr->next;
}
}
free(TempPtr);
}
void NodeCounter()
{
int i;
struct LL *temp;
temp=(struct LL *)malloc(sizeof(struct LL));
temp=START;
for(LAST=0;temp!=NULL;LAST++)
temp=temp->next;
}

Program no. 3
Object: Write a ‘C’ program to implement ‘STACK’ using Array.
Program:
/*C program for implementation of
-------STACK using ARRAY----------*/
#include
#include
#define TOP 0 //0 is being taken as TOP of STACK
#define MAX_SIZE 30
PUSH();
void POP();
void printSTACK();
int STACK[MAX_SIZE];
static int count=0;
void main()
{
int num, opt, i, overflow;
char ch;
clrscr( );
STACK[TOP]=NULL;
do
{
clrscr( );
printf("\n\tC program implementation of STACK using Array");
printf("\n\t-----------------------------------------------");
printf("\n\tMain Menu");
printf("\n\t-----------------------------------------------\n");
printf("\t1.PUSH elements into the STACK (Max. %d elements),\n", MAX_SIZE);
printf("\t2.POP elements from STACK,\n");
printf("\t3.Print elements of STACK,\n");
printf("\t4.Exit.\n");
printf("\tEnter your option here:");
scanf("%d", &opt);
switch(opt)
{
case 1: do
{
overflow= PUSH( );
if(overflow==1)
break;
printf("\nElement inserted at the top.\n");
printf("Want to enter more items?...(y/n):");
scanf("%s", &ch);
}while(ch=='Y'||ch=='y');
printSTACK();
getch();
break;
case 2: if(STACK[TOP]==NULL)
printf("\nSorry, STACK is empty! So, POP not possible.");
else
{
POP();
printSTACK();
}
getch();
break;
case 3: if(STACK[TOP]==NULL)
printf("\nSorry, the STACK is empty.");
else
printSTACK();
getch();
break;
case 4: printf("Thanks for using this program. Press any key to exit...");
getch();
exit();
default: printf("\nSorry, wrong choice! Try again, please...");
getch();
}
}while(opt!=4);

}
PUSH()
{
int i;
if(count>=MAX_SIZE)
{
printf("\nSorry, Overflow occuring.\n");
return(1);
}
else
{
for(i=MAX_SIZE; i>0; i--)
STACK[i]=STACK[i-1];
printf("\nEnter the item at 0th (TOP) location:");
scanf("%d", &STACK[0]);
count++;
return(0);
}
}
void POP()
{
int i, top_ele;
top_ele=STACK[0];
for(i=0; STACK[i]!=NULL; i++)
STACK[i]=STACK[i+1];
printf("Element %d from TOP erased.", top_ele);
--count;
}
void printSTACK()
{
int i;
printf("\nThe STACK is:\n");
for(i=0; STACK[i]!=NULL; i++)
printf("%d ", STACK[i]);
}

Program no. 4
Object: Write a ‘C’ program to implement ‘STACK’ using Linked List.
Program:
/* C program for implementation of STACK using SINGLY LINKED LIST.*/
#include
#include
struct STACK
{
int data;
struct STACK *next;
}*TOP;
void PUSH();
void POP();
void printSTACK (struct STACK *);
void main()
{
int num, count=0, opt, i;
char ch;
TOP=NULL;
while(1)
{
clrscr()
printf("\n\t---------------------------------------------------");
printf("\n\tAvailable Options:");
printf("\n\t---------------------------------------------------\n");
printf("\t1.PUSH elements into the STACK,\n");
printf("\t2.POP elements from STACK,\n");
printf("\t3.Print elements of STACK,\n");
printf("\t4.Exit.\n");
printf("\tEnter your option here:");
scanf("%d", &opt);

switch(opt)
{
case 1: count=0;
do
{
printf("\nEnter the item to be inserted at TOP:");
PUSH();
count++;
printf("Want to enter more items?...(y/n):");
scanf("%s", &ch);
}while(ch=='Y'||ch=='y');
printSTACK(TOP);
getch();
break;
case 2: if(TOP==NULL)
printf("\nSorry, STACK is empty! POP operation not possible.");
else
{
POP();
printSTACK(TOP);
}
getch();
break;
case 3: printSTACK(TOP);
getch();
break;
case 4: printf("Thanks for using this program. Press any key to exit...");
getch();
exit();
default:printf("\nSorry, Wrong choice! Try again, please...");
getch();
}
}
}
void PUSH()
{
struct STACK *temp;
int num;
scanf("%d", &num);
if( (temp= (struct STACK *) malloc (sizeof(struct STACK)) )==NULL)
printf("\Sorry, Memory space isn't available! Insertion not possible.");
else
{
temp->data=num;
if(TOP==NULL)
{
temp->next=NULL;
TOP=temp;
}
else
{
temp->next=TOP;
TOP=temp;
}
}
}
void POP()
{
int top_item;
struct STACK *temp;
temp=(struct STACK *)malloc( sizeof(struct STACK) );
top_item= TOP->data;
temp= TOP;
TOP= TOP->next;
free(temp);
printf("Element %d from TOP erased.", top_item);
}
void printSTACK (struct STACK *d)
{
d= (struct STACK *)malloc(sizeof (struct STACK) );
d=TOP;
if(d==NULL)
printf("\nSorry, the STACK is empty...");
else
{
printf("\nThe STACK is:\n");
while(d!=NULL)
{
printf("%d\n", d->data);
d=d->next;
}
}
}


Program No. 5
Object: Write a C program to implement ‘QUEUE’ using Array.
Program:
/*C program for implementation of QUEUE using ARRAY*/
#include
#include
#define MAX_SIZE 25
void Insert_Queue(int);
void Delete_Queue();
void Print_Queue();
int QUEUE[MAX_SIZE];
static int R=0,F=0;
int main()
{
int ch, item;
while(1)
{
clrscr();
printf("\n------------Main Menu--------------\n");
printf("1.Insert elements into queue;\n");
printf("2.Delete elements from queue;\n");
printf("3.Print the queue;\n");
printf("4.Exit.\n");
printf("Enter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("Enter the element to be Inserted:");
scanf("%d", &item);
Insert_Queue(item);
getch();
break;
case 2: Delete_Queue(item);
getch();
break;
case 3: Print_Queue(QUEUE);
getch();
break;
case 4: printf("\nThanks for using the program. Press any key to exit...");
getch();
exit(0);
default:printf("Sorry, Wrong choice! Try again please..");
getch();
break;
}
}
}
void Insert_Queue(int item)
{
int i;
if(F==0&&R==0)
{
printf("\nQueue was empty! So, Inserting element at location 1 only.");
QUEUE[R]=item;
F=R;
R=R+1;
}
else if(R>MAX_SIZE)
printf("\nSorry, QUEUE size exceeding MAX. size of QUEUE. Insertion not possible.");
else
{
QUEUE[R]=item;
R=R+1;
}
Print_Queue();
}
void Delete_Queue()
{
int i, item;
if(F==0&&R==0)
printf("Sorry, the QUEUE is empty! Deletion not possible.");
else
{
printf("Enter the element to be Deleted:");
scanf("%d", &item);
if(QUEUE[F]==item)
{
QUEUE[F]=00;
F=F+1;
}
else
printf("\nSorry, item \'%d\' isn\'t located at FRONT of QUEUE.",item);
Print_Queue();
}
}
void Print_Queue()
{
int i;
if(F==R)
printf("The QUEUE is empty currently with no elements in it.");
else
{
printf("\nThe Queue currently consists of:\n");
for(i=F; iitem=data;
Node->next=Front;
Front=Node;
}
else
{
Node->item=data;
Node->next=NULL;
while(TempPtr!=NULL)
{
TempNode=TempPtr;
TempPtr=TempPtr->next;
}
TempNode->next=Node;
TempPtr=TempNode;
}
printf("\n%d inserted at the REAR of the QUEUE.", data);
}
void Delete()
{
struct QNODE *TempNode;
TempNode= (struct QNODE *)malloc(sizeof(struct QNODE));
TempNode= Front;
Front=TempNode->next;
free(TempNode);
}
void PrintQUEUE()
{
int i;
struct QNODE *TempPtr;
TempPtr= (struct QNODE *)malloc(sizeof(struct QNODE));
TempPtr= Front;
if(TempPtr==NULL)
printf("\nThe QUEUE is EMPTY currently..");
else
{
printf("\nThe current QUEUE is:\n");
while(TempPtr!=NULL)
{
printf("%d ", TempPtr->item);
TempPtr=TempPtr->next;
}
}
free(TempPtr);
}

Program No. 7
Object: Write a C program to convert an ‘Infix Expression’ to ‘postfix Expression’.
Program:
/* C program implementation for
Conversion of Infix expresssion to Postfix expression */
#include
#include
#include
#define MAXS 100
void POP();
void PUSH(char);
void evaluate(char eqn[MAXS]);
char exp[MAXS], finalExp[MAXS];
char STACK[MAXS];
static int TOP=-1;
void main()
{
clrscr();
printf("C program for Conversion of Infix Expression to Postfix Expressions using STACK\n");
printf("Enter the Arithematic expression:\n");
scanf("%s", exp);
printf("\nSymbol\t\t\tStack\t\t\tExpression\n");
printf("--------------------------------------------------------------------------------");
evaluate(exp);
printf("\n------------------------------------------------------------------------------");
printf("\nThe final POSTFIX expression is : %s.", finalExp);
getch();
}
void evaluate(char exp[MAXS])
{
int I ,j, k, Counter=0, tempCount;
static int Ex=0, Sx=25, Expx=52;
char Temp;
PUSH('(');
strcat(exp,")");
for(i=0; exp[i]!=NULL; i++)
{
Temp=exp[i];
if( (Temp>=65 && Temp<=91) || (Temp>=97&&Temp<=123) ) finalExp[Counter++]=Temp; else if(Temp=='(' || Temp=='^') PUSH(Temp); else if(Temp=='*' || Temp=='/') { while(STACK[TOP]==Temp || STACK[TOP]=='^') { finalExp[Counter++]=STACK[TOP]; POP(); } PUSH(Temp); } else if(Temp=='+' || Temp=='-') { while(STACK[TOP]=='*'||STACK[TOP]=='/'||STACK[TOP]=='^'||STACK[TOP]==Temp) { finalExp[Counter++]=STACK[TOP]; POP(); } PUSH(Temp); } else if(Temp==')') { while(STACK[TOP]!='(') { finalExp[Counter++]=STACK[TOP]; POP(); } POP(); } for(j=0; j<=i; j++) { gotoxy(Ex+j, i+7); printf("%c", exp[j]); } gotoxy(Sx,i+7); printf("%s",STACK); gotoxy(Expx, i+7); printf("%s", finalExp); }//End of FOR loop } void PUSH(char item) { STACK[++TOP]=item; } void POP() { STACK[TOP--]=NULL; } Program No. 9 Object: Write a C program to for ‘INORDER, PREORDER, and POSTORDER TRAVERSAL’ of a Binary Tree. Program: /* C Program implementation for INORDER, PREDORDER, and POSTORDER traversal of Binary Tree*/ # include
# include
# include
struct node
{
struct node *left;
int data;
struct node *right;
};
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
void main()
{
struct node *ptr;
int will, i, num;
ptr = NULL;
ptr->data=NULL;
clrscr();
printf("Enter the number of terms you want to add to the tree.");
scanf("%d", &will);
for(i=0; ileft = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{
if(num==(*p)->data)
{
printf("\nREPEATED ENTRY ERROR,VALUE REJECTED");
return;
}
else if(num<(*p)->data)
{
printf("\nDirected to left link.");
insert(&((*p)->left), num);
}
else
{
printf("\nDirected to right link.");
insert(&((*p)->right), num);
}
}
return;
}
void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d, ", p->data);
inorder(p->right);
}
else
return;
}
void preorder(struct node *p)
{
if(p!=NULL)
{
printf("%d, ", p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}
void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d, ", p->data);
}
else
return;
}

Program No. 10
Object: Write a C program to implement ‘Linear Search’ and ‘Binary Search’ in Linked List.
Program:
/* C program implementation of
---------Searching in Singly Linked List----------*/
#include
#include
#include "SLL.C"
#include "SEARCHLL.C"
void main()
{
int opt, loc;
START=NULL;
while(1)
{
clrscr();
printf("----C program implementation of Singly Lined List----\n");
printf("Main Menu:\n");
printf("------------------------------------------------------\n");
printf("1.Insert Node at begining;\n");
printf("2.Insert Node at last;\n");
printf("3.Insert Node at specific location;\n");
printf("4.Delete Node from begining;\n");
printf("5.Delete Node from last;\n");
printf("6.Delete Node from specific location;\n");
printf("7.Print the linked list;\n");
printf("8.Search a nnumber in the LIST by BINARY SEARCH Method;\n");
printf("9.Search a nnumber in the LIST by LINEAR LINEAR Method;\n");
printf("10.Exit...\n");
printf("\nCurrently, the number of nodes is %d\n", LAST);
printf("Choose an option:");
scanf("%d", &opt);
switch(opt)
{
case 1: InsertNode(0);
PrintLL();
getch();
break;
case 2: InsertNode(LAST);
PrintLL();
getch();
break;
case 3: printf("\nInsert the location AFTER which you want to insert the node:");
scanf("%d", &loc);
InsertNode(loc);
PrintLL();
getch();
break;
case 4: PrintLL();
DeleteNode(0);
PrintLL();
getch();
break;
case 5: PrintLL();
DeleteNode(LAST);
PrintLL();
getch();
break;
case 6: PrintLL();
printf("\nWhich Node do you want to erase?(Node number):");
scanf("%d", &loc);
DeleteNode(loc);
PrintLL();
getch();
break;
case 7: printf("\nYou have chosen to view the list...\n");
PrintLL();
getch();
break;
case 8: PrintLL();
BinarySearchLL(LAST, START);
getch();
break;
case 9: PrintLL();
LinearSearchLL(LAST, START);
getch();
break;
case 10:printf("\nThanks for using the program.");
printf("\nPress any key to exit...");
getch();
exit();
}
}
}
SEARCHLL.C
void BinarySearchLL(int, struct LL *);
void LinearSearchLL(int , struct LL *ptr);
void BinarySearchLL(int num_nodes, struct LL *ptr)
{
int beg=1, last, flag=0;
int numIter=0, data, i, mid;
struct LL *NEW;
NEW=(struct LL *)malloc(sizeof(struct LL));
if(ptr!=NULL)
{
printf("\nNOTE:LIST should be in sorted in ascending order.");
printf("\nEnter the number to be searched in the LIST:");
scanf("%d", &data);
last=num_nodes;
NEW=START;
mid=(beg+last)/2;
while(flag==0&&beg<=last) { numIter++; ptr=NEW; for(i=beg; inext;
if(ptr->item==data)
{
printf("\nItem found at location %d", mid);
flag=1;
}
else if(ptr->item>data)
{
last=mid-1;
NEW=START;
}
else
{
beg=mid+1;
NEW=ptr->next;
}
mid=(beg+last)/2;
}
if(flag==1)
printf("\nSearch successful!, Number of Iterations invovled:%d", numIter);
else
printf("\nItem doesnot exist in the current LINKED LIST...");
}
else
printf("Sorry, the LIST is EMPTY currently!...\n");
}
void LinearSearchLL(int num_nodes,struct LL *ptr)
{
int i, numIter=0, data, mid, flag=0;
struct LL *NEW;
if(ptr!=NULL)
{
printf("\nNOTE:LINEAR search can also be implemented to UNSORTED LIST.");
printf("\nEnter the number to be searched in the LIST:");
scanf("%d", &data);
for(i=0; iitem)
{
printf("\nItem found at location %d in LIST!", i+1);
flag=1;
}
numIter++;
ptr=ptr->next;
}
if(flag==1)
printf("\nSearch successful!, Number of Iterations invovled:%d", numIter);
else
printf("\nItem doesnot exist in the current LINKED LIST...");
}
else
printf("Sorry, the LIST is EMPTY currently!...\n");
}

No comments:

Post a Comment

Contact Me

Name

Email *

Message *