Sie sind auf Seite 1von 193

RACHIT GUPTA MCA 2ND SEM ROLL NO.

66 MCA 2011
/* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM --> TO WRITE A MENU DRIVEN PROGRAM WHICH ALLOWS FOLLOWING OPERATIONS ON ARRAYS : 1) Creating an integer array. 2) Displaying array. 3) Reversing the array. 4) Finding range of array. 5) Inserting an element. 6) Deleting an element. 7) Counting number of elements. 8) Searching array. */

#include<conio.h> #include<stdio.h> #define size 100 //defining array size

void create(int [],int); void display(int[],int); void reverse(int[],int); void range(int[],int); void insert_element(int[],int); void delete_element(int[],int); void count(int);
PRACTICAL FILE 2ND SEM Page 1

//prototype declaration of all U D F's

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


void search(int[],int); int i,j,k,loc,key,temp,choice,limit; //global declaration void main() { int ar[size]; clrscr(); printf("Befor performing operations on array, first create an array"); do { printf("\n\n\t\tEnter how many number of elements you want in array : "); scanf("%d",&limit); if(limit<=0||limit>size) { printf("Entered number is greater than size of array"); } } while(limit<=0||limit>size);

create(ar,limit); while(1) { fflush(stdin); clrscr(); printf("\n\n\n\n\t\t\t M A I N M E N U "); // Infinite while loop

printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); printf("\n\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); printf("\n\n\n\n\t******************************************************************");


PRACTICAL FILE 2ND SEM Page 2

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


printf("\n\t------------------------------------------------------------------"); printf("\n\n\t*\tPress 1 for displaying array. printf("\n\n\t*\tPress 2 for reversing the array. printf("\n\n\t*\tPress 3 for finding range of array. printf("\n\n\t*\tPress 4 for inserting an element. printf("\n\n\t*\tPress 5 for deleting an element. printf("\n\n\t*\tPress 6 to count number of elements. printf("\n\n\t*\tPress 7 to search an element in array. printf("\n\n\t*\tPress 0 to quit. *"); *"); *"); *"); *"); *"); *"); *");

printf("\n\t------------------------------------------------------------------"); printf("\n\t******************************************************************"); printf("\n\n\t\t\tEnter your choice please : "); scanf("%d",&choice); fflush(stdin); switch(choice) { //buffer memory clear. //switch case start.

case 2: reverse(ar,limit); case 3: range(ar,limit); case 1: display(ar,limit);

break; break; break;

// UDF CALLING.

case 4: insert_element(ar,limit); case 5: delete_element(ar,limit); case 6: count(limit); case 7: search(ar,limit); case 0: exit(0); break; break;

break; break;

PRACTICAL FILE 2ND SEM

Page 3

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


default : { printf("\n\n\t\t\tEnter valid choice.(SEE THE MENU)"); } } } } //default end. //switch case ends. //infinite while loop ends. //main ends here.

void display(int ar[],int s) //UDF FOR DISPLAYING ARRAY. { if(s<=0) { printf("The array is empty"); return; }

printf("\n\nThe actual array is : \n"); for(i=0;i<s;i++) { printf("\t%d",ar[i]); //PRINT the element of array one by one. } }

void reverse(int ar[],int sz) //UDF FOR REVERSING AN ARRAY. { if(sz<=0)


PRACTICAL FILE 2ND SEM Page 4

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


{ //check wether array is empty

printf("\n\n\t\tThe array is empty"); return; }

printf("\n\nThe reverse array is : \n"); for(i=sz-1;i>=0;i--) { printf("\t%d",ar[i]); } } void range(int ar[],int siz) { //UDF FOR FINDING RANGE OF ARRAY. //PRINTING REVERSE OF ARRAY.

int small=ar[0],large=ar[0]; clrscr(); if(siz==-1||siz==0) { printf("The array is empty"); return; } //check wether array is empty

for(i=0;i<siz;i++) { if(ar[i]<small) {
PRACTICAL FILE 2ND SEM Page 5

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


small=ar[i]; } } for(i=0;i<siz;i++) { if(ar[i]>large) { large=ar[i]; } } printf("\n\n\t\tThe largest element in array is : %d",large); printf("\n\n\t\tThe smallest element in array is : %d",small); printf("\n\n\t\t\tThe range of array is : %d",large-small); return; } void insert_element(int ar[],int si) { clrscr(); count(si); printf("\n\n\t\tEnter the location where you want to insert element : "); scanf("%d",&loc); if(loc>si+1||loc<=0) { printf("\n\n\t\tError: The location entered exceded/deceded the limit"); return; }
PRACTICAL FILE 2ND SEM Page 6

//smallest element found.

//largest element found.

//UDF FOR INSERTING ELEMENT.

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


printf("\n\n\t\tEnter the element to be inserted : "); scanf("%d",&key); for(i=si+1;i>=loc-1;i--) { ar[i+1]=ar[i]; } ar[loc-1]=key; printf("\n\n\t\tElement is inserted."); limit++; return; } void delete_element(int ar[],int sizz) { char ch; clrscr(); count(sizz); printf("\n\n\t\tEnter the location where you want to delete element : "); scanf("%d",&loc); if(loc>limit||loc<=0) { printf("\n\n\t\tError: The location entered exceded/deceded the limit."); return; } for(i=loc-1;i<=sizz;i++) { ar[i]=ar[i+1];
PRACTICAL FILE 2ND SEM

//shifting elements towards right

//putting desired element at desired location

//UDF FOR DELETING ELEMENTS.

//shifting elements to left.


Page 7

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


} printf("\n\n\t\tThe element is deleted\n\n"); for(i=0;i<sizz-1;i++) { printf("\t%d",ar[i]); } if(i==0) { printf("\n\n\t\tEmpty! \t\tEmpty! \t\tEmpty! \t\tEmpty! \t\tEmpty!"); } limit--; return; } void count(int li) { printf("\n\n\t\t The number of elements in array is : %d",li); return; } void create(int ar[],int limit) { printf("\n\n\t\tEnter elements of array :"); for(i=0;i<limit;i++) { scanf("%d",&ar[i]); } return;
PRACTICAL FILE 2ND SEM Page 8

//PRINTING ARRY AFTER

//check wether array is empty

//UDF FOR COUNTING NUMBER OF ELEMENTS.

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011


}

void search(int ar[],int l) { int item,flag=0; clrscr(); printf("\n\n\t\tEnter the element you want to search :"); scanf("%d",&item); for(i=0;i<l;i++) { if(ar[i]==item) { flag++; } } if(flag>0) printf("\n\n\t\t%d found in array %d times",item,flag); else printf("\n\n\t\t%d not found in array",item); return; } // end of search function.

PRACTICAL FILE 2ND SEM

Page 9

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011. 2ND. MCA

PROGRAM:- A MENU DRIVEN PROGRAM FOR ARRAY BASED STACK WHICH PERFORMS THE FOLLOWING OPERATIONS ON STACK:1.PUSH OPERATION 2.POP OPERATION 3.DISPLAY OPERATION */

#define max 100 #include<stdio.h> #include<conio.h> void push(int); void pop(); void display(); int stack[max],size,num,top=-1,choice,i; //GLOBAL DECLARATION //UDF DECLARED.

void main() {
PRACTICAL FILE 2ND SEM

//MAIN STARTS

Page 10

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); printf("\n\n\t\tEnter size of the stack:"); scanf("%d",&size); if(size>max||size<1) { printf("\n\n\t\tSize entered is invalid, enter correct size !!!\n"); return; } do { printf("\n\n\t\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_ *_*_*"); printf("\n\n\t\t\tPRESS 1 FOR PERFORMING PUSH OPERATION\n"); printf("\n\n\t\t\tPRESS 2 FOR PERFORMING POP OPERATION\n"); printf("\n\n\t\t\tPRESS 3 FOR VIEWING THE CONTENTS OF STACK\n"); printf("\n\n\t\t\tPRESS 0 TO EXIT\n"); printf("\n\n\t\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_ *_*_*"); printf("\n\n\n\t\t\tEnter your choice:"); scanf("%d",&choice); clrscr(); switch(choice) {
PRACTICAL FILE 2ND SEM Page 11

//SIZE IOF STACK INITILIZED //CHECK FOR SIZE OF STACK.

//DO-WHILE LOOP STARTS

//SWITCH STATS HERE

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 case 1: if(top==size-1) { printf("\n\n\t\tError! Stack Overflow\tError! Stack Overflow\tError! Stack Overflow"); return; } printf("\n\n\n\tEnter the number you want to insert at the top of the stack:"); scanf("%d",&num); push(num); break; case 2: pop(); break; case 3: display(); break; case 0: exit(0);

default: printf("\n\n\n\t\tEnter correct choice:\n"); } } while(choice!=4); } void push(int element) {


PRACTICAL FILE 2ND SEM Page 12

//switch case ends here //do-while loop ends here // main ends here //DEFINING FOR PUSH UDF

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); if(top==size-1) { printf("\n\n\t\tStack Overflow"); return; }

{ top++; stack[top]=element; //TOP OF STACK INCREMENTED //VALUE ASSIGNED TO TOP OF STACK

printf("\nElement %d inserted successfully into the stack\n",element); } return; } void pop() { clrscr(); if(top==-1) printf("\n\n\t\t Warning! the stack is empty."); else { printf("\n%d deleted successfully from the stack",stack[top]);
PRACTICAL FILE 2ND SEM Page 13

//DEFINITION OF PUSH ENDS HERE //DEFINITION FOR POP UDF

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 top=top-1; } return; } void display() { clrscr(); if(top==-1) { printf("\n\n\t\t There is noting in stack.Stack is empty."); return; } printf("\n\n\t\tStack elements are:\n"); for(i=top;i>=0;i--) STACK { printf("\n %d",stack[i]); } return; } //DEFINITION OF DISPLAY ENDS HERE //PRINTING ELEMENTS OF STACK TOP (LIFO) //FOR LOOP FOR PRINTING ELEMENTS OF //DEFINITION OF POP ENDS HERE //DEFINITION FOR DISPLAY UDF //TOP DECREMENTED AFTER POP OPERATION

PRACTICAL FILE 2ND SEM

Page 14

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter size of the stack:4

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1 FOR PERFORMING PUSH OPERATION

PRESS 2 FOR PERFORMING POP OPERATION

PRESS 3 FOR VIEWING THE CONTENTS OF STACK

PRESS 0 TO EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

Enter the number you want to insert at the top of the stack:222 Stack elements are:

222 111

PRACTICAL FILE 2ND SEM

Page 15

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Befor performing operations on array, first create an array. Enter how many number of elements you want in array : 5 Enter the elements of array : 111 222 333 444 555

PRACTICAL FILE 2ND SEM

Page 16

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
M A I N M E N U

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ****************************************************************** ------------------------------------------------------------------

* * * * * * * *

Press 1 for displaying array. Press 2 for reversing the array. Press 3 for finding range of array. Press 4 for inserting an element. Press 5 for deleting an element. Press 6 to count number of elements. Press 7 to search an element in array. Press 0 to quit.

* * * * * * * *

-----------------------------------------------------------------******************************************************************

Enter your choice please : 1 The actual array is : 111 222 333 444 555

PRACTICAL FILE 2ND SEM

Page 17

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
M A I N M E N U

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ****************************************************************** ------------------------------------------------------------------

* * * * * * * *

Press 1 for displaying array. Press 2 for reversing the array. Press 3 for finding range of array. Press 4 for inserting an element. Press 5 for deleting an element. Press 6 to count number of elements. Press 7 to search an element in array. Press 0 to quit.

* * * * * * * *

-----------------------------------------------------------------******************************************************************

Enter your choice please : 7

Enter the element you want to search :444

444 found in array 1 times.


PRACTICAL FILE 2ND SEM Page 18

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011. : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR ORDINARY QUEUE WHICH PERFORMS THE FOLLOWING OPERATIONS :1.ENQUEUE OPERATION 2.DEQUEUE OPERATION 3.DISPLAY OPERATION 0.EXIT */

#include<stdio.h> #include<conio.h> #define max 100 void enqueue(int); void dequeue(); void display(); int queue[max],front=-1,size,rear=-1,choice,num,element,i; //global declaration void main() { clrscr(); printf("\nEnter size of the queue:");
PRACTICAL FILE 2ND SEM Page 19

//udf prototyping.

//main starts here

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&size); if(size>max||size<1) { printf("Error! Size entered is not correct to handle.\n\nplease enter correct size(less than %d and greater than 0)",max); getch(); return; } do { printf("\n\n\t\t\t\t M A I N M E N U"); printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\tPRESS 1. TO INSERT ELEMENTS IN QUEUE (ENQUEUE OPERATION)."); printf("\n\n\t\tPRESS 2. TO DELETE ELEMENTS IN QUEUE (DENQUEUE OPERATION)."); printf("\n\n\t\tPRESS 3. TO SEE THE QUEUE"); printf("\n\n\t\tPRESS 0. EXIT"); //infinite do-while loop starts. //size of stack initilized. //checking for correct size.

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&choice);


PRACTICAL FILE 2ND SEM

//getting the choice of user


Page 20

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 switch(choice) { case 1: if(rear==size-1) { printf("\n\n\t\tYou cann't insert any more,Queue is full!"); return; } printf("\n\n\tEnter the number you want to insert at the rear of the queue:"); scanf("%d",&num); enqueue(num); break; case 2: dequeue(); break; case 3: display(); break; case 0: exit(); break; default: { printf("\n\n\t\tEnter correct choice:\n"); return; }
PRACTICAL FILE 2ND SEM

//switch starts here

//enqueue called here

//dequeue called

//display called

//default ends here


Page 21

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } } while(choice!=4); getch(); return; } //main ends here. //switch ends here //do-while loop ends here

void enqueue(int num) { clrscr(); if(rear==size-1) {

//definition of enqueue

printf("\n\n\t\tWarning ! Queue is full."); return; } else { if(front==-1) front++; rear=rear+1; queue[rear]=num; //incrementing rear //inserting number at rear end.

printf("\n\n\t\t %d inserted sucessfully",num); }


PRACTICAL FILE 2ND SEM Page 22

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 return; getch(); } void dequeue() { clrscr(); if(front==rear) { printf("\n\n\t\t element %d is deleted successfully",queue[front]); front=rear=-1; printf("\n\n\t\t Warning! Queue is gone empty."); return; } else element=queue[front]; printf("\n\n\t\t element %d is dequeued successfully",queue[front]); front=front+1; getch(); return; } void display() {
PRACTICAL FILE 2ND SEM Page 23

//definition of enqueue ends here //defining dequeue

//definition of dequeue ends here //defining the display

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); if(front==rear==-1) { printf("\n\n\t\ttTHERE IS NOTNING TO SEE.QUEUE IS EMPTY !"); return; } printf("\n\n\t\t ELEMENTS OF QUEUE ARE:\n"); for(i=front;i<=rear;i++) { printf("%d\t",queue[i]); } getch(); return; } //definition of display ends here //printing the queue elements one by one. //for loop starts.

PRACTICAL FILE 2ND SEM

Page 24

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter size of the queue:5 MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN QUEUE (ENQUEUE OPERATION).

PRESS 2. TO DELETE ELEMENTS IN QUEUE (DENQUEUE OPERATION).

PRESS 3. TO SEE THE QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1 Enter the element to be inserted at rear of queue :111

Enter the element to be inserted at rear of queue :222

PRACTICAL FILE 2ND SEM

Page 25

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN QUEUE (ENQUEUE OPERATION).

PRESS 2. TO DELETE ELEMENTS IN QUEUE (DENQUEUE OPERATION).

PRESS 3. TO SEE THE QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice: 3 ELEMENTS OF QUEUE ARE: 111 222

PRACTICAL FILE 2ND SEM

Page 26

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN QUEUE (ENQUEUE OPERATION).

PRESS 2. TO DELETE ELEMENTS IN QUEUE (DENQUEUE OPERATION).

PRESS 3. TO SEE THE QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice: 2

Element 111 is dequeued successfully.

PRACTICAL FILE 2ND SEM

Page 27

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR ARRAY BASED circular QUEUE WHICH PERFORMS THE FOLLOWING OPERATIONS ON QUEUE:-

1.ENQUEUE OPERATION 2.DEQUEUE OPERATION 3.DISPLAY OPERATION 0.EXIT */

#include<conio.h> #include<stdio.h> #define max 50 void cirqueueinsert(int); void cirqueuedelete(); void cirqueuedisplay(); int cirqueue[max],front=-1,size,rear=-1,choice,num,element,i; //global declaration void main() {
PRACTICAL FILE 2ND SEM Page 28

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); printf("\nEnter size of the queue:"); scanf("%d",&size); if(size>max||size<1) { printf("Error! Size entered is not correct to handle.\n\nplease enter correct size(less than %d and greater than 0)",max); getch(); return; } do { clrscr(); printf("\n\n\t\t\t\t M A I N M E N U"); //infinite do-while loop starts. //size of cicular queue initilized. //checking for correct size.

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\tPRESS 1. TO INSERT ELEMENTS IN CIRCULAR QUEUE."); printf("\n\n\t\tPRESS 2. TO DELETE ELEMENTS IN CIRCULAR QUEUE."); printf("\n\n\t\tPRESS 3. TO SEE THE CIRCULAR QUEUE"); printf("\n\n\t\tPRESS 0. EXIT");

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" );
PRACTICAL FILE 2ND SEM Page 29

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("\n\n\tEnter the number you want to insert at the rear of the queue:"); scanf("%d",&num); cirqueueinsert(num); break; case 2: cirqueuedelete(); break; case 3: cirqueuedisplay(); break; case 0: exit(); break; default: { printf("\n\n\t\tEnter correct choice:\n"); return; } } } while(choice!=4); getch();
PRACTICAL FILE 2ND SEM Page 30

//getting the choice of user //switch starts here

//cirqueueinsert called here

// cirqueuedelete called

//display called

//default ends here //switch ends here //do-while loop ends here

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 return; } //main ends here.

void cirqueueinsert(int element) { clrscr(); if((front==0)&&(rear==size-1)) { printf("\n\n\t\tWarning ! Queue is full."); getch(); return; } if(front==rear+1) { printf("\n\n\t\tWarning ! Queue is full."); getch(); return; } if(front==-1) { front++; } if(rear==size-1)
PRACTICAL FILE 2ND SEM Page 31

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

rear=0; else rear++; cirqueue[rear]=element; printf("\n\n\tElement is inserted sucessfully at the rear of circular queue"); getch(); return; } void cirqueuedelete() { clrscr(); if(front==-1) { printf("\n\n\t\t Warning! Queue is gone empty."); getch(); return; } if(front!=rear) printf("\n\n\t\t element %d is deleted successfully",cirqueue[front]); if(front==rear) {
PRACTICAL FILE 2ND SEM Page 32

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\t\t element %d is deleted successfully",cirqueue[front]); front=-1; rear=-1; } if(front==size-1) { front=-1; } else { front++; } getch(); return; } void cirqueuedisplay() { clrscr(); if(front==-1) { printf("\n\n\t\tTHERE IS NOTNING TO SEE.QUEUE IS EMPTY !"); getch();
PRACTICAL FILE 2ND SEM Page 33

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 return; } printf("\n\n\t\t ELEMENTS OF QUEUE ARE:\n"); i=front; do { printf("%d\t",cirqueue[i]); if(i==rear) break; else if(i==size-1) i=0; else i++; } while(i<=rear); getch(); return; } //definition of display ends here //printing the queue elements //do=while loop starts.

PRACTICAL FILE 2ND SEM

Page 34

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter the size of the circular queue : 3 MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN CIRCULAR QUEUE.

PRESS 2. TO DELETE ELEMENTS IN CIRCULAR QUEUE.

PRESS 3. TO SEE THE CIRCULAR QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

Enter the number you want to insert at the rear of the queue: 111 The number is inserted successfully.

PRACTICAL FILE 2ND SEM

Page 35

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN CIRCULAR QUEUE.

PRESS 2. TO DELETE ELEMENTS IN CIRCULAR QUEUE.

PRESS 3. TO SEE THE CIRCULAR QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

Enter the number you want to insert at the rear of the queue: 222 The number is inserted successfully.

PRACTICAL FILE 2ND SEM

Page 36

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN CIRCULAR QUEUE.

PRESS 2. TO DELETE ELEMENTS IN CIRCULAR QUEUE.

PRESS 3. TO SEE THE CIRCULAR QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

Enter the number you want to insert at the rear of the queue: 333 The number is inserted successfully.

PRACTICAL FILE 2ND SEM

Page 37

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INSERT ELEMENTS IN CIRCULAR QUEUE.

PRESS 2. TO DELETE ELEMENTS IN CIRCULAR QUEUE.

PRESS 3. TO SEE THE CIRCULAR QUEUE

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice : 3

ELEMENTS OF CIRCULAR QUEUE ARE: 111 222 333

PRACTICAL FILE 2ND SEM

Page 38

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR ARRAY BASED DOUBLE ENDE QUEUE WHICH PERFORMS THE FOLLOWING OPERATIONS ON QUEUE:-

1.INPUT RESTRICT QUEUE. 2.OUTPUT RESTRICT QUEUE. */ #define max 50 #include<conio.h> #include<stdio.h> void input_restrict_queue(); void output_restrict_queue(); void input_right(int); void input_left(int); void delete_right(); void delete_left(); void display(); int deque[max],front=-1,rear=-1,size,element,choice; void main()
PRACTICAL FILE 2ND SEM Page 39

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { clrscr(); printf("\nEnter size of the queue:"); scanf("%d",&size); if((size>max)||(size<1)) { printf("Error! Size entered is not correct to handle.\n\nplease enter correct size(less than %d and greater than 0)",max); return; } do { clrscr(); printf("\n\n\t\t\t\t M A I N M E N U"); //infinite do-while loop starts. //size of deque initilized. //checking for correct size.

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\tPRESS 1. TO DEAL WITH INPUT RESTRICT QUEUE."); printf("\n\n\t\tPRESS 2. TO DEAL WITH OUTPUT RESTRICT QUEUE."); printf("\n\n\t\tPRESS 0. EXIT");

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\t\tEnter your choice:");


PRACTICAL FILE 2ND SEM Page 40

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&choice); switch(choice) { case 1: input_restrict_queue(); break; case 2: output_restrict_queue(); break; case 0: exit(); //getting the choice of user //switch starts here

default: printf("Enter valid choice"); } //switch ends here. //while ends here.

}while(choice!=0);

//main ends here.

void input_restrict_queue() { int ch; do {


PRACTICAL FILE 2ND SEM Page 41

//infinite do-while loop starts.

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); printf("\n\n\t\t\t\t S U B M E N U");

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\tPRESS 1. TO INPUT FROM RIGHT."); printf("\n\n\t\tPRESS 2. TO DELETE FROM RIGHT."); printf("\n\n\t\tPRESS 3. TO DELETE FROM LEFT."); printf("\n\n\t\tPRESS 4. TO SEE THE DOUBLE ENDED QUEUE."); printf("\n\n\t\tPRESS 0. TO RETURN TO MAIN MENU");

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to be inserted"); scanf("%d",&element); input_right(element); break; case 3: delete_left(); break; case 2: delete_right();
PRACTICAL FILE 2ND SEM Page 42

//getting the choice of user //switch starts here

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 break; case 4: display(); break; case 0 : return; default: printf("Enter valid choice");

//switch ends here. //while ends here.

}while(1); }

//main ends here.

void output_restrict_queue() { int ch; do { clrscr(); printf("\n\n\t\t\t\t S U B M E N U"); printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\tPRESS 1. TO INPUT FROM RIGHT."); printf("\n\n\t\tPRESS 2. TO INPUT FROM LEFT."); printf("\n\n\t\tPRESS 3. TO DELETE FROM LEFT.");
PRACTICAL FILE 2ND SEM Page 43

//infinite do-while loop starts.

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\t\tPRESS 4. TO SEE THE DOUBLE ENDED QUEUE."); printf("\n\n\t\tPRESS 0. TO RETURN TO MAIN MENU");

printf("\n\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*" ); printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to be inserted"); scanf("%d",&element); input_right(element); break; case 2: printf("Enter the element to be inserted"); scanf("%d",&element); input_left(element); break; case 3: delete_left(); break; case 4: display(); break; case 0: return; default:
PRACTICAL FILE 2ND SEM Page 44

//getting the choice of user //switch starts here

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("Enter valid choice"); } //switch ends here. //while ends here.

}while(1); }

//main ends here.

void input_right(int element) { clrscr(); if((front==0) && (rear==size-1)) { printf("\n\n\tOverflow!\tOverflow!\tOverflow!\tOverflow!"); getch(); return; } if(front==rear+1) { printf("\n\n\tOverflow!\tOverflow!\tOverflow!\tOverflow!"); getch(); return; } if(front==-1) { front=0; rear=0;
PRACTICAL FILE 2ND SEM Page 45

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } else if(rear==size-1) { rear=0; } else { rear++; } deque[rear]=element; printf("Element is inserted sucessfully"); } void delete_left() { clrscr(); if(front==-1) { printf("\n\n\tUNDERFLOW !\tUNDERFLOW !\tUNDERFLOW !\tUNDERFLOW !"); return; } printf("%d is deleted sucessfully",deque[front]); if(front==rear)
PRACTICAL FILE 2ND SEM Page 46

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { front=-1; rear=-1; } else if(front==size-1) { front=0; } else { front++; } } void input_left(int element) { clrscr(); if(((front==0)&&(rear==size-1))||(front==rear+1)) { printf("\n\n\toverflow!\toverflow!\toverflow!\toverflow!"); return; } if(front==-1) {
PRACTICAL FILE 2ND SEM Page 47

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 front=0; rear=0; } else if(front==0) { front=size-1; } else { front--; } deque[front]=element; printf("\n\n\t\tElement is inserted sucessfully"); } void delete_right() { clrscr(); if(front==-1) { printf("\n\n\tUNDERFLOW!\tUNDERFLOW!\tUNDERFLOW!\tUNDERFLOW!"); return; } if(front==rear)
PRACTICAL FILE 2ND SEM Page 48

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { front=rear=-1; } else if(rear==0) { rear=size-1; } else { rear--; } } void display() { int fp,rp; clrscr(); fp=front; rp=rear; printf("\n\n\t\tThe deque is as follow :"); if(fp<=rp) { while(fp<=rp)
PRACTICAL FILE 2ND SEM Page 49

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { printf("%d\t",deque[fp]); fp++; } } else { while(fp<=size-1) { printf("%d\t",deque[fp]); fp++; fp=0; } } while(fp<=rp) { printf("%d\t",deque[fp]); fp++; } getch(); }

PRACTICAL FILE 2ND SEM

Page 50

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter size of the queue : 3

MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO DEAL WITH INPUT RESTRICT QUEUE.

PRESS 2. TO DEAL WITH OUTPUT RESTRICT QUEUE.

PRESS 0. EXIT

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

PRACTICAL FILE 2ND SEM

Page 51

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
SUB MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INPUT FROM RIGHT.

PRESS 2. TO DELETE FROM RIGHT.

PRESS 3. TO DELETE FROM LEFT.

PRESS 4. TO SEE THE DOUBLE ENDED QUEUE.

PRESS 0. TO RETURN TO MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1

PRACTICAL FILE 2ND SEM

Page 52

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
SUB MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INPUT FROM RIGHT.

PRESS 2. TO DELETE FROM RIGHT.

PRESS 3. TO DELETE FROM LEFT.

PRESS 4. TO SEE THE DOUBLE ENDED QUEUE.

PRESS 0. TO RETURN TO MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:1 Enter the element to be inserted 123

PRACTICAL FILE 2ND SEM

Page 53

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
The deque is as follow :123

SUB MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

PRESS 1. TO INPUT FROM RIGHT.

PRESS 2. TO INPUT FROM LEFT.

PRESS 3. TO DELETE FROM LEFT.

PRESS 4. TO SEE THE DOUBLE ENDED QUEUE.

PRESS 0. TO RETURN TO MAIN MENU

*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*

Enter your choice:0

PRACTICAL FILE 2ND SEM

Page 54

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011. 2ND. MCA

PROGRAM:- A MENU DRIVEN PROGRAM FOR SINGLE LINKED LIST WHICH PERFORMS THE FOLLOWING OPERATIONS ON SINGLE LINKED LIST:1.CREATE 2.ADD NODES 3.DELETE NODES 4.COUNT NUMBER OF NODES 5.SEARCH DATA FIELD 6. DISPLAY. */

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *link;
PRACTICAL FILE 2ND SEM

//header file included

//structure declared

Page 55

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 }*head,*p,*q,*temp,*t; //global declaration of structure

int i=0,cnt=0;

void create(); void display(); void addfirst(); void addlast(); void addbetween(); void delfirst(); void delbetween(); void reverse(); count(); void dellast(); void search();

//udfs declared

void main() //MAIN STARTS { int choice; clrscr(); do {


PRACTICAL FILE 2ND SEM Page 56

//do while loop strats

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\n\t\t*********************************************"); printf("\n\n\t\tPRESS 1. CREATE A NODE FOR SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 2. DISPLAY THE NODES OF SINGLE LIST LIST\n"); printf("\n\t\tPRESS 3. ADD A NODE AT BEGINNING OF SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 4. ADD A NODE AT THE END OF THE SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 5. ADD A NODE IN BETWEEN THE SINGLE LINK LIST\n"); printf("\n\t\tPRESS 6. DELETE A NODE AT BEGINNING of SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 7. DELETE A NODE IN BETWEEN SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 8. DELETE A NODE AT THE END OF SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 9. TO REVERSE A SINGLE LINKED LIST\n"); printf("\n\t\tPRESS 10. TO COUNT THE NUMBER OF NODES.\n"); printf("\n\t\tPRESS 11. TO SEARCH THE DATA FIELDS OF NODES."); printf("\n\n\t\tPRESS 0. EXIT\n"); printf("\n\n\t\t*********************************************"); printf("\n\n\n\t\t\tEnter your choice:\n"); scanf("%d",&choice); switch(choice) { //switch strts here //getting choice

case 1: create(); break; case 2: display(); break;


PRACTICAL FILE 2ND SEM Page 57

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 case 3: addfirst(); break; case 4: addlast(); break; case 5: addbetween(); break; case 6: delfirst(); break; case 7: delbetween(); break; case 8: dellast(); break; case 9: reverse(); break; case 10:count(); break; case 11: search(); break; case 0: exit(0); default: printf("\n\tEnter correct choice:\n"); } //switch ends
PRACTICAL FILE 2ND SEM Page 58

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 }while(choice!=0);//do-while ends getch(); } //main ends here

void create() { clrscr();

//creare starts here

printf("\n\n\t\tCREATING NODE OF SINGLE LINKED LIST\n"); if(i==0) { p=malloc(sizeof(struct node)); //craete node

printf("\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&p->data); p->link=NULL; head=p; } // end of if statement else { //else strats //create node //assign null to p link

q=malloc(sizeof(struct node));

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); p->link=q;


PRACTICAL FILE 2ND SEM Page 59

//get data from user

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 q->link=NULL; p=q; } i++; //END OF ELSE //i incremented //null assigned to q link

printf("\nData inserted successfully in the node\n"); }

void display() { temp=head; if(temp==NULL) {

//display starts

//check for empty list

printf("\n\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!"); return; } //end of if printf("\nElement of Single linked list are:\n"); temp=head; while(temp!=NULL) { printf("\t%d",temp->data); temp=temp->link; } // end of do-while loop
PRACTICAL FILE 2ND SEM Page 60

//printing data of linked list

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } //end of display function

void addfirst() { clrscr(); temp=head; if(temp==NULL) {

//addfirst strats

//assign head to temp

printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); q->link=temp; head=q; printf("\n\n\t\tNODE ADDED TO THE LIST"); i++; getch(); } //getting dta from user

void addlast()

//add last start


Page 61

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { clrscr(); temp=head; if(temp==NULL) { printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node //assign head to temp

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); q->link=NULL; //null assigned to q link while(temp->link!=NULL) //while starts { temp=temp->link; } temp->link=q; p=temp; printf("NODE ADDED SUCCESSFULLY\n"); i++; getch(); }
PRACTICAL FILE 2ND SEM Page 62

//jump to next node

//while ends

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void addbetween() { int c,pos,j; clrscr(); temp=head; if(temp==NULL) {

//addbetween defined

printf("LINKED LIST IS EMPTY ! \n"); return; } printf("\nENTER THE POSITION WHERE YOU WANT TO ADD NODE\n"); scanf("%d",&pos); c=count(); if(pos<1||pos>c) { printf("ERROR! ENTERED POSITION IS OUTSIDE LIMIT"); return; } q=malloc(sizeof(struct node)); //creating node //check for position

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); //getting data from user
PRACTICAL FILE 2ND SEM Page 63

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 for(j=1;j<pos-1;j++) //for loop strats { temp=temp->link; } q->link=temp->link; temp->link=q; p=temp; printf("\n\n\t\tNODE ADDED SUCCESSFULLY"); i++; getch(); } //q assigned to temp link //jump to next node

void delfirst() { clrscr(); temp=head; if(temp==NULL) {

//del fisrt defination

printf("\n\n\t\tTHE LINK LIST IS EMPTY"); getch(); return; }


PRACTICAL FILE 2ND SEM Page 64

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 head=temp->link; //assign temp link to head free(temp); //temp freed

printf("DELETED NODE FROM LIST"); i--; getch(); }

void delbetween() { int c,j,pos; clrscr(); temp=head; if(temp==NULL) {

//delbetween definition

//chech for empty list

printf("\n\n\t\tTHE LINK LIST IS EMPTY"); return; } printf("\n\n\tEnter the position where u want to delete node"); scanf("%d",&pos); c=count(); if(pos<1||pos>c) //check for correct position {
PRACTICAL FILE 2ND SEM Page 65

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\t\tERROR! Position entered is outside limit."); return; } for(j=1;j<pos-1;j++) { temp=temp->link; } temp->link=temp->link->link; //putting link of 2nd node to first free(temp->link); //free up memory of t //jump to next node //for loop starts

printf("\n\n\t\t NODE DELETED FROM LIST"); i--; getch(); } void reverse() { int j=1,k; clrscr(); t=head; //assign head to temp //reverse starts

if(t==NULL) //check for empty list { printf("\n\n\t\tLIST IS EMPTY"); return;


PRACTICAL FILE 2ND SEM Page 66

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } //cnt=count(); printf("\n\tThe reverse of the linked list is :\n");

while(j<=i) //while loop strats(i is meant for number of nodes. { t=head; for(k=1;k<=i-j;k++) { t=t->link; } printf("%d\t",t->data); //printing data in reverse order j++; } } //reverse ends //jump to next node //for loop strats

count() { int c=0; temp=head;

//count starts

while(temp!=NULL) //while starts {


PRACTICAL FILE 2ND SEM Page 67

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 c++; temp=temp->link; //jump to next node } printf("the value of count is %d",c); } //

void dellast() { int j=1,c; c=count(); temp=head; if(temp==NULL) {

//defination of dellast start

//assign head to temp

printf("\n\n\t\tWarning! list is empty"); return; } if(c==1) { head=temp->link; free(temp); printf("\n\n\t\tNode is deleted"); }


PRACTICAL FILE 2ND SEM Page 68

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 else { while(temp->link->link!=NULL) { temp=temp->link; } //jump to next node //while starts

//while ends //null assigned to last node

temp->link=NULL; p=temp; free(temp->link);

//free the memory

printf("\n\n\t\tNode is deleted"); } i--; getch(); } void search() //search definition start { int item,found=0; clrscr(); printf("\n\n\t\tEnter the data to de searched:"); scanf("%d",&item); temp=head; while(temp!=NULL)
PRACTICAL FILE 2ND SEM

//while loop statrs


Page 69

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { if(temp->data==item) { found++; temp=temp->link; } else { temp=temp->link; } //else ends //if ends //found incremented //jump to next node //checking the data field

}// end of while loop if(found>0) { printf("\n\n\t\tItem is present/found"); printf("\n\n\t\t%d found in list %d times",item,found); } else { printf("Item not present"); getch(); } } //end of search module
Page 70

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
********************************************* PRESS 1. CREATE A NODE FOR SINGLE LINKED LIST PRESS 2. DISPLAY THE NODES OF SINGLE LIST LIST PRESS 3. ADD A NODE AT BEGINNING OF SINGLE LINKED LIST PRESS 4. ADD A NODE AT THE END OF THE SINGLE LINKED LIST PRESS 5. ADD A NODE IN BETWEEN THE SINGLE LINK LIST PRESS 6. DELETE A NODE AT BEGINNING of SINGLE LINKED LIST PRESS 7. DELETE A NODE IN BETWEEN SINGLE LINKED LIST PRESS 8. DELETE A NODE AT THE END OF SINGLE LINKED LIST PRESS 9. TO REVERSE A SINGLE LINKED LIST PRESS 10. TO COUNT THE NUMBER OF NODES. PRESS 11. TO SERACH THE DATA FIELD 0F NODES. ********************************************* ENTER YOUR CHOICE : 1 CREATING NODE OF SINGLE LINKED LIST ENTER DATA TO THE NODE: 111 ENTER YOUR CHOICE : 1

CREATING NODE OF SINGLE LINKED LIST ENTER DATA TO THE NODE: 222 ENTER YOUR CHOICE : 1
PRACTICAL FILE 2ND SEM Page 71

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
CREATING NODE OF SINGLE LINKED LIST ENTER YOUR CHOICE : 1

ENTER DATA TO THE NODE: 333 ENTER YOUR CHOICE : 2

ELENENTS of Single linked list are: 111 222 333

Enter your choice: 3

ENTER DATA TO THE NODE: 555 NODE ADDED TO THE LIST Enter your choice: 2

ELEMENTS of Single linked list are: 555 111 222 333

Enter your choice: 3

ENTER THE POSITION WHERE YOU WANT TO ADD NODE 3 ENTER DATA TO THE NODE: 888
PRACTICAL FILE 2ND SEM Page 72

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
NODE ADDED SUCCESSFULLY Enter your choice:2

ELENENTS of Single linked list are: 555 111 888 222 333

Enter your choice: 6

DELETED NODE FROM LIST Enter your choice: 2

ELENENTS of Single linked list are: 111 888 222 333

Enter your choice: 9

The reverse of the linked list is : 333 222 888 111

Enter your choice: 10 THE VALUE OF THE COUNT IS 4.

PRACTICAL FILE 2ND SEM

Page 73

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter your choice: 11 Enter the data to de searched:333

Item is present/found

333 found in list 1 times

PRACTICAL FILE 2ND SEM

Page 74

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011. 2ND. MCA

PROGRAM:- A MENU DRIVEN PROGRAM FOR CIRCULAR LINKED LIST WHICH PERFORMS THE FOLLOWING OPERATIONS ON CIRCULAR LINKED LIST:1.CREATE 2.ADD NODES( FIRST, BETWEEN AND LAST POSITIONS) 3.DELETE NODES( FIRST, BETWEEN AND LAST POSITIONS) 4.COUNT NUMBER OF NODES 5.SEARCH DATA FIELD 6. DISPLAY. */

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *link;
PRACTICAL FILE 2ND SEM

//header file included

//structure declared

Page 75

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 }*head,*p,*q,*temp,*t; //global declaration of structure

int i=0,cnt=0;

//global declaration

void create(); void display(); void addfirst(); void addlast(); void addbetween(); void delfirst(); void delbetween(); void reverse(); count(); void dellast(); void search();

//udfs declared

void main() //MAIN STARTS { int choice; clrscr(); do {


PRACTICAL FILE 2ND SEM Page 76

//do while loop strats

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\n\t\t*********************************************"); printf("\n\n\t\tPRESS 1. CREATE A NODE FOR CIRCULAR LINKED LIST\n"); printf("\n\t\tPRESS 2. printf("\n\t\tPRESS 3. printf("\n\t\tPRESS 4. printf("\n\t\tPRESS 5. printf("\n\t\tPRESS 6. printf("\n\t\tPRESS 7. printf("\n\t\tPRESS 8. printf("\n\t\tPRESS 9. DISPLAY THE NODES OF CIRCULAR LIST LIST\n"); ADD A NODE AT START OF CIRCULAR LINKED LIST\n"); ADD A NODE IN END OF THE CIRCULAR LINKED LIST\n"); ADD A NODE IN BETWEEN THE CIRCULAR LINK LIST\n"); DELETE A NODE AT START OF CIRCULAR LINKED LIST\n"); DELETE A NODE IN BETWEEN CIRCULAR LINKED LIST\n"); DELETE A NODE AT LAST OF CIRCULAR LINKED LIST\n"); TO REVERSE A CIRCULAR LINKED LIST\n");

printf("\n\t\tPRESS 10. TO COUNT THE NUMBER OF NODES.\n"); printf("\n\t\tPRESS 11. TO SEARCH THE DATA FIELDS OF NODES."); printf("\n\n\t\tPRESS 0. XIT\n"); printf("\n\n\t\t*********************************************"); printf("\n\n\n\t\t\tEnter your choice:\n"); scanf("%d",&choice); switch(choice) { //switch strts here //getting choice

case 1: create(); break; case 2: display(); break;


PRACTICAL FILE 2ND SEM Page 77

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 case 3: addfirst(); break; case 4: addlast(); break; case 5: addbetween(); break; case 6: delfirst(); break; case 7: delbetween(); break; case 8: dellast(); break; case 9: reverse(); break; case 10:count(); break; case 11: search(); break; case 0: exit(0); default: printf("\n\tEnter correct choice:\n"); } //switch ends
PRACTICAL FILE 2ND SEM Page 78

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 }while(choice!=0);//do-while ends getch(); } //main ends here

void create() { clrscr();

//creare starts here

printf("\n\n\t\tCREATING NODE OF CIRCULAR LINKED LIST\n"); if(i==0) { p=malloc(sizeof(struct node)); //craete node

printf("\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&p->data); p->link=NULL; head=p; } // end of if statement else { //else strats //create node //assign null to p link

q=malloc(sizeof(struct node));

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); p->link=q;


PRACTICAL FILE 2ND SEM Page 79

//get data from user

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 q->link=head; p=q; } i++; //END OF ELSE //i incremented //null assigned to q link

printf("\nData inserted successfully in the node\n"); }

void display() { clrscr(); temp=head; if(temp==NULL) {

//display starts

//check for empty list

printf("\n\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!"); return; } //end of if printf("\nElement of Single linked list are:\n"); temp=head; do { printf("\t%d",temp->data); temp=temp->link;
PRACTICAL FILE 2ND SEM Page 80

//printing data of linked list

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } while(temp!=head); // end of do-while loop } //end of display function

void addfirst() { clrscr(); temp=head;

//addfirst strats

//assign head to temp

if(temp==NULL) { printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); while(temp->link!=head) { temp=temp->link; } q->link=head; head=q;
PRACTICAL FILE 2ND SEM Page 81

//getting dta from user

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 temp->link=head; printf("\n\n\t\tNODE ADDED TO THE LIST"); i++; getch(); }

void addlast() {

//add last start

clrscr(); temp=head; if(temp==NULL) { printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node //assign head to temp

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); q->link=head; //null assigned to q link while(temp->link!=head) //while starts {
PRACTICAL FILE 2ND SEM Page 82

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 temp=temp->link; } //jump to next node

//while ends

temp->link=q; p=temp; printf("NODE ADDED SUCCESSFULLY\n"); i++; getch(); }

void addbetween() { int c,pos,j; clrscr(); temp=head; if(temp==NULL) {

//addbetween defined

printf("LINKED LIST IS EMPTY ! \n"); return; } printf("\nENTER THE POSITION WHERE YOU WANT TO ADD NODE\n"); scanf("%d",&pos); c=count();
PRACTICAL FILE 2ND SEM Page 83

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 if(pos<1||pos>c) { printf("ERROR! ENTERED POSITION IS OUTSIDE LIMIT"); return; } q=malloc(sizeof(struct node)); //creating node //check for position

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); //getting data from user for(j=1;j<=pos-1;j++) //for loop strats { temp=temp->link; } q->link=temp->link; temp->link=q; p=temp; printf("\n\n\t\tNODE ADDED SUCCESSFULLY"); i++; getch(); } //q assigned to temp link //jump to next node

void delfirst() {

//del fisrt defination

PRACTICAL FILE 2ND SEM

Page 84

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); q=temp=head; if(temp==NULL) { printf("\n\n\t\tTHE LINK LIST IS EMPTY"); getch(); return; } head=q->link; //assign temp link to head while(temp->link!=q) { temp=temp->link; } temp->link=head; free(q); printf("DELETED NODE FROM LIST"); i--; getch(); }

void delbetween() {
PRACTICAL FILE 2ND SEM

//delbetween definition

Page 85

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 int c,j,pos; clrscr(); temp=head; if(temp==NULL) { printf("\n\n\t\tTHE LINK LIST IS EMPTY"); return; } printf("\n\n\tEnter the position where u want to delete node"); scanf("%d",&pos); //c=count(); if(pos<1||pos>c) //check for correct position { printf("\n\n\t\tERROR! Position entered is outside limit."); return; } for(j=1;j<pos-1;j++) { temp=temp->link; } q=temp->link; temp->link=temp->link->link; //putting link of 2nd node to first
PRACTICAL FILE 2ND SEM Page 86

//chech for empty list

//for loop starts

//jump to next node

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 free(q); //free up memory of t

printf("\n\n\t\t NODE DELETED FROM LIST"); i--; getch(); }

void reverse() { int j=1,k; clrscr(); t=head;

//reverse starts

//assign head to temp

if(t==NULL) //check for empty list { printf("\n\n\t\tLIST IS EMPTY"); return; } printf("\n\tThe reverse of the linked list is :\n"); while(j<=i) //while loop strats(i is meant for number of nodes. { t=head; for(k=1;k<=i-j;k++) {
PRACTICAL FILE 2ND SEM Page 87

//for loop strats

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 t=t->link; } printf("%d\t",t->data); //printing data in reverse order j++; } } //reverse ends //jump to next node

count() { int c=0; temp=head;

//count starts

while(temp->link!=head) //while starts { c++; temp=temp->link; //jump to next node } printf("the value of count is %d",c+1); return c+1; } //

void dellast() {

//defination of dellast start

PRACTICAL FILE 2ND SEM

Page 88

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 int j=1,c; c=count(); temp=head; if(temp==NULL) { printf("\n\n\t\tWarning! list is empty"); return; } if(c==1) { head=NULL; free(temp); printf("\n\n\t\tNode is deleted"); } else { while(temp->link->link!=head) { temp=temp->link; } p=temp; free(temp->link); //free the memory
PRACTICAL FILE 2ND SEM Page 89

//assign head to temp

//while starts

//jump to next node

//while ends

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 temp->link=head; printf("\n\n\t\tNode is deleted"); } i--; getch(); } //dellast ends

void search() //search definition start { int item,found=0; clrscr(); printf("\n\n\t\tEnter the data to de searched:"); scanf("%d",&item); temp=head; do { if(temp->data==item) { found++; } temp=temp->link; } while(temp!=head);
PRACTICAL FILE 2ND SEM

//while loop statrs

//checking the data field

//found incremented

//jump to next node //if ends


Page 90

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 if(found>0) { printf("\n\n\t\tItem is present/found"); printf("\n\n\t\t%d found in list %d times",item,found); } else { printf("Item not present"); getch(); } }

PRACTICAL FILE 2ND SEM

Page 91

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
********************************************* PRESS 1. CREATE A NODE FOR CIRCULAR LINKED LIST PRESS 2. DISPLAY THE NODES OF CIRCULAR LIST LIST PRESS 3. ADD A NODE AT BEGINNING OF CIRCULAR LINKED LIST PRESS 4. ADD A NODE AT THE END OF THE CIRCULAR LINKED LIST PRESS 5. ADD A NODE IN BETWEEN THE CIRCULAR LINK LIST PRESS 6. DELETE A NODE AT BEGINNING of CIRCULAR LINKED LIST PRESS 7. DELETE A NODE IN BETWEEN CIRCULAR LINKED LIST PRESS 8. DELETE A NODE AT THE END OF CIRCULAR LINKED LIST PRESS 9. TO REVERSE A CIRCULAR LINKED LIST PRESS 10. TO COUNT THE NUMBER OF NODES. ********************************************* ENTER YOUR CHOICE : 1 CREATING NODE OF CIRCULAR LINKED LIST ENTER DATA TO THE NODE: 11 ENTER YOUR CHOICE : 1 CREATING NODE OF CIRCULAR LINKED LIST ENTER DATA TO THE NODE: 22 ENTER YOUR CHOICE : 1 CREATING NODE OF CIRCULAR LINKED LIST ENTER DATA TO THE NODE: 33
PRACTICAL FILE 2ND SEM Page 92

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
ENTER YOUR CHOICE : 1 CREATING NODE OF CIRCULAR LINKED LIST ENTER DATA TO THE NODE: 44 Enter your choice: 2 Element of Single linked list are: 11 22 33 44

Enter your choice: 3 ENTER DATA TO THE NODE: 88 NODE ADDED TO THE LIST Enter your choice: 2 Element of Single linked list are: 88 11 22 33 44

Enter your choice: 5 ENTER THE POSITION WHERE YOU WANT TO ADD NODE : 4 ENTER DATA TO THE NODE: 66 NODE ADDED SUCCESSFULLY Enter your choice: 2 Element of Single linked list are: 88 11 22 66 33 44

Enter your choice: 4 ENTER DATA TO THE NODE: 99


PRACTICAL FILE 2ND SEM Page 93

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
NODE ADDED SUCCESSFULLY Enter your choice: 2 Element of Single linked list are: 88 11 22 66 33 44 99

Enter your choice: 6 DELETED NODE FROM LIST Enter your choice: 2 Element of Single linked list are: 11 22 66 33 44 99

Enter your choice: 7 Enter the position where u want to delete node3 NODE DELETED FROM LIST Enter your choice: 2 Element of Single linked list are: 11 22 33 44 99

Enter your choice: 8 NODE IS DELETED. Enter your choice: 2 Element of circular linked list are: 11 22 33 44

PRACTICAL FILE 2ND SEM

Page 94

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter your choice: 9 The reverse of the linked list is : 44 33 22 11

Enter your choice: 10 THE NUMBER OF NODES IS 4 Enter your choice: 11 Enter the data to de searched:44 Item is present/found 44 found in list 1 times

PRACTICAL FILE 2ND SEM

Page 95

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011. 2ND. MCA

PROGRAM:- A MENU DRIVEN PROGRAM FOR DOUBLE LINKED LIST WHICH PERFORMS THE FOLLOWING OPERATIONS ON DOUBLE LINKED LIST:1.CREATE 2.ADD NODES( FIRST, BETWEEN AND LAST POSITIONS) 3.DELETE NODES( FIRST, BETWEEN AND LAST POSITIONS) 4.COUNT NUMBER OF NODES 5.SEARCH DATA FIELD 6. DISPLAY. */

#include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data;

//header file included

//structure declared

struct node *link,*prev;


PRACTICAL FILE 2ND SEM Page 96

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 }*head,*p,*q,*temp,*t; int i=0,cnt=0; void create(); void display(); void addfirst(); void addlast(); void addbetween(); void delfirst(); void delbetween(); void reverse(); count(); void dellast(); void search(); void main() //MAIN STARTS { int choice; clrscr(); do { printf("\n\n\n\t\t*********************************************"); printf("\n\n\n\t\t\t\tM A I N M E N U"); printf("\n\n\t\tPRESS 1. CREATE A NODE FOR DOUBLE LINKED LIST\n");
PRACTICAL FILE 2ND SEM Page 97

//global declaration of structure

//udfs declared

//do while loop strats

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\t\tPRESS 2. DISPLAY THE NODES OF DOUBLE LIST LIST\n"); printf("\n\t\tPRESS 3. ADD A NODE AT BEGINNING OF DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 4. ADD A NODE AT THE END OF THE DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 5. ADD A NODE IN BETWEEN THE DOUBLE LINK LIST\n"); printf("\n\t\tPRESS 6. DELETE A NODE AT BEGINNING of DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 7. DELETE A NODE IN BETWEEN DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 8. DELETE A NODE AT THE END OF DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 9. TO REVERSE A DOUBLE LINKED LIST\n"); printf("\n\t\tPRESS 10. TO COUNT THE NUMBER OF NODES.\n"); printf("\n\t\tPRESS 11. TO SEARCH THE DATA FIELDS OF NODES."); printf("\n\n\t\tPRESS 0. EXIT\n"); printf("\n\n\t\t*********************************************"); printf("\n\n\n\t\t\tEnter your choice:\n"); scanf("%d",&choice); switch(choice) { //switch strts here //getting choice

case 1: create(); break; case 2: display(); break; case 3: addfirst(); break;


PRACTICAL FILE 2ND SEM Page 98

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 case 4: addlast(); break; case 5: addbetween(); break; case 6: delfirst(); break; case 7: delbetween(); break; case 8: dellast(); break; case 9: reverse(); break; case 10:count(); break; case 11: search(); break; case 0: exit(0); default: printf("\n\tEnter correct choice:\n"); } //switch ends }while(choice!=0);//do-while ends getch();
PRACTICAL FILE 2ND SEM Page 99

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } //main ends here

void create() { clrscr();

//creare starts here

printf("\n\n\t\tCREATING NODE OF DOUBLE LINKED LIST\n"); if(i==0) { p=malloc(sizeof(struct node)); //craete node

printf("\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&p->data); p->link=NULL; p->prev=NULL; head=p; } // end of if statement else { //else strats //create node //assign null to p link

q=malloc(sizeof(struct node));

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); p->link=q; q->link=NULL; //null assigned to q link
Page 100

//get data from user

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 q->prev=p; p=q; } i++; //END OF ELSE //i incremented

printf("\nData inserted successfully in the node\n"); }

void display() { clrscr(); temp=head; if(temp==NULL) {

//display starts

//check for empty list

printf("\n\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!\tLIST EMPYTY!"); return; } //end of if printf("\nElement of double linked list are:\n"); temp=head; do { printf("\t%d",temp->data); temp=temp->link;
PRACTICAL FILE 2ND SEM Page 101

//printing data of linked list

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } while(temp!=NULL);// end of do-while loop } //end of display function

void addfirst() { clrscr(); temp=head; if(temp==NULL) {

//addfirst strats

//assign head to temp

printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node

printf("\nENTER DATA TO THE NODE: "); scanf("%d",&q->data); q->link=temp; q->prev=NULL; head=q; printf("\n\n\t\tNODE ADDED TO THE LIST"); i++; getch(); }
PRACTICAL FILE 2ND SEM Page 102

//getting dta from user

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void addlast() { clrscr(); temp=head; if(temp==NULL) {

//add last start

//assign head to temp

printf("\n\n\t\tLINKED LIST IS EMPTY !\n"); return; } q=malloc(sizeof(struct node)); //create node

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); q->link=NULL; //null assigned to q link while(temp->link!=NULL) //while starts { temp=temp->link; } temp->link=q; q->prev=temp; p=temp; printf("NODE ADDED SUCCESSFULLY\n");
PRACTICAL FILE 2ND SEM Page 103

//jump to next node

//while ends

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 i++; getch(); }

void addbetween() { int c,pos,j; c=count(); clrscr(); temp=head; if(temp==NULL) {

//addbetween defined

printf("LINKED LIST IS EMPTY ! \n"); return; } printf("\nENTER THE POSITION WHERE YOU WANT TO ADD NODE\n"); scanf("%d",&pos); if(pos<1||pos>c) { printf("ERROR! ENTERED POSITION IS OUTSIDE LIMIT"); return; }
PRACTICAL FILE 2ND SEM Page 104

//check for position

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 q=malloc(sizeof(struct node)); //creating node

printf("\n\n\t\tENTER DATA TO THE NODE: "); scanf("%d",&q->data); //getting data from user for(j=1;j<pos-1;j++) //for loop strats { temp=temp->link; } q->link=temp->link; temp->link=q; q->link->prev=q; p=temp; printf("\n\n\t\tNODE ADDED SUCCESSFULLY"); i++; getch(); } //q assigned to temp link //jump to next node

void delfirst() { clrscr(); temp=head; if(temp==NULL) {

//del fisrt defination

PRACTICAL FILE 2ND SEM

Page 105

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\t\tTHE LINK LIST IS EMPTY"); getch(); return; } head=temp->link; //assign temp link to head temp->link->prev=NULL; free(temp); //temp freed

printf("DELETED NODE FROM LIST"); i--; getch(); }

void delbetween() { int c,j,pos; c=count(); clrscr(); temp=head; if(temp==NULL) {

//delbetween definition

//chech for empty list

printf("\n\n\t\tTHE LINK LIST IS EMPTY"); return;


PRACTICAL FILE 2ND SEM Page 106

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } printf("\n\n\tEnter the position where u want to delete node"); scanf("%d",&pos); if(pos<1||pos>c) //check for correct position { printf("\n\n\t\tERROR! Position entered is outside limit."); return; } for(j=1;j<pos-1;j++) { temp=temp->link; } q=temp->link; temp->link=temp->link->link; //putting link of 2nd node to first temp->link->prev=temp; free(q); //free up memory of t //jump to next node //for loop starts

printf("\n\n\t\t NODE DELETED FROM LIST"); i--; getch(); } void reverse() {


PRACTICAL FILE 2ND SEM Page 107

//reverse starts

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 clrscr(); t=head; //assign head to temp

if(t==NULL) //check for empty list { printf("\n\n\t\tLIST IS EMPTY"); return; } //cnt=count(); printf("\n\tThe reverse of the linked list is :\n"); while(t->link!=NULL) //while loop strats(i is meant for number of nodes. { t=t->link; } while(t!=NULL) //while loop strats { printf("%d\t",t->data); //printing data in reverse order t=t->prev; } } count() { int c=0;
PRACTICAL FILE 2ND SEM Page 108

//jump to next node

//reverse ends //count starts

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 temp=head; while(temp!=NULL) //while starts { c++; temp=temp->link; //jump to next node } printf("the value of count is %d",c); return c; } // //defination of dellast start

void dellast() { int c; c=count(); temp=head; if(temp==NULL) {

//assign head to temp

printf("\n\n\t\tWarning! list is empty"); return; } if(c==1) { head=temp->link;


PRACTICAL FILE 2ND SEM Page 109

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 free(temp); printf("\n\n\t\tNode is deleted"); } else { while(temp->link->link!=NULL) { temp=temp->link; } //jump to next node //while starts

//while ends //free the memory //null assigned to last node

free(temp->link); temp->link=NULL; p=temp;

printf("\n\n\t\tNode is deleted"); } i--; getch(); } void search() //search definition start { int item,found=0; clrscr(); printf("\n\n\t\tEnter the data to de searched:");
PRACTICAL FILE 2ND SEM Page 110

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&item); temp=head; while(temp!=NULL) { if(temp->data==item) { found++; temp=temp->link; } else { temp=temp->link; } //else ends //if ends //found incremented //jump to next node //checking the data field //while loop statrs

}// end of while loop if(found>0) { printf("\n\n\t\tItem is present/found"); printf("\n\n\t\t%d found in list %d times",item,found); }

else {
PRACTICAL FILE 2ND SEM Page 111

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("Item not present"); getch(); } } //end of search module

PRACTICAL FILE 2ND SEM

Page 112

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
********************************************* PRESS 1. CREATE A NODE FOR DOUBLE LINKED LIST PRESS 2. DISPLAY THE NODES OF DOUBLE LIST LIST PRESS 3. ADD A NODE AT BEGINNING OF DOUBLE LINKED LIST PRESS 4. ADD A NODE AT THE END OF THE DOUBLE LINKED LIST PRESS 5. ADD A NODE IN BETWEEN THE DOUBLE LINK LIST PRESS 6. DELETE A NODE AT BEGINNING of DOUBLE LINKED LIST PRESS 7. DELETE A NODE IN BETWEEN DOUBLE LINKED LIST PRESS 8. DELETE A NODE AT THE END OF DOUBLE LINKED LIST PRESS 9. TO REVERSE A DOUBLE LINKED LIST PRESS 10. TO COUNT THE NUMBER OF NODES. ********************************************* ENTER YOUR CHOICE : 1 CREATING NODE OF DOUBLE LINKED LIST ENTER DATA TO THE NODE: 111 ENTER YOUR CHOICE : 1 CREATING NODE OF DOUBLE LINKED LIST ENTER DATA TO THE NODE: 222 ENTER YOUR CHOICE : 1 CREATING NODE OF DOUBLE LINKED LIST ENTER DATA TO THE NODE: 333
PRACTICAL FILE 2ND SEM Page 113

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
ENTER YOUR CHOICE : 1 CREATING NODE OF DOUBLE LINKED LIST ENTER DATA TO THE NODE: 444 Enter your choice: 2 Element of double linked list are: 111 222 333 444

Enter your choice: 3 ENTER DATA TO THE NODE: 888 NODE ADDED TO THE LIST Enter your choice: 2 Element of double linked list are: 888 111 222 333 444

Enter your choice: 5 ENTER THE POSITION WHERE YOU WANT TO ADD NODE : 4 ENTER DATA TO THE NODE: 666 NODE ADDED SUCCESSFULLY Enter your choice: 2 Element of double linked list are: 888 111 222 666 333 444

Enter your choice: 4 ENTER DATA TO THE NODE: 999


PRACTICAL FILE 2ND SEM Page 114

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
NODE ADDED SUCCESSFULLY Enter your choice: 2 Element of double linked list are: 888 111 222 666 333 444 999

Enter your choice: 6 DELETED NODE FROM LIST Enter your choice: 2 Element of double linked list are: 111 222 666 333 444 999

Enter your choice: 7 Enter the position where u want to delete node : 3 NODE DELETED FROM LIST Enter your choice: 2 Element of double linked list are: 111 222 333 444 999

Enter your choice: 8 NODE IS DELETED. Enter your choice: 2 Element of double linked list are: 111 222 333 444

PRACTICAL FILE 2ND SEM

Page 115

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter your choice: 9 The reverse of the double linked list is : 444 333 222 111

Enter your choice: 10 THE NUMBER OF NODES IS 4 Enter your choice: 11 Enter the data to de searched:44 Item is present/found 333 found in list 1 times

PRACTICAL FILE 2ND SEM

Page 116

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011. 2ND. MCA

PROGRAM:- A MENU DRIVEN PROGRAM FOR LINKED LIST BASED STACK WHICH PERFORMS THE FOLLOWING OPERATIONS ON STACK:1.PUSH OPERATION 2.POP OPERATION 3.DISPLAY OPERATION 4.COUNT THE ELEMENTS */

#include<stdio.h> #include<conio.h> #include<alloc.h> //header file included

struct stack //global declaration of stack structure { int data; struct stack *next; } *p,*q,*top,*t; //structure variable declared
PRACTICAL FILE 2ND SEM Page 117

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

int i=0,cnt=0; //global declaration

void push_stack(); //udf declared void pop_stack(); void display_stack(); void count_stack();

void main() { int choice; top=NULL; clrscr(); do { clrscr();

//main starts

//do while loop starts

printf("\n\n\t\t\t M A I N

M E N U ");

printf("\n\t*******************************************************"); printf("\n\t\tPRESS 1 FOR PUSHING INTO STACK."); printf("\n\t\tPRESS 2 FOR DISPLAYING STACK."); printf("\n\t\tPRESS 3 FOR POPING OUT OF STACK."); printf("\n\t\tPRESS 4 FOR COUNTING ELEMENTS OF STACK.");
PRACTICAL FILE 2ND SEM Page 118

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\t\tPRESS 0 FOR EXIT."); printf("\n\t*******************************************************"); printf("\n\n\t\t\tENTER YOUR CHOICE : "); scanf("%d",&choice); switch(choice) { case 1: push_stack(); break; case 2: display_stack(); break; case 3: pop_stack(); break; case 4: count_stack(); break; case 0: exit(); //choice scanned

//switch case start

default: printf("\n\n\t\tERROR! INVALID CHOICE ENTERED "); } //switch case ends here

}while(choice!=0); //do-while loop ends here } //main ends here

PRACTICAL FILE 2ND SEM

Page 119

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void push_stack() { clrscr(); if(i==0) { top=malloc(sizeof(struct stack)); //node created printf("\n\n\tEnter data into the stack : "); scanf("%d",&top->data); top->next=NULL; } else { p=malloc(sizeof(struct stack)); printf("\n\n\tEnter data into the stack : "); scanf("%d",&p->data); //data scanned p->next=top; top=p; } i++; printf("\n\n\tELEMENT IS SUCESSFULLY ENTERED INTO STACK"); getch(); }
PRACTICAL FILE 2ND SEM Page 120

//defination of push stack

//i is meant for number of elements

//data entered //null assigned to top next

//top assigned to p next

//p is made top here

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void pop_stack() { clrscr();

//defination of pop stack

if(top==NULL) //check for underflow { printf("\n\n\tERROR! UNDERFLOW\tERROR! UNDERFLOW\tERROR! UNDERFLOW"); } p=top; //p is made top here top=top->next; //top is decremented

printf("\n\n\tELEMENT IS SUCESSFULLY POPPED FROM STACK"); free(p); i--; //i decremented

getch(); }

PRACTICAL FILE 2ND SEM

Page 121

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void count_stack() { clrscr();

//defination of count stack

if(top==NULL) //check for underflow { printf("\n\n\tERROR! UNDERFLOW\tERROR! UNDERFLOW\tERROR! UNDERFLOW"); return; getch(); } p=top; // top assigned to p

while(p!=NULL) //while loop starts { cnt++; //cnt incremented(cnt meant for number fo elements)

p=p->next; //p incremented } printf("\n\n\tTHE NUMBER OF ELEMENTS IN STACK ARE : %d",cnt); getch(); }

PRACTICAL FILE 2ND SEM

Page 122

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void display_stack() //defination of display stack { clrscr(); if(top==NULL) //check for underflow { printf("\n\n\tERROR! UNDERFLOW\tERROR! UNDERFLOW\tERROR! UNDERFLOW"); getch(); return; } q=top; //q is made top here

printf("THE STACK LOOK LIKE AS UNDER :"); while(q!=NULL) { printf("\n\t\t\t\t\t%d\t",q->data); //printing data of stack q=q->next; //incrementing the value of q } //while loop ends here //while loop starts

printf("\n\t\t\t\t"); getch(); } /* //display stack ends here NAME : RACHIT GUPTA.


Page 123

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 ROLL NO. SEMESTER DEPARTMENT : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR LINKED SINGLE QUEUE WHICH PERFORMS THE FOLLOWING OPERATIONS:1.ENQUEUE OPERATION 2.DEQUEUE OPERATION 3.COUNT ELEMENTS. 4.DISPLAY OPERATION 0.EXIT */

#include<conio.h> #include<stdio.h> #include<alloc.h> //header file included

struct queue { int data;

//structure declared

struct queue *next; }*p,*front,*rear,*temp; //structure variables declared

void enqueue();
PRACTICAL FILE 2ND SEM

//udfs declared
Page 124

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void dequeue(); void display(); void count(); int choice,i=0,cnt=0; //global declaration

void main() {

//main starts

front=rear=NULL; clrscr(); do {

//rear and front initiled at NULL

//do-while loop starts

printf("\n\n\t\t\t\t M A I N M E N U"); printf("\n\n\t"); printf("\n\n\t\tPRESS 1. TO INSERT ELEMENTS IN QUEUE."); printf("\n\n\t\tPRESS 2. TO DELETE ELEMENTS IN QUEUE."); printf("\n\n\t\tPRESS 3. TO COUNT THE ELEMENTS OF QUEUE."); printf("\n\n\t\tPRESS 4. TO SEE THE ELEMENTS IN QUEUE."); printf("\n\n\t\tPRESS 0. EXIT"); printf("\n\n\t"); printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&choice); //getting the choice of user

PRACTICAL FILE 2ND SEM

Page 125

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 switch(choice) { case 1: enqueue(); clrscr(); break; case 2: dequeue(); clrscr(); break; case 3: count(); clrscr(); break; case 4: display(); clrscr(); break; case 0: exit(); //display called //count called // dequeue called //enqueue called here //switch starts here

default: printf("\n\n\t\tEnter correct choice:\n"); } } while(choice!=0); getch(); }


PRACTICAL FILE 2ND SEM

//switch ends here //do-while loop ends here

//main ends here.


Page 126

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void enqueue() { clrscr(); if(i==0) { p=malloc(sizeof(struct queue)); //IST NODE CREATED //ENQUEUE STARTS

printf("\n\tEnter data at rear end of stack "); scanf("%d",&p->data); p->next=NULL; front=rear=p; } else { temp=malloc(sizeof(struct queue)); //NODE CREATED //DATA SCANNED

//NULL ASSIGNED TO P NEXT //FRONT AND REAR MADE SAME FOR IST NODE

printf("\n\tEnter data at rear end of stack "); scanf("%d",&temp->data); rear->next=temp; temp->next=NULL; rear=temp; p=temp; } i++; //I INCREMENTED
Page 127

//DATA SCANNED

//TEMP ASSIGNED TO REAR NEXT //NULL ASSIGNED TO TEMP NEXT

//REAR MADE TEMP

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\tData enqueued into queue sucessfully"); } //ENQUEUE ENDS

void dequeue() { clrscr(); if(front==NULL) {

//DEQUEUE STARTS

//CHECK FOR EMPTY QUEUE

printf("\n\n\tQUEUE EMPTY!\tQUEUE EMPTY!\tQUEUE EMPTY!"); getch(); return; } temp=front; front=front->next; //FRONT INCREMENTED free(temp); i--; // I DECREMENTED(I MEANT FOR NODES COUNT)

printf("\n\tData dequeued from queue sucessfully"); } //DEQUE ENDS HERE

void count() { clrscr();

//COUNT STARTS HERE

PRACTICAL FILE 2ND SEM

Page 128

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 if(front==NULL) { printf("\n\n\tQUEUE EMPTY!\tQUEUE EMPTY!\tQUEUE EMPTY!"); getch(); return; } temp=front; //TEMP INITILIZED AS FRONT //CHECK FOR EMPTY QUEUE

while(temp!=NULL) //WHILE LOOP STARTS { cnt++; //CNT INCREMENTED(CNT MEANT FOR NUMBER OF ELEMENTS)

temp=temp->next; //TEMP INCREMENTED } printf("\n\n\t\tThe number of nodes in queue is : %d",cnt); getch(); }

void display() { clrscr();

//DISPLAY STARTS HERE

if(front==NULL) //CHECK FOR EMPTY QUEUE { printf("\n\n\tQUEUE EMPTY!\tQUEUE EMPTY!\tQUEUE EMPTY!");


PRACTICAL FILE 2ND SEM Page 129

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 getch(); return; } printf("The queue will look like as :\n"); temp=front; if(temp==front) { printf("FRONT-> "); } while(temp!=NULL) //WHILE LOOP STARTS { printf("\t%d",temp->data); //PRINTING DATA OF QUEUE temp=temp->next; } if(temp==NULL) { printf("\t<-REAR"); } getch(); } //DISPLAY ENDS HERE //TEMP INITILIZED AS FRONT

PRACTICAL FILE 2ND SEM

Page 130

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR BINARY SEARCH TREE WHICH PERFORMS FOLLOWING OPERATIONS ON BINARY SEARCH TREE :

1. INSERTING A NODE. 2. PREORDER TRAVERSAL. 3. POSTORDER TRAVERSAL. 4. INORDER TRAVERSAL */

#include<stdio.h> #include<conio.h> struct tree { int num; struct tree *left; struct tree *right; }*btree; void preorder(struct tree*); void postorder(struct tree*);
PRACTICAL FILE 2ND SEM

//headerfile included

//global declaration of structure

//udfs declared

Page 131

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void inorder(struct tree*); struct tree* insert(struct tree*,int digit); struct tree *btree=NULL; void main() { int choice,digit; clrscr(); do { printf("\n\n\n*********** Main Menu *************\n"); printf("\t\t\nPress 1 for insertion\n"); printf("\t\t\nPress 2 for display in pre-order\n"); printf("\t\t\nPress 3 for display in post-order\n"); printf("\t\t\nPress 4 for display in in-order\n"); printf("\t\t\nPress 0 to exit\n"); printf("\t\t\n***********************************************\n"); printf("\t\t\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1:printf("\t\t\nEnter data for new node: "); scanf(" %d ",&digit);
PRACTICAL FILE 2ND SEM

//main starts

//scanning choice of user /switch case starts

//scan data for node


Page 132

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 while(digit!=0) { btree=insert(btree,digit); scanf("%d",&digit); } break; case 2:printf("\t\t\nPre-order traversal is: "); preorder(btree); break; case 3:printf("\t\t\nPost-order traversal is: "); postorder(btree); break; case 4:printf("\t\t\nIn-order traversal is: "); inorder(btree); break; case 0: exit 0; //call for inorder //call for post order //call for preorder //recursive call for insert

default:printf("\t\t\nWrong choice!"); } } while(choice!=0); getch();


PRACTICAL FILE 2ND SEM Page 133

//switch case ends //do while loop ends

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } //main ends

struct tree * insert(struct tree *btree,int digit)//definition of insert { if(btree==NULL) { btree=(struct tree *)malloc(sizeof(struct tree));//creating node btree->num=digit; btree->left=NULL; btree->right=NULL; } else if(digit<btree->num) { btree->left=insert(btree->left,digit); } else if(digit>btree->num) //check where to insert { btree->right=insert(btree->right,digit); } else if(btree->num==digit) { printf("\t\t\nDuplicacy of data is not allowed!"); exit(0);
PRACTICAL FILE 2ND SEM Page 134

//assign values to parts of nodes

//check where to insert

//recursive call to insert

//recursive call to insert

//check for duplicacy

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } return (btree); } void preorder(struct tree *btree) { if(btree) { printf(" %d ",btree->num); preorder(btree->left); preorder(btree->right); } } void postorder(struct tree *btree) { if(btree) { postorder(btree->left); postorder(btree->right); printf(" %d ",btree->num); } } void inorder(struct tree *btree)
PRACTICAL FILE 2ND SEM

//preorder defined

//assigning values to parts of node //recursive call for preorder

//if ends here

//defination of post order

//call for post order

//printing nodes

//definition of inorder
Page 135

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { if(btree) { inorder(btree->left); printf(" %d ",btree->num); inorder(btree->right); } } //if ends //inorder ends here //call for inorder //printing nodes // recursive call for inorder

PRACTICAL FILE 2ND SEM

Page 136

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:

..BINARY SEARCH TREE. ********************MAIN MENU*********************** Press 1: INSERT INFO IN A BST Press 2: PREORDER VIEW OF BST Press 3: INORDER VIEW OF BST Press 4: POSTORDER VIEW OF BST Press 5: DISPLAY BST Press 6:SEARCH A NODE INFO Press 7: COUNT NUMBER OF NODES IN BST Press 0: EXIT *********************************************************

Enter your Choice: 1 Enter info to the node, 0 for exit 50 40 60 0 Enter your Choice: 2 preorder of BST is:
PRACTICAL FILE 2ND SEM Page 137

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 50 40 60 Enter your Choice:3 inorder of BST is: 40 50 60 Enter your Choice: 2 postorder of BST is: 40 60 50 Enter your Choice:6 Enter INFO to be Searched40 Search Successfull!!!!!!!!! Enter your Choice7 Total number of nodes in BST are 3 Enter your Choice:0

PRACTICAL FILE 2ND SEM

Page 138

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR LINEAR SEARCH ON AN ARRAY. */

#include<conio.h> #include<stdio.h> void main() { int ar[100],i,j,limit,item; j=0; clrscr();

//header files included

//main starts

printf("Enter the size of the array : "); scanf("%d",&limit); //scan limit of array

printf("Enter the elements of array :"); for(i=0;i<limit;i++) { scanf("%d",&ar[i]); } //scanning elements of array //for loop starts

printf("Enter the element you want to search in the array :"); scanf("%d",&item);
PRACTICAL FILE 2ND SEM

//insert item to de searched


Page 139

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 for(i=0;i<limit;i++) { if(ar[i]==item) j++; } if(j>0) //check for search successful or not //check for item //j incremented //for loop starts

printf("The element %d found %d times in the array",item,j); else printf("The element %d not found in the array",item); getche(); }

PRACTICAL FILE 2ND SEM

Page 140

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:
ENTER THE LENGTH OF ARRAY::10 ENTER THE ELEMENTS IN THE ARRAY : 1 22 3 4 5 22 78 229 22 55

ELEMENT OF THE ARRAY ARE 1 22 3 4 5 22 78 229 22 55

ENTER THE ELEMENT YOU WANT TO SEARCH::22

ELEMENT 22 FOUND IN ARRAY 3 TIMES. ENTER THE ELEMENT YOU WANT TO SEARCH::101 ELEMENT 101 NOT FOUND IN THE ARRAY.

PRACTICAL FILE 2ND SEM

Page 141

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR BINARY SEARCH ON AN ARRAY. */

#include<stdio.h> //HEADER FILES INCLUDED #include<conio.h> void main() { int len,found=0; int data[100]; int lb=0,item,loc; int beg,end,mid,i; clrscr(); printf("Enter no. of elements\n\n"); scanf("%d",&len); //SIZE OF ARRAY SCANNED //variables declared //MAIN STARTS

printf("Enter the elements of array in sorted order\n"); for(i=0;i<len;i++) { scanf("%d",&data[i]); }


PRACTICAL FILE 2ND SEM Page 142

//ELEMENTS ENTERED

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\nEntered array is :\n"); for(i=0;i<len;i++) { printf("\t%d",data[i]); } //ELEMENTS PRINTED

printf("\n\n\tEnter the item to be searched"); scanf("%d",&item); //SCAN THE ITEM TO BE SEARCHED

beg=lb; end=len-1; mid=(beg+end)/2; //FIND THE MID LOCATION

while((beg<=end)&&found!=1) { if(data[mid]==item) { found++; } else if(item<data[mid]) //COMPARE ITEM WITH MID { end=mid-1; }
PRACTICAL FILE 2ND SEM Page 143

//CHECK FOR ITEM FOUND

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 else if(item>data[mid]) //COMPARE ITEM WITH MID { beg=mid+1; } mid=(beg+end)/2; } if(found>0) //CHECK WHETEHER ITEM FOUND OR NOT

printf("\n\nThe desired item i.e %d is found at location %d",item,mid+1); else printf("\n\nThe item does not exist"); getch(); }

PRACTICAL FILE 2ND SEM

Page 144

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:

Enter the no.of elements in array 6

Enter the elements 23 43 12 44 22 21

Sorted array IS : 12

21

22

23

43

44

Enter the element to be searched : 12

12 FOUND IN ARRAY 1 TIMES. Enter the element to be searched : 33

33 NOT FOUND IN THE ARRAY ]

PRACTICAL FILE 2ND SEM

Page 145

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR BUBBLE SOTING ON AN ARRAY */ #include<stdio.h> #include<conio.h> #define max 100

void display(int[],int); void bubblesort(int[],int);

int i,j,size;

void main() { int ar[max]; clrscr(); printf("\n\nEnter the number of elements you want in array : "); scanf("%d",&size); printf("\n\nEnter the elements in array\n"); for(i=0;i<size;i++)
PRACTICAL FILE 2ND SEM Page 146

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { scanf("%d",&ar[i]); } printf("The original array is :\n"); display(ar,size); bubblesort(ar,size); printf("The array after applying bubble sort is :\n"); display(ar,size); getch(); }

void display(int ar[],int size) { for(i=0;i<size;i++) { printf("%d\t",ar[i]); } } void bubblesort(int ar[],int size) { int temp; for(i=0;i<size;i++)
PRACTICAL FILE 2ND SEM Page 147

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { for(j=0;j<size-1;j++) { if(ar[j]>ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp; } } } }

PRACTICAL FILE 2ND SEM

Page 148

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:
Before sorting the array using Bubble sort

first create the array:

how many numbers u want to enter:4

Enter numbers: 23 12 11 9

before sort the array using Bubble sort, the array is:23 12 11 9

Apply Bubble Sort on the array:

After sorting the array using Bubble sort the array is:

9 11 12 23

PRACTICAL FILE 2ND SEM

Page 149

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR INSERTION SOTING ON AN ARRAY */

#include<stdio.h> #include<conio.h> #define max 100

//header files included

void display(int[],int); void insertionsort(int[],int);

//udfs declared

int i,j,size;

//global declaration

void main() { int ar[max]; clrscr();

//main starts

printf("\n\nEnter the number of elements you want in array : "); scanf("%d",&size); //scanning size of array

printf("\n\nEnter the elements in array\n");


PRACTICAL FILE 2ND SEM Page 150

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 for(i=0;i<size;i++) { scanf("%d",&ar[i]); } printf("The original array is :\n"); display(ar,size); insertionsort(ar,size); //call to display //call to insertion sort //scanning elements of array

printf("The array after applying insertion sort is :\n"); display(ar,size); getch(); } //call to display

void display(int ar[],int size) { for(i=0;i<size;i++) { printf("%d\t",ar[i]); } }

//display defined

//for loop starts

//printing elements of array

void insertionsort(int ar[],int size) {


PRACTICAL FILE 2ND SEM

//insertion sort defined

Page 151

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 int temp; for(i=1;i<size;i++) { temp=ar[i]; for(j=i-1;j>=0;j--) { if(temp<ar[j]) ar[j+1]=ar[j]; else break; } ar[j+1]=temp; } } /inner loop ends //set temp as inserted element //outer loop ends //end of insertion sort //compare elements //put elements //set temp as ist element //for loop starts //for loop starts

PRACTICAL FILE 2ND SEM

Page 152

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:

Before sorting the array using Insertion sort first create the array:

How many elenents you want to b insert: 5

Enter the elements: 44 33 22 6 99

before sort the array using insertion sort, the array is:44 33 22 6 99

Apply Insertion Sort on the array:

After sorting the array using Insertion sort, the array is:6 22 33 44 99

PRACTICAL FILE 2ND SEM

Page 153

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR SELECTION SOTING ON AN ARRAY */ #include<stdio.h> #include<conio.h> #define max 100 //header files included

void display(int[],int); void selectionsort(int[],int);

//udfs declared

int i,j,size;

//global declaration of variables

void main() { int ar[max]; clrscr();

//main strats

printf("\n\nEnter the number of elements you want in array : "); scanf("%d",&size); //scanning the size of array

printf("\n\nEnter the elements in array\n"); for(i=0;i<size;i++)


PRACTICAL FILE 2ND SEM Page 154

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { scanf("%d",&ar[i]); } printf("The original array is :\n"); display(ar,size); selectionsort(ar,size); //call to selection sort //scanning the elements of array

printf("The array after applying selection sort is :\n"); display(ar,size); getch(); } //main ends here //call to display

void display(int ar[],int size) { for(i=0;i<size;i++) { printf("%d\t",ar[i]); } } void selectionsort(int ar[],int size) { int temp,small; for(i=0;i<size-1;i++)
PRACTICAL FILE 2ND SEM

//display defined

//printing elements of array

//definition of selection sort

//for loop starts


Page 155

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { small=i; //put small=i //for loop starts

for(j=i+1;j<size;j++) { if(ar[j]<ar[small]) small=j; } temp=ar[i]; ar[i]=ar[small]; ar[small]=temp; } } //outer loop ends //set small=j

//inner loop ends //swap the elements

//end of selection sort

PRACTICAL FILE 2ND SEM

Page 156

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:

Enter the number of elements you want in array : 5

Enter the elements in array 45 69 21 0 58 The original array is : 45 69 21 0 58

The array after applying selection sort is : 0 21 45 58 69

PRACTICAL FILE 2ND SEM

Page 157

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR QUICK SOTING ON AN ARRAY */ #include<stdio.h> #include<conio.h> void quick_sort(int [],int,int); void display(int [],int); int ar[100],i,j,k,temp,size; void main() { clrscr(); printf("Enter the size of array : "); scanf("%d",&size); //scanning the size of array //declaration of global variables //udfs declared //header file inclusion

printf("Enter the elements of array "); for(i=0;i<size;i++) { scanf("%d",&ar[i]); } printf("\n\nBefore sorting the array is :\n"); display(ar,size);
PRACTICAL FILE 2ND SEM Page 158

//scanning the elements of array

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\nAfter sorting the array is :\n"); quick_sort(ar,0,size-1); display(ar,size); getch(); } //main ends //definition of quick sort //call to quick sort //call to display

void quick_sort(int ar[],int first,int last) { int low,high,c; low=first; high=last; //put first =low //put last=high

c=ar[(first+last)/2]; do { while(ar[low]<c) low++; while(ar[high]>c) high--; if(low<=high) { temp=ar[low]; ar[low]=ar[high]; ar[high]=temp;


PRACTICAL FILE 2ND SEM

//find the comparand

//while starts

//while strats //high decremented //check condition

//swap ar[low] and ar[high]

Page 159

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 low++; high--; } } while(low<=high); if(low<last) quick_sort(ar,low,last); if(first<high) quick_sort(ar,first,high); } void display(int ar[],int size) { for(i=0;i<size;i++) { printf("\t%d",ar[i]); } } //printing the elements //definition of display starts //recursive call for quick sort //check condition //recursive call for quick sort //low incremented //high decremented

PRACTICAL FILE 2ND SEM

Page 160

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:

BEFORE SORTING THE ARRAY FIRST CREATE THE ARRAY

Enter size of the array : 6 Enter elements of the array 21 1 3 44 5 99 BEFORE SORTING THE ARRAY

THE ELEMENT OF ARRAY ARE::21 1 3 44 5 99

AFTER APPLYING QUICK SORT

THE ELEMENT OF ARRAY ARE::1 3 5 21 44 99

PRACTICAL FILE 2ND SEM

Page 161

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : : : : RACHIT GUPTA. 66-MCA-2011 2ND. M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR MERGE SORTING ON AN ARRAY */

#include<stdio.h> #include<conio.h> //header files included

void merge_sort(int [],int,int [],int,int []); //all udfs declared void display(int [],int); void bubblesort(int [],int); int ar1[100],ar2[100],ar3[100],i,j,k,temp,size1,size2,size3;

void main() { clrscr();

//main starts

printf("Enter the size of array 1 : "); printf(Enter the elements of array 1 (sorted) :); scanf("%d",&size1); for(i=0;i<size1;i++) {
PRACTICAL FILE 2ND SEM Page 162

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&ar1[i]); } printf("\nEnter the size of array 2 : "); printf(Enter the elements of array 2 (sorted) :); scanf("%d",&size2); for(i=0;i<size2;i++) { scanf("%d",&ar2[i]); } printf("\nFirst array is :"); for(i=0;i<size1;i++) { printf("\t%d",ar1[i]); } printf("\nSecond array is :"); for(i=0;i<size2;i++) { printf("\t%d",ar2[i]); } printf("\n\nAfter sorting the array is :\n"); merge_sort(ar1,size1,ar2,size2,ar3); //call for merge sort for(i=0;i<size3;i++)
PRACTICAL FILE 2ND SEM Page 163

//scanning the ist array

//scanning the second array

//printing the ist array

//printing the second array

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { printf("\t%d",ar3[i]); } getch(); } //print the merged & sorted array

void merge_sort(int ar1[],int size1,int ar2[],int size2,int ar3[]) { for(i=j=k=0;i<size1&&j<size2;) //for loop starts { if(ar1[i]<ar2[j]) { ar3[k]=ar1[i]; //put the element of ist array into third k++;i++;size3++; } //end if //compare elements of ist and 2nd array

else if(ar1[i]>ar2[j]) //compare elements of ist and 2nd array { ar3[k]=ar2[j]; //put the element of 2nd array into third

k++,j++;size3++; } else {
PRACTICAL FILE 2ND SEM Page 164

//end else-if

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 ar3[k]=ar1[i]; // //put the element of ist array into third

i++;k++;k++;size3++; } } for(;i<size1;) { ar3[k]=ar1[i]; k++;i++;size3++; } for(;j<size2;) { ar3[k]=ar2[j]; k++,j++,size3++; } } } //end of outer for loop //end MERGesort. //put the element of 2nd array into third //end of inner loop //put the element of ist array into third

PRACTICAL FILE 2ND SEM

Page 165

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT:
Enter the size of array 1 : 4 Enter the elements of the array (sorted): 11 22 34 2

Enter the size of array 2 : 5 Enter the elements of the array (sorted): 33 2 45 21 45

First array is : Second array is :

11 33

22 2

34 45

2 21 45

After applying the merge sorting the array is : 2 2 11 21 22 33 34 45 45

PRACTICAL FILE 2ND SEM

Page 166

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM FOR ARRAY PRIORITY QUEUE WHICH PERFORMS THE FOLLOWING OPERATIONS ON QUEUE:1.ENQUEUE OPERATION 2.DEQUEUE OPERATION 3.DISPLAY OPERATION*/

#include<stdio.h> #include<conio.h> #include<alloc.h> void enqueue(); void dequeue(); void display(); struct prque { int data; int priority; struct prque *next;

//headerfiles included

//udf prototyping.

//global declaration of structure

}*p,*q,*temp,*front=NULL;
PRACTICAL FILE 2ND SEM Page 167

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

void main() { int choice; clrscr(); do {

//main starts here

//infinite do-while loop starts.

printf("\n\n\t\t\t\t M A I N M E N U"); printf("\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"); printf("\n\tPRESS 1. TO INSERT ELEMENTS IN PRIORITY QUEUE OPERATION)."); printf("\n\tPRESS 2. TO DELETE ELEMENTS IN PRIORITY QUEUE OPERATION)."); printf("\n\tPRESS 3. TO SEE THE PRORITY QUEUE"); printf("\n\tPRESS 0. EXIT"); printf("\n\t*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*"); printf("\n\n\t\t\tEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: enqueue(); break; case 2: dequeue(); break;
PRACTICAL FILE 2ND SEM Page 168

//getting the choice of user //switch starts here

//calling various functions

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 case 3: display(); break; case 0: exit(); default : printf("\n\n\tPlease enter correct choice"); return; } //end of switch. //while loop ends here.

} while(choice!=0); }

//main ends here.

void enqueue() { int n_priority,n_data;

//enqueue starts here

temp=malloc(sizeof(struct prque)); do {

//node created

//loop starts

printf("Enter priority of the node"); scanf("%d",&n_priority); }while(n_priority<1); printf("Enter data of the node"); scanf("%d",&n_data); temp->data=n_data; temp->priority=n_priority;
PRACTICAL FILE 2ND SEM Page 169

//scanning the priority //end of loop

//scanning the data

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 if((front==NULL)||(n_priority<front->priority)) { temp->next=front; front=temp; } else { q=front; while((q->next!=NULL)&&(q->next->priority<=n_priority)) { q=q->next; } temp->next=q->next; q->next=temp; } printf("\n\n\tNode inserted sucessfully"); }

void display() { if(front==NULL) {


PRACTICAL FILE 2ND SEM Page 170

//CHECK FOR EMPTY QUEUE

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\n\n\tQUEUE EMPTY!\tQUEUE EMPTY!\tQUEUE EMPTY!"); getch(); return; } temp=front; printf("The priority queue is as follow :\n"); while(temp!=NULL) { printf("\t%d",temp->data); temp=temp->next; } }

void dequeue() { clrscr(); if(front==NULL) { printf("\n\n\tQUEUE EMPTY!\tQUEUE EMPTY!\tQUEUE EMPTY!"); getch(); return; }
PRACTICAL FILE 2ND SEM Page 171

//CHECK FOR EMPTY QUEUE

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 p=front; front=front->next; //FRONT INCREMENTED free(p); printf("\n\tNode deleted from queue sucessfully"); } //DEQUE ENDS HERE

PRACTICAL FILE 2ND SEM

Page 172

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
MAIN MENU *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_* PRESS 1. TO INSERT ELEMENTS IN PRIORITY QUEUE OPERATION. PRESS 2. TO DELETE ELEMENTS IN PRIORITY QUEUE OPERATION. PRESS 3. TO SEE THE PRORITY QUEUE PRESS 0. EXIT *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_* Enter your choice: 1 Enter priority of the node : 2 Enter data of the node : 222 Node inserted sucessfully

Enter your choice: 1 Enter priority of the node : 1 Enter data of the node : 111 Node inserted sucessfully

Enter your choice:1 Enter priority of the node : 4 Enter data of the node : 444 Node inserted sucessfully
PRACTICAL FILE 2ND SEM Page 173

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
Enter your choice:3 The priority queue is as follow : 111 222 444

Enter your choice : 2 Node deleted from queue sucessfully

Enter your choice : 3 The priority queue is as follow : 222 444

Enter your choice:1 Enter priority of the node : -112 Enter priority of the node : 0 Enter priority of the node : 99 Enter data of the node : 999 Node inserted sucessfully

Enter your choice:3 The priority queue is as follow : 222 444 999
Page 174

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU). */

Program -> TO sort an array using heap sort #include<stdio.h> #include<conio.h> void maxheap(int [],int); void maxheap_prop(int [],int,int); void main() { int heapsize,temp; int ar[100],b[100],i,j,size; clrscr(); printf("Enter the size of array : "); scanf("%d",&size); printf("Enter the elements of array "); for(i=1;i<=size;i++) { scanf("%d",&ar[i]); } printf("\n\nBefore sorting the array is :\n");
PRACTICAL FILE 2ND SEM

Page 175

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 for(i=1;i<=size;i++) { printf("\t%d",ar[i]); } maxheap(ar,size); heapsize=size; for(i=size;i>=1;i--) { temp=ar[i]; ar[i]=ar[1]; ar[1]=temp; b[i]=ar[i]; heapsize--; maxheap_prop(ar,1,heapsize); } printf("\n\nSorted array using heap sort :\n"); for(i=1;i<=size;i++) printf("\t%d",b[i]); getch(); } void maxheap(int ar[],int size) {
PRACTICAL FILE 2ND SEM Page 176

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 int temp,i; for(i=(size/2);i>=1;i--) maxheap_prop(ar,i,size); for(i=1;i<=size;i++) printf("\n ar[%d] contains %d element ",i,ar[i]); getch(); } void maxheap_prop(int ar[],int i,int heapsize) { int left,right,temp,largest; left=2*i; right=2*i+1; if((left<=heapsize)&&(ar[left]>ar[i])) largest =left; else largest=i; if((right<=heapsize)&&(ar[right]>ar[largest])) largest=right; if(largest!=i) { temp=ar[largest]; ar[largest]=ar[i]; ar[i]=temp; maxheap_prop(ar,largest,heapsize);
PRACTICAL FILE 2ND SEM Page 177

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } }

OUTPUT
Enter the size of array : 9 Enter the elements of array 5 3 17 10 84 19 6 22 9

Before sorting the array is : 5 3 17 10 84 19 6 22 9

ar[1] contains 84 element

PRACTICAL FILE 2ND SEM

Page 178

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 ar[2] contains 22 element ar[3] contains 19 element ar[4] contains 10 element ar[5] contains 3 element ar[6] contains 17 element ar[7] contains 6 element ar[8] contains 5 element ar[9] contains 9 element

AFTER SORTING ARRAY USING HEAP SORT : 3 5 6 9 10 17 19 22 84

PRACTICAL FILE 2ND SEM

Page 179

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 /* NAME ROLL NO. SEMESTER DEPARTMENT : RACHIT GUPTA. : 66-MCA-2011 : 2ND. : M C A (BC JU).

PROGRAM:- A MENU DRIVEN PROGRAM OF AN ARRAY WHICH PERFORMS THE FOLLOWING OPERATIONS ON TWO DIMENSIONAL ARRAY:1. CREATING AN ARRAY 2.VIEWING AN ARRAY 3.DELETING AN ELEMENT IN THE ARRAY 4.REVERSING THE ARRAY 5.INSERTING AN ELEMENT IN THE ARRAY 6.RANGE OF AN ARRAY 7.COUNT NO. OF ELEMENYTS IN THE ARRAY */

#define row 20 #define col 20

//macro defined

#include<stdio.h> //header files included #include<conio.h> void create(); void view(); void del();
PRACTICAL FILE 2ND SEM Page 180

//udfs included

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void transpose(); void count(); void insert(); void search();

int i,j,r,c,cnt=0; int m;

//global declaration of variables

int a[row][col],choice; void main() //main starts

{ clrscr(); printf("\n\n\t.CREATE AN INTEGER ARRAY:\n"); create(); clrscr(); do { printf("\n\n\n\t\t..........MAIN MENU..........\n"); printf("\t1.VIEW ELEMENTS OF THE ARRAY:\n"); printf("\t2.DELETE ELENMENT FROM ARRAY:\n"); printf("\t3.TRANSPOSE AN ARRAY:\n"); printf("\t4.INSERT ELEMENTS INTO ARRAY:\n"); printf("\t5.COUNT NO. OF ELEMENYTS\n");
PRACTICAL FILE 2ND SEM Page 181

//create called

//do-while loop starts

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 printf("\t6.SEARCH AN ELEMENT\n"); printf("\t7.EXIT\n"); printf("\t\tENTER YOYUR CHOICE........: "); scanf("%d",&choice); switch(choice) { case 1: //switch starts view(); break;

case 2: del(); break; case 3: transpose(); break; case 4: case 5: case 6: case 7: default: printf("Enter correct choice:\n"); } } //switch ends //do while loop ends insert(); break; count(); break; search(); break; exit();

while(choice!=7); getch(); } //main ends

void create()
PRACTICAL FILE 2ND SEM

//CREATING AN ARRAY FUNCTION


Page 182

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { printf("\nEnter the no. of rows of the array: "); scanf("%d",&r); if(r>row||r<1) { printf("\n number of rows out of bound"); } printf("\nEnter the no. of columns of the array: "); scanf("%d",&c); if(c>col||c<1) { printf("\n number of columns out of bound"); create(); } else { m=r*c; printf("Enter the elements:\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) {
PRACTICAL FILE 2ND SEM Page 183

//getting no of rows //check for limit

//getting no of columns //check limit

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&a[i][j]); } } }} void view() { if(r==0&&c==0) { printf("\nARRAY IS EMPTY:"); } else { printf("\n\nElements of the array are:\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } } } //printing elements of array //check for arra empty or not //VIEWING AN ARRAY FUNCTION //inner loop ends //outer loop ends //scanning elements of array

void transpose()
PRACTICAL FILE 2ND SEM

//REVERSING AN ARRAY FUNCTION


Page 184

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { if(r==0&&c==0) { printf("\nARRAY IS EMPTY:"); } else { printf("\n\nElements of the array are:\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } printf("\ntranspose of the two dimensional array is:\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[j][i]); printf("\n"); } }}
PRACTICAL FILE 2ND SEM Page 185

//printing transpose of array

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 void count() { if(r==0&&c==0) { printf("\nARRAY IS EMPTY:"); } for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); cnt++;//cnt incremented } printf("\nTotal no of elements in the array is =%d",cnt); } void search() { int se,flag=0,cnt=0; printf("\nEnter number to b searched:"); scanf("%d",&se); //scan the item to be searched for(i=0;i<r;i++) { for(j=0;j<c;j++)
PRACTICAL FILE 2ND SEM Page 186

//COUNT ELEMENTS OF AN ARRAY FUNCTION

//search starts

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 if(a[i][j]==se) { flag=1; //if matched set flag to 1 //compare for item

cnt++;//cnt incremented } } if(flag==1) { printf("Search successfull:\n"); printf("%d found in the array in %d times\n",se,cnt); } else { printf("Search unsuccessfull\n"); } } void del() { int rn,cn,j,temp,val=0; if(r==0&&c==0) { printf("\nARRAY IS EMPTY!!!!!!!");
PRACTICAL FILE 2ND SEM Page 187

//DELETING AN ELEMENT FUNCTION

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 return; } for(i=0;i<r;i++) { for(j=0;j<c;j++) { if(a[i][j]==0) printf("\nARRAY is empty!!!!!!\n NO further Deletion!!!\n"); } } printf("\n\nElements of the array are:\n"); for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } do { printf("\n:Enter the row no in which you want to delete the no. "); scanf("%d",&rn); printf("\n:Enter the column no in which you want to delete the no. ");
PRACTICAL FILE 2ND SEM Page 188

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 scanf("%d",&cn); if(rn>r||rn<1&&cn>c||cn<1) //check for bounds

printf("The position is outside the bounds."); }while((rn>r||rn<1)&&(cn>c||cn<1)); for(i=0;i<rn;i++) { for(j=0;j<cn;j++) { if((i==rn-1)&&(j==cn-1)) a[i][j]=x; } } } void insert() { int rn,cn,j,k,temp,val=0; if(r==0&&c==0) { printf("\nARRAY IS EMPTY!!!!!!!"); return; } printf("\n\nElements of the array are:\n"); for(i=0;i<r;i++)
PRACTICAL FILE 2ND SEM Page 189

//for loop starts

//for loop starts

//set x in place of deleted item

// end of del function //INSERTIN AN ELEMENT INTO AN ARRAY FUNCTION

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 { for(j=0;j<c;j++) printf("%d\t",a[i][j]); printf("\n"); } do { printf("\n:Enter the row no in which you want to overwrite the no. "); scanf("%d",&rn); printf("\n:Enter the column no in which you want to overwrite the no. "); scanf("%d",&cn); if(rn>r+1||rn<1&&cn>c+1||cn<1) printf("The position is outside the bounds."); }while((rn>r+1||rn<1)&&(cn>c+1||cn<1)); printf("\nEnter the number you want to insert:"); scanf("%d",&k); for(i=0;i<=rn;i++) { for(j=0;j<=cn;j++) { if((i==rn-1)&&(j==cn-1)) a[i][j]=k; //overwrite the element with inserted item
Page 190

//scan item to de inserted

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 } } } //inner for loop ends //outer for loop ends //insert ends

PRACTICAL FILE 2ND SEM

Page 191

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011

OUTPUT
1.VIEW ELEMENTS OF THE ARRAY: 2.DELETE ELENMENT FROM ARRAY: 3.TRANSPOSE AN ARRAY: 4.INSERT ELEMENTS INTO ARRAY: 5.COUNT NO. OF ELEMENYTS 6.SEARCH AN ELEMENT 7.EXIT ENTER YOUR CHOICE. : 1 Elements of the array are: 1 7 6 5 5 4 9 3 2 ENTER YOUR CHOICE.: 2 Enter the row no in which you want to delete the no. 3 Enter the column no in which you want to delete the no. 3 Element deleted. ENTER YOUR CHOICE. : 1 Elements of the array are: 1 7 6 5 5 4 9 3 0
Page 192

PRACTICAL FILE 2ND SEM

RACHIT GUPTA MCA 2ND SEM ROLL NO. 66 MCA 2011 ENTER YOUR CHOICE. : 3 transpose of the two dimensional array is: 1 5 9 7 5 3 6 4 0 ENTER YOYUR CHOICE.: 4 Enter the row no in which you want to overwrite the no. 3 Enter the column no in which you want to overwrite the no. 3 Enter the number you want to insert:99 ENTER YOUR CHOICE.: 1 Elements of the array are: 1 7 6 5 5 4 9 3 99 ENTER YOUR CHOICE.: 5 1 7 6 5 5 4 9 3 99

Total no of elements in the array is = 9. ENTER YOUR CHOICE.: 6 Enter number to b searched:99 99 found in the array in 1 times
PRACTICAL FILE 2ND SEM Page 193

Das könnte Ihnen auch gefallen