EC2209 Manual

Preview:

DESCRIPTION

EC2209 Manual

Citation preview

Ex No: 1(A)Date:

Class & Objects

Aim:

To write a C++ program to implement the basic concepts of Class and Object’s.

Algorithm:

1) Start the program.

2) Define a class stud with the needed data members.

3) Define the function input () o read the value such as rno, name, and 3marks.

4) Define the function print () to calculate the total marks, and average.

5) Display the calculated values with the rno and name.

6) In main (), create an object for the class stud.

7) Call the function input () with the object.

8) Call the function print () with the object.

9) Stop the program.

// PROGRAM FOR CLASS AND OBJECTS

#include<iostream.h>

#include<conio.h>

class stud

{

private:

int m1,m2,m3,rno,tot;

char name[20];

float avg;

public:

void input()

{

cout<<"\n\n\t\t\t CLASS AND OBJECTS \n ";

cout<<"\n ENTER YOUR NAME : ";

cin>>name;

cout<<"\n ENTER A REGISTER NUMBER : ";

cin>>rno;

cout<<"\n ENTER THREE SUBJECT MARKS : ";

cin>>m1>>m2>>m3;

}

void print()

{

cout<<"\n NAME : "<<name<<endl;

cout<<"\n REGISTER NUMBER : "<<rno<<endl;

cout<<"\n THREE SUBJECT MARKS : "<<m1<<"\t"<<m2<<"\t"<<m3<<endl;

cout<<"\n RESULT = ";

if((m1>=50)&&(m2>=50)&&(m3>=50))

{

cout<<" PASS "<<endl;

tot=m1+m2+m3;

avg=tot/3;

cout<<"\n TOTAL = "<<tot<<endl;

cout<<"\n AVERAGE = "<<avg<<endl;

}

else

{

cout<<" FAIL "<<endl;

}

}

};

void main()

{

clrscr();

stud s;

s.input();

s.print();

getch();

}

SAMPLE INPUT AND OUTPUT:

CLASS AND OBJECTS

ENTER YOUR NAME: RAJA.A

ENTER A REGISTER NUMBER: 12345

ENTER THREE SUBJECT MARKS: 90

98

78

NAME: RAJA.A

REGISTER NUMBER: 12345

THREE SUBJECT MARKS: 90 98 78

RESULT = PASS

TOTAL = 266

AVERAGE = 88

CLASS AND OBJECTS

ENTER YOUR NAME: RAVI.S

ENTER A REGISTER NUMBER: 4321

ENTER THREE SUBJECT MARKS: 10

60

90

NAME: RAVI.S

REGISTER NUMBER: 4321

THREE SUBJECT MARKS: 10 60 90

RESULT = FAIL

Result:

Thus the C++ program for implementing the concept of Classes and Objects has

been executed successfully and the output was verified.

Ex No: 1(B)Date:

Constructor & Destructor

Aim:

To write a C++ program to implement the basic concepts of Constructor and

Destructor.

Algorithm:

1) Start the program.

2) Define a class prime.

3) Define the Constructor under public section, in that read the input.

4) Depend on the input check the given input is prime or not.

5) Define the Destructor to de-allocate the memory for the data members.

6) In main (), create an object for the class prime.

7) Once the object is created, the constructor invoked automatically.

8) Destructor gets invoked at the termination of the program.

9) Stop the program.

// PROGRAM FOR CONSTRUCTOR AND DESTRUCTOR

#include<iostream.h>

#include<conio.h>

#include<process.h>

class prime

{

private:

int a,i,n;

public:

prime()

{

cout<<"\n\n\t\t CONSTRUCTOR AND DESTRUCTOR \n";

cout<<"\n ENTER A NUMBER: ";

cin>>n;

for(i=2;i<=n/2;i++)

{

if(n%2==0)

{

cout<<"\n THE GIVEN NUMBER IS NOT PRIME. "<<endl;

getch();

exit(0);

}

}

cout<<"\n THE GIVEN NUMBER IS PRIME. "<<endl;

}

~prime()

{

cout<<"\n ALL CONTENTS ARE DELETED........"<<endl;

getch();

}

};

void main()

{

clrscr();

prime p;

getch();

}

SAMPLE INPUT AND OUTPUT:

CONSTRUCTOR AND DESTRUCTOR

ENTER A NUMBER: 8

THE GIVEN NUMBER IS NOT PRIME.

CONSTRUCTOR AND DESTRUCTOR

ENTER A NUMBER: 5

THE GIVEN NUMBER IS PRIME.

ALL CONTENTS ARE DELETED........

Result:

Thus the C++ program for implementing the concepts of Constructor and

Destructor has been executed successfully and the output was verified.

Ex No: 2Date:

Array Implementation

Aim:

To write a C++ program to implement the basic concepts of an Array.

Algorithm:

1) Start the program.

2) Define a class array and declare the needed variables.

3) Define the function arrayin (), read the value for n and read the elements for the

array.

4) Define the function print (), to display the array elements.

5) Define the function insert (), insert the element in the array at the given location.

6) Define the function del (), to delete the element in the given location.

7) In main (), create an object for the class.

8) Call the function arrayin () with the object.

9) Display the menus such as insert, delete and display option inside the switch case.

10) Read the choice, depend on the choice, the case gets execute and

the respective function is invoked.

11) Stop the program.

// PROGRAM FOR ARRAY IMPLEMENTATION

#include<iostream.h>

#include<conio.h>

#include<process.h>

class array

{

private:

int a[20],i,n,p,d,i1,d1;

public:

void arrayin()

{

cout<<"\n\n\t\t ARRAY IMPLEMENTATION \n\n";

cout<<"\n ENTER THE ARRAY SIZE : ";

cin>>n;

cout<<"\n ENTER THE ARRAY ELEMENTS ONE BY ONE : "<<endl;

for(i=0;i<n;i++)

{

cin>>a[i];

}

}

void print()

{

cout<<"\n ARRAY ELEMENT "<<endl;

for(i=0;i<n;i++)

{

cout<<a[i]<<endl;

}

}

void insert()

{

cout<<"\n ENTER THE POSITION TO INSERT : "<<endl;

cin>>p;

for(i=n+1;i>=p;i--)

{

a[i]=a[i-1];

}

cout<<"\n ENTER THE ELEMENT TO INSERT : "<<endl;

cin>>i1;

a[i]=i1;

cout<<"\n THE NEW ARRAY ELEMENTS AFTER INSERTION : "<<endl;

for(i=0;i<n+1;i++)

{

cout<<a[i]<<endl;

}

}

void del()

{

cout<<"\n ENTER THE POSITION TO DELETE : ";

cin>>d;

for(i=d-1;i<n+1;i++)

{

a[i]=a[i+1];

}

cout<<"\n THE NEW ARRAY ELEMENTS AFTER DELETION : ";

for(i=0;i<n-1;i++)

{

cout<<a[i]<<endl;

}

}

};

void main()

{

array k;

int ch;

clrscr();

k.arrayin();

cout<<"\n\n\t\t INDEX \n"<<endl;

cout<<"\n 1.ARRAY ELEMENT INSERTION \n"<<endl;

cout<<"\n 2.ARRAY ELEMENT DELETION \n"<<endl;

cout<<"\n 3.exit \n"<<endl;

cout<<"\n ENTER YOUR CHOICE : ";

cin>>ch;

switch (ch)

{

case 1:

k.insert();

break;

case 2:

k.del();

break;

case 3:

exit(0);

default: cout<<"\n INVALID CHOICE "<<endl;

}

getch();

}

SAMPLE INPUT AND OUTPUT:

ARRAY IMPLEMENTATION

ENTER THE ARRAY SIZE: 5

ENTER THE ARRAY ELEMENTS ONE BY ONE:

10

20

30

40

50

INDEX

1. ARRAY ELEMENT INSERTION

2. ARRAY ELEMENT DELETION

3. EXIT

ENTER YOUR CHOICE: 1

ENTER THE POSITION TO INSERT: 2

ENTER THE ELEMENT TO INSERT: 25

THE NEW ARRAY ELEMENTS AFTER INSERTION:

10

25

20

30

40

50

ARRAY IMPLEMENTATION

ENTER THE ARRAY SIZE: 5

ENTER THE ARRAY ELEMENTS ONE BY ONE:

10

20

30

40

50

INDEX

1. ARRAY ELEMENT INSERTION

2. ARRAY ELEMENT DELETION

3. EXIT

ENTER YOUR CHOICE: 2

ENTER THE POSITION TO DELETE: 2

THE NEW ARRAY ELEMENTS AFTER DELETION: 10

30

40

50

Result:

Thus the C++ program for implementing the concept of Array has been executed

successfully and the output was verified.

Ex No: 3Date:

Linked List Operations

Aim:

To write a C++ program to implement all the operations of Linked List.

Algorithm:

1) Start the program.

2) Define the list using structure.

3) In main (), display the menus such as Insert, delete, and display.

4) Read the choice, depend on the choice, the case gets execute and it invokes the

respective function.

5) To insert an element, we can have the following:

6) Define the function insempty (), to insert an element in the empty list.

7) Define the function insbegin (), to insert an element at the beginning of the list.

8) Define the function insmiddle (), to insert an element at the middle of the list.

9) Define function insend (), to insert an element at the end of the list.

10) To delete an element, we can have the following:

11) Define the function delsingle (), to delete the single node element.

12) Define the function delbegin (), to delete the element at the

beginning of the list.

13) Define the function delmiddle (), to delete the element at the

middle of the list.

14) Define the function delend (), to delete the element at the end of

the list.

15) Define the function display (), to display the elements in the list.

16) Stop the program.

// PROGRAM FOR LINKED LIST OPERATIONS

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

struct list

{

int data;

struct list*next;

}*start;

void insempty();

void insbegin();

void insmiddle();

void insend();

void delsingle();

void delbegin();

void delmiddle();

void delend();

void display();

void main()

{

int ch,ch1;

clrscr();

do

{

cout<<"\n\n\t\t SINGLY LINKED LIST \n\n";

cout<<"\n\n\t\t LIST OPERATIONS \n\n";

cout<<"\n 1.INSERT AN ELEMENT \n";

cout<<"\n 2.DELETE AN ELEMENT \n";

cout<<"\n 3.DISPLAY AN ELEMENT \n";

cout<<"\n 4.EXIT \n";

cout<<"\n\n\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\n\n\t\t INSERT OPERATIONS \n\n";

cout<<"\n 1.INSERT AT EMPTY \n";

cout<<"\n 2.INSERT AT BEGIN \n";

cout<<"\n 3.INSERT AT MIDDLE \n";

cout<<"\n 4.INSERT AT END \n";

cout<<"\n\n\n ENTER YOUR CHOICE : ";

cin>>ch1;

switch(ch1)

{

case 1:

insempty();

break;

case 2:

insbegin();

break;

case 3:

insmiddle();

break;

case 4:

insend();

break;

}

break;

case 2:

cout<<"\n\n\t\t DELETE OPERATIONS \n\n";

cout<<"\n 1.SINGLE NODE DELETION \n";

cout<<"\n 2.DELETE AT BEGIN \n";

cout<<"\n 3.DELETE AT MIDDLE \n";

cout<<"\n 4.DELETE AT END \n";

cout<<"\n\n\n ENTER YOUR CHOICE : ";

cin>>ch1;

switch(ch1)

{

case 1:

delsingle();

break;

case 2:

delbegin();

break;

case 3:

delmiddle();

break;

case 4:

delend();

break;

}

break;

case 3:

display();

break;

case 4:

exit(0);

}

}

while(ch<=3);

getch();

}

void insempty()

{

if(start!=NULL)

cout<<"\n LIST IS NOT EMPTY ";

else

{

struct list*node;

node=new list;

cout<<"\n ENTER THE VALUE : ";

cin>>node->data;

node->next=NULL;

start=node;

}

}

void insbegin()

{

if(start==NULL)

cout<<"\n LIST IS EMPTY SO PLEASE INSERT AT EMPTY ";

else

{

struct list*node;

node=new list;

cout<<"\n ENTER THE VALUE : ";

cin>>node->data;

node->next=start;

start=node;

}

}

void insmiddle()

{

int item;

if(start->next==NULL)

cout<<"\n IT IS NOT POSSIBLE TO INSERT AN ELEMENT AT MIDDLE ";

else

{

struct list *node,*pre,*ptr,*temp;

node=new list;

cout<<"\n ENTER THE VALUE : ";

cin>>node->data;

cout<<"\n ENTER THE ELEMENT BEFORE INSERTION : ";

cin>>item;

pre=start;

while(pre->data!=item)

{

temp=pre;

pre=pre->next;

}

temp->next=node;

node->next=pre;

}

}

void insend()

{

if (start==NULL)

cout<<"\n LIST IS EMPTY ";

else

{

struct list *node,*pre,*ptr;

node=new list;

cout<<"\n ENTER THE VALUE : ";

cin>>node->data;

ptr=start;

while(ptr!=NULL)

{

pre=ptr;

ptr=ptr->next;

}

pre->next=node;

node->next=NULL;

}

}

void delsingle()

{

if(start->next!=NULL)

cout<<"\n LIST HAVING MORE THAN ONE NODE ";

else

{

cout<<"\n DELETED NODE IS : "<<start->data;

start=NULL;

}

}

void delbegin()

{

if(start->next==NULL)

cout<<"\n LIST HAVING ONLY ONE NODE ";

else

{

cout<<"\n DELETED ELEMENT IS : "<<start->data;

start=start->next;

}

}

void delmiddle()

{

int item;

struct list*ptr,*pre;

ptr=start->next;

if(ptr->next==NULL)

cout<<"\n THERE IS NO MIDDLE NODE ";

else

{

cout<<"\n ENTER THE ELEMENT TO DELETE : ";

cin>>item;

pre=start;

for(ptr=start;(ptr);ptr->next)

{

if(ptr->data==item)

{

pre->next=ptr->next;

break;

}

pre=ptr;

}

}

}

void delend()

{

if(start->next==NULL)

cout<<"\n IT IS NOT POSSIBLE TO DELETE AT END ";

else

{

struct list*ptr,*pre,*loc;

pre=loc= start;

for(ptr=start;(ptr);ptr->next)

{

loc=pre;

pre=ptr;

}

cout<<"\n DELETED ELEMENT IS : "<<pre->data;

loc->next=NULL;

}

}

void display()

{

struct list*ptr;

if(start==NULL)

cout<<"\n LIST IS EMPTY ";

else

{

cout<<"\n NOW, LIST HAVING THE FOLLOWING ELEMENTS : ";

for(ptr=start;(ptr);ptr=ptr->next)

cout<<ptr->data<<"\n";

}

}

SAMPLE INPUT AND OUTPUT:

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 1

INSERT OPERATIONS1. INSERT AT EMPTY2. INSERT AT BEGIN3. INSERT AT MIDDLE4. INSERT AT END

ENTER YOUR CHOICE: 1ENTER THE VALUE: 10

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 1

INSERT OPERATIONS1. INSERT AT EMPTY2. INSERT AT BEGIN3. INSERT AT MIDDLE4. INSERT AT END

ENTER YOUR CHOICE: 2ENTER THE VALUE: 20

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3LIST HAVING THE FOLLOWING ELEMENTS: 20 10

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 1

INSERT OPERATIONS

1. INSERT AT EMPTY2. INSERT AT BEGIN3. INSERT AT MIDDLE4. INSERT AT END

ENTER YOUR CHOICE: 3ENTER THE VALUE: 30ENTER ELEMENT BEFORE INSERTION: 10

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT

4. EXIT

ENTER YOUR CHOICE: 3LIST HAVING THE FOLLOWING ELEMENTS: 20 30 10

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 1

INSERT OPERATIONS

1. INSERT AT EMPTY2. INSERT AT BEGIN3. INSERT AT MIDDLE4. INSERT AT END

ENTER YOUR CHOICE: 4ENTER THE VALUE: 40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3NOW, LIST HAVING THE FOLLOWING ELEMENTS: 20 30 10

40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS

1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 1LIST HAVING MORE THAN ONE NODE

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS

1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 2DELETED ELEMENT IS: 20

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3NOW, LIST HAVING THE FOLLOWING ELEMENTS: 30

10 40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS

1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 3ENTER THE ELEMENT TO DELETE: 10

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3NOW, LIST HAVING THE FOLLOWING ELEMENTS: 30 40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS

1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 4DELETED ELEMENT IS: 30

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3NOW, LIST HAVING THE FOLLOWING ELEMENTS: 40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS

1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 4IT IS NOT POSSIBLE TO DELETE AT END

SINGLY LINKED LIST

LIST OPERATIONS1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2

DELETE OPERATIONS1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 1DELETED NODE IS: 40

SINGLY LINKED LIST

LIST OPERATIONS

1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 3LIST IS EMPTY

SINGLY LINKED LIST

LIST OPERATIONS1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 2 DELETE OPERATIONS1. SINGLE NODE DELETION2. DELETE AT BEGIN3. DELETE AT MIDDLE4. DELETE AT END

ENTER YOUR CHOICE: 1LIST IS EMPTY

LIST OPERATIONS1. INSERT AN ELEMENT2. DELETE AN ELEMENT3. DISPLAY AN ELEMENT4. EXIT

ENTER YOUR CHOICE: 4

Result:

Thus the C++ program for implementing the Linked List Operations has been

executed successfully and the output was verified.

Ex No: 4Date:

Cursor Implementation of List ADT

Aim:

To write a C++ program to implement the Cursor concepts for LIST ADT.

Algorithm:

1) Start the program.

2) Display the option such as Insert, Delete, Display, Search and Exit.

3) Depend on the choice, case gets execute and the respective function is called.

4) CASE 1: read the total number of elements and read the elements

In the array.

5) CASE 2: insert an element at the desired location.

6) CASE 3: delete an element from the specified location.

7) CASE 4: display the elements present in the array.

8) Stop the program.

// PROGRAM FOR CURSOR IMPLEMENTATION OF LIST ADT

#include<iostream.h>

#include<conio.h>

int main()

{

int a[20],i,j,ch,n,x;

while(1)

{

clrscr();

cout<<"\n\n\t\t CURSOR IMPLEMENTATION OF LIST ADT \n";

cout<<"\n\n\t LIST OPERATIONS \n\n";

cout<<"\n 1.CREATION \n";

cout<<"\n 2.INSERTION \n";

cout<<"\n 3.DELETION \n";

cout<<"\n 4.DISPLAY \n";

cout<<"\n 5.SEARCH \n";

cout<<"\n 6.EXIT \n";

cout<<"\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\n ENTER THE TOTAL NO OF VALUE : ";

cin>>n;

for(i=0;i<n;i++)

{

cout<<"\n ENTER THE "<<i+1<<" VALUE : ";

cin>>a[i];

}

break;

case 2:

cout<<"\n ENTER THE INSERT POSITION : ";

cin>>i;

for(j=n;j>i-1;j--)

a[j]=a[j-1];

cout<<"\n ENTER THE INSERT VALUE : ";

cin>>a[i];

n++;

break;

case 3:

if(n<0)

cout<<"\n THE LIST IS EMPTY ";

else

{

cout<<"\n ENTER THE DELETING POSITION : ";

cin>>j;

cout<<"\n THE DELETED ELEMENT IS : "<<a[j];

for(i=j-1;i<n;i++)

a[i]=a[i+1];

n--;

}

getch();

break;

case 4:

for(i=1;i<n;i++)

cout<<a[i]<<"\t";

getch();

break;

case 5:

if(n<0)

cout<<"\n THE LIST IS EMPTY ";

else

{

cout<<"\n ENTER THE SEARCHING ELEMENT : ";

cin>>x;

for(i=0;i<n;i++)

if(a[i]==x)

{

cout<<"\n THE GIVEN ELEMENT FOUND IN THE POSITION "<<i;

goto aa;

}

cout<<"\n THE GIVEN ELEMENT NOT FOUND IN THIS LIST ";

aa:getch();

break;

case 6:

return(0);

}

getch();

}

}

}

SAMPLE INPUT AND OUTPUT:

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 1

ENTER THE TOTAL NO OF VALUE: 5

ENTER THE 1 VALUE: 10

ENTER THE 2 VALUE: 20

ENTER THE 3 VALUE: 30

ENTER THE 4 VALUE: 40

ENTER THE 5 VALUE: 50

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 4

10 20 30 40 50

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 2

ENTER THE INSERT POSITION: 0

ENTER THE INSERT VALUE: 5

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 4

5 10 20 30 40 50

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 3

ENTER THE DELETING POSITION: 0

THE DELETED ELEMENT IS: 5

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 4

10 20 30 40 50

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 5

ENTER THE SEARCHING ELEMENT: 10

THE GIVEN ELEMENT FOUND IN THE POSITION 0

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 5

ENTER THE SEARCHING ELEMENT: 20

THE GIVEN ELEMENT FOUND IN THE POSITION 1

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 5

ENTER THE SEARCHING ELEMENT: 90

THE GIVEN ELEMENT NOT FOUND IN THIS LIST

CURSOR IMPLEMENTATION OF LIST ADT

LIST OPERATIONS

1. CREATION

2. INSERTION

3. DELETION

4. DISPLAY

5. SEARCH

6. EXIT

ENTER YOUR CHOICE: 6

Result:

Thus the C++ program for implementing the Cursor concepts for LIST ADT Operations

has been executed successfully and the output was verified.

Ex No: 5Date:

STACK ADT using Array Implementations

Aim: To write a C++ program to implement the STACK ADT using array implementations.

Algorithm:

1) Start the program.

2) In main (), display the options such as Push, Pop, and Display.

3) Read the choice, depend on the choice the case gets execute and the respective

function gets invoked.

4) Define the function push (), to insert a new element into the stack.

5) Define the function pop (), to delete an element from the stack.

6) Define the function display (), to display the elements in the stack.

7) Stop the program.

// PROGRAM FOR STACK ADT USING ARRAY IMPLEMENTATION

#include<iostream.h>

#include<conio.h>

#include<process.h>

#define size 3

void push();

void pop();

void display();

int s[size],top=-1;

void main()

{

int ch;

clrscr();

do

{

cout<<"\n\n\t\t STACK ADT USING ARRAY IMPLEMENTATION \n";

cout<<"\n STACK OPERATIONS "<<endl;

cout<<"\n 1.PUSH \n \n 2.POP \n \n 3.DISPLAY \n \n 4.EXIT \n "<<endl;

cout<<"\n ENTER UR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

push();

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

exit(0);

}

}

while(ch<4);

}

void push()

{

int no;

if(top==size-1)

cout<<"\n STACK IS FULL "<<endl;

else

{

cout<<"\n ENTER THE NUMBER : ";

cin>>no;

top++;

s[top]=no;

}

}

void pop()

{

int no;

if(top==-1)

cout<<"\n STACK IS EMPTY "<<endl;

else

{

no=s[top];

cout<<"\n ELEMENT "<<no<<" IS POPPED OUT FROM THE STACK "<<endl;

top--;

}

}

void display()

{

int i;

if(top==-1)

{

cout<<"\n STACK IS EMPTY "<<endl;

return;

}

cout<<"\n THE ELEMENTS IN STACK ARE : "<<endl;

for(i=top;i>=0;i--)

{

cout<<s[i]<<endl;

} }

SAMPLE INPUT AND OUTPUT:

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 1

ENTER THE NUMBER: 10

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 1

ENTER THE NUMBER: 20

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 1

ENTER THE NUMBER: 30

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 3

THE ELEMENTS IN STACK ARE:

30

20

10

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 2

ELEMENT 30 IS POPPED OUT FROM THE STACK

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 3

THE ELEMENTS IN STACK ARE:

20

10

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 2

ELEMENT 20 IS POPPED OUT FROM THE STACK

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 3

THE ELEMENTS IN STACK ARE:

10

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 2

ELEMENT 10 IS POPPED OUT FROM THE STACK

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 3

STACK IS EMPTY

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 2

STACK IS EMPTY

STACK ADT USING ARRAY IMPLEMENTATION

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER UR CHOICE: 4

Result: Thus the C++ program for implementing the STACK ADT operations using Array

has been executed successfully and the output was verified.

Ex No: 6Date:

STACK ADT using Linked List

Aim: To write a C++ program to implement the STACK ADT using linked list.

Algorithm:

1) Start the program.

2) In main (), display the options such as Push, Pop, and Display.

3) Read the choice, depend on the choice the case gets execute and the respective

function gets invoked.

4) Define the function sadd (), to insert a new element into the stack.

5) Define the function sdelete (), to delete an element from the stack.

6) Define the function sprint (), to display the elements in the stack.

7) Stop the program.

// PROGRAM FOR STACK ADT USING LINKED LIST

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int ch,top=0,n;

void sadd();

void sdelete();

void sprint();

struct list

{

int data;

struct list*next;

}*start;

void main()

{

clrscr();

cout<<"\n\n\t\t STACK ADT USING LINKED LIST \n";

cout<<"\n ENTER THE SIZE OF THE STACK : ";

cin>>n;

W:

cout<<"\n\n\t\t STACK OPERATIONS \n";

cout<<"\n 1.PUSH \n";

cout<<"\n 2.POP \n";

cout<<"\n 3.DISPLALY \n";

cout<<"\n 4.EXIT \n";

cout<<"\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

sadd();

break;

case 2:

sdelete();

break;

case 3:

sprint();

break;

case 4:

exit(0);

}

if(ch<=3)

goto W;

getch();

}

void sadd()

{

if(top==n)

{

cout<<"\n STACK IS FULL \n";

}

else

{

if(start==NULL)

{

struct list * node;

node=new list;

cout<<"\n ENTER THE VALUE TO PUSH : ";

cin>>node->data;

node->next=NULL;

start=node;

top++;

}

else

{

struct list * node;

node=new list;

cout<<"\n ENTER THE VALUE TO PUSH : ";

cin>>node->data;

node->next=start;

start=node;

top++;

}

}

}

void sprint()

{

struct list*ptr;

if(start==NULL)

cout<<"\n STACK IS EMPTY ";

else

{

cout<<"\n STACK HAVING THE FOLLOWING ELEMENTS : ";

for(ptr=start;(ptr);ptr=ptr->next)

cout<<ptr->data<<"\n";

getch();

}

}

void sdelete()

{

if(top<=0)

{

cout<<"\n STACK IS EMPTY ";

}

else

{

if(start->next!=NULL)

{

cout<<"\n DELETED ELEMENT IS : "<<start->data;

start=start->next;

}

else

{

cout<<"\n DELETED ELEMENT IS : "<<start->data;

start=NULL;

}

}

top--;

}

SAMPLE INPUT AND OUTPUT:

STACK ADT USING LINKED LIST

ENTER THE SIZE OF THE STACK: 5

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO PUSH: 10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO PUSH: 20

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO PUSH: 30

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO PUSH: 40

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO PUSH: 50

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

STACK IS FULL

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK HAVING THE FOLLOWING ELEMENTS: 50

40

30

20

10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 50

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK HAVING THE FOLLOWING ELEMENTS: 40

30

20

10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 40

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK HAVING THE FOLLOWING ELEMENTS: 30

20

10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 30

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK HAVING THE FOLLOWING ELEMENTS: 20

10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 20

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK HAVING THE FOLLOWING ELEMENTS: 10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 10

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

STACK IS EMPTY

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

STACK IS EMPTY

STACK OPERATIONS

1. PUSH

2. POP

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 4

Result: Thus the C++ program for implementing the STACK ADT operations using Linked

List has been executed successfully and the output was verified.

Ex No: 7Date:

Infix to Postfix Conversion using STACKApplication

Aim: To write a C++ program to convert Infix to postfix expression using STACK Application.

Algorithm:

1) Start the program.

2) Define the class stack and declare the needed variables.

3) Define the constructor to initialize the data members.

4) Define the function get () to read the equation.

5) Define the function post () and prcd () to convert the Infix to Postfix conversion.

6) In main (), declare an object for the class.

7) Call the function get () and post () and prcd () with the object.

8) Stop the program.

// PROGRAM FOR INFIX TO POSTFIX CONVERSION USING STACK

APPLICATION

#include<iostream.h>

#include<conio.h>

class stack

{

char ch[20],eq[20],st[20];

char top;

int x,y,z;

public:

stack()

{

top='+';

x=y=0;

z=-1;

}

void get();

void post();

int prcd(char,char);

};

void stack::get()

{

cout<<"\n\n\t\t INFIX TO POSTFIX CONVERSION USING STACK APPLICATION \n";

cout<<"\n\n ENTER ANY EQUATION : ";

cin>>st;

cout<<"\n BEFORE INFIX TO POSTFIX CONVERSION : "<<st;

}

void stack::post()

{

while(st[x]!='\0')

{

if((st[x]>='A'&&st[x]<'Z')||(st[x]>='a'&&st[x]<='z'))

ch[y++]=st[x];

else if(st[x]==')')

{

while(eq[z]!=')')

ch[y++]=eq[z--];

top=eq[--z];

}

else

{

while(z>=0&&prcd(top,st[x]==1))

{

ch[y++]=top;

top=eq[--z];

}

eq[++z]=st[x];

top=st[x];

}

x++;

}

while(z>=0)

{

if(eq[z]=='(')

z--;

else

ch[y++]=eq[z--];

}

ch[y]='\0';

cout<<"\n\n AFTER INFIX TO POSTFIX CONVERSION : "<<ch;

}

int stack::prcd(char g,char r)

{

if(r=='('||r=='$')

return 0;

else if(g=='$'||g=='*'||g=='/')

return 1;

else if(r=='*'||g=='9'||r=='/')

return 0;

else

return 1;

}

void main()

{

stack s;

clrscr();

s.get();

s.post();

getch();

}

SAMPLE INPUT AND OUTPUT:

INFIX TO POSTFIX CONVERSION USING STACK APPLICATION

ENTER ANY EQUATION: A+B

BEFORE INFIX TO POSTFIX CONVERSION: A+B

AFTER INFIX TO POSTFIX CONVERSION: AB+

INFIX TO POSTFIX CONVERSION USING STACK APPLICATION

ENTER ANY EQUATION: E-F

BEFORE INFIX TO POSTFIX CONVERSION: E-F

AFTER INFIX TO POSTFIX CONVERSION: EF-

INFIX TO POSTFIX CONVERSION USING STACK APPLICATION

ENTER ANY EQUATION: A+B-C

BEFORE INFIX TO POSTFIX CONVERSION: A+B-C

AFTER INFIX TO POSTFIX CONVERSION: AB+C-

Result:

Thus the C++ program to convert Infix to postfix expression using STACK

Application has been executed successfully and the output was verified.

Ex No: 8Date: QUEUE Operations using Array

Aim:

To write a C++ program to implement all QUEUE operations using Arrays.

Algorithm:

1) Start the program.

2) In main () display the option such as Add, Delete, and Print.

3) Read the choice, depend on the choice the case gets execute and the respective

function gets invoked.

4) Define the function qadd (), to insert an element in the queue.

5) Define the function qdel (), to delete an element from the queue.

6) Define the function qprint (), to display he elements of queue.

7) Stop the program.

// PROGRAM FOR QUEUE OPERATIONS USING ARRAY

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int queue[10];

int ch,rear=0,front=0,n;

void qadd();

void qdelete();

void qprint();

void main()

{

clrscr();

cout<<"\n\n\t\t QUEUE OPERATIONS USING ARRAY \n";

cout<<"\n ENTER THE SIZE OF THE QUEUE : ";

cin>>n;

W:

cout<<"\n\n\t\t QUEUE OPERATIONS \n";

cout<<"\n 1.INSERTION \n";

cout<<"\n 2.DELETION \n";

cout<<"\n 3.DISPLAY \n";

cout<<"\n 4.EXIT \n";

cout<<"\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

qadd();

break;

case 2:

qdelete();

break;

case 3:

qprint();

break;

case 4:

exit(0);

}

if(ch<=3)

goto W;

}

void qadd()

{

if(rear>=n)

{

cout<<"\n QUEUE IS FULL \n";

}

else

{

cout<<"\n ENTER THE ELEMENT : ";

cin>>queue[rear];

rear++;

}

}

void qprint()

{

int i;

clrscr();

if(rear==front)

{

cout<<"\n QUEUE IS EMPTY \n";

}

else

{

cout<<"\n QUEUE HAVING THE FOLLOWING ELEMENTS : \n";

for(i=front;i<rear;i++)

cout<<"\n ELEMENT IN THE POSITION "<<i<<" IS "<<queue[i]<<endl;

}

getch();

}

void qdelete()

{

if(rear==front)

{

cout<<"\n QUEUE IS EMEPTY \n";

getch();

}

else

{

cout<<"\n DELETED ELEMENT IN THE QUEUE IS : "<<queue[front];

front++;

}

}

SAMPLE INPUT AND OUTPUT:

QUEUE OPERATIONS USING ARRAY

ENTER THE SIZE OF THE QUEUE: 5

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 10

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 20

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 30

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 40

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

QUEUE IS FULL

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS:

ELEMENT IN THE POSITION 0 IS 10

ELEMENT IN THE POSITION 1 IS 20

ELEMENT IN THE POSITION 2 IS 30

ELEMENT IN THE POSITION 3 IS 40

ELEMENT IN THE POSITION 4 IS 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IN THE QUEUE IS: 10

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS:

ELEMENT IN THE POSITION 1 IS 20

ELEMENT IN THE POSITION 2 IS 30

ELEMENT IN THE POSITION 3 IS 40

ELEMENT IN THE POSITION 4 IS 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IN THE QUEUE IS: 20

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS:

ELEMENT IN THE POSITION 2 IS 30

ELEMENT IN THE POSITION 3 IS 40

ELEMENT IN THE POSITION 4 IS 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IN THE QUEUE IS: 30

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS:

ELEMENT IN THE POSITION 3 IS 40

ELEMENT IN THE POSITION 4 IS 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IN THE QUEUE IS: 40

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS:

ELEMENT IN THE POSITION 4 IS 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IN THE QUEUE IS: 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE IS EMPTY

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

QUEUE IS EMEPTY

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 4

Result:

Thus the C++ program for implementing the QUEUE operations using array has

been executed successfully and the output was verified.

Ex No: 9Date: QUEUE Operations using Linked List

Aim:

To write a C++ program to implement all QUEUE operations using Linked List.

Algorithm:

1) Start the program.

2) Define the list using structure.

3) In main (), display the option such as Add, Delete, and Print.

4) Read the choice, depend on the choice the case gets execute and

The respective function gets invoked.

5) Define the function qadd (), to insert an element in the queue.

6) Define the function qdel(), to delete an element from the queue.

7) Define the function qprint (), to display the elements of queue.

8) Stop the program.

// PROGRAM FOR QUEUE OPERATIONS USING LINKED LIST

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

struct list

{

int data;

struct list*next;

}*start;

int ch,rear=0,front=0,n;

void qadd();

void qdelete();

void print();

void main()

{

clrscr();

cout<<"\n\n\t\t QUEUE OPERATIONS USING LINKED LIST \n\n";

cout<<"\n ENTER THE SIZE OF THE QUEUE : ";

cin>>n;

W:

cout<<"\n\n\t QUEUE OPERATIONS \n\n";

cout<<"\n 1.INSERTION \n";

cout<<"\n 2.DELETION \n";

cout<<"\n 3.DISPLAY \n";

cout<<"\n 4.EXIT \n\n";

cout<<"\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

qadd();

break;

case 2:

qdelete();

break;

case 3:

print();

break;

case 4:

exit(0);

}

if(ch<=3)

goto W;

}

void qadd()

{

if(rear==n)

{

cout<<"\n QUEUE IS FULL\n";

}

else

{

if(start==NULL)

{

struct list*node;

node=new list;

cout<<"\n ENTER THE VALUE TO INSERT : ";

cin>>node->data;

node->next=NULL;

start=node;

rear=rear+1;

}

else

{

struct list*node,*pre,*ptr;

node=new list;

cout<<"\n ENTER THE VALUE TO INSERT : ";

cin>>node->data;

for(ptr=start;(ptr);ptr=ptr->next)

{

pre=ptr;

}

pre->next=node;

node->next=NULL;

rear=rear+1;

}

}

}

void print()

{

int i;

clrscr();

if(rear==front)

{

cout<<"\n QUEUE IS EMPTY ";

}

else

{

struct list*ptr;

cout<<"\n QUEUE HAVING THE FOLLOWING ELEMENTS : ";

for(ptr=start;(ptr);ptr=ptr->next)

cout<<ptr->data<<"\n";

}

}

void qdelete()

{

if(rear==front)

{

cout<<"\n QUEUE IS EMPTY ";

getch();

}

else

{

if(start->next==NULL)

{

cout<<"\n DELETED ELEMENT IS : "<<start->data;

start=NULL;

front=front+1;

}

else

{

cout<<"\n DELETED ELEMENT IS : "<<start->data;

start=start->next;

front=front+1;

}

}

}

SAMPLE INPUT AND OUTPUT:

QUEUE OPERATIONS USING LINKED LIST

ENTER THE SIZE OF THE QUEUE: 5

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO INSERT: 10

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO INSERT: 20

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO INSERT: 30

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO INSERT: 40

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

ENTER THE VALUE TO INSERT: 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

QUEUE IS FULL

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS: 10

20

30

40

50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 10

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS: 20

30

40

50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 20

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS: 30

40

50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 30

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS: 40

50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 40

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE HAVING THE FOLLOWING ELEMENTS: 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 2

DELETED ELEMENT IS: 50

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 3

QUEUE IS EMPTY

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 1

QUEUE IS FULL

QUEUE OPERATIONS

1. INSERTION

2. DELETION

3. DISPLAY

4. EXIT

ENTER YOUR CHOICE: 4

Result:

Thus the C++ program for implementing the QUEUE operations using Linked List

has been executed successfully and the output was verified.

Ex No: 10Date: Binary Search Tree

Aim:

To write a C++ program to implement a Binary Search Tree.

Algorithm:

1) Start the program.

2) Define the node using structure.

3) Display the choice such as Insert, Pre-order, Post-order and In-order.

4) Depend on the choice, the case gets execute and respective function gets invoked.

5) Define the function insert (), to insert an element in the binary tree.

6) Define the function preorder (), to perform preorder traversal.

7) Define the function inorder (), to perform inorder traversal.

8) Define the function postorder (), to perform postorder traversal.

9) Stop the program.

// PROGRAM FOR BINARY SEARCH TREE

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

struct node

{

int data;

node*left;

node*right;

};

node*tree=NULL;

node*insert(node*tree,int ele);

void preorder(node*tree);

void inorder(node*tree);

void postorder(node*tree);

int count=1;

void main()

{

int ch,ele;

clrscr();

do

{

clrscr();

cout<<"\n\n\t\t BINARY SEARCH TREE \n";

cout<<"\n\n\n 1.INSERT A NODE IN A BINARY TREE \n";

cout<<"\n 2.PREORDER TRAVERSAL \n";

cout<<"\n 3.INORDER TRAVERSAL \n";

cout<<"\n 4.POSTORDER TRAVERSAL \n";

cout<<"\n 5.EXIT \n";

cout<<"\n\n\n ENTER YOUR CHOICE : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\n ENTER THE ELEMENT : ";

cin>>ele;

tree=insert(tree,ele);

break;

case 2:

cout<<"\n PREORDER TRAVERSAL OF A BINARY TREE : ";

preorder(tree);

break;

case 3:

cout<<"\n INORDER TRAVERSAL OF A BINARY TREE : ";

inorder(tree);

break;

case 4:

cout<<"\n POSTORDER TRAVERSAL OF A BINARY TREE : ";

postorder(tree);

break;

case 5:

exit(0);

}

}

while(ch!=5);

}

node *insert(node*tree,int ele)

{

if(tree==NULL)

{

tree=new node;

tree->left=tree->right=NULL;

tree->data=ele;

count++;

}

else

if(count%2==0)

tree->left=insert(tree->left,ele);

else

tree->right=insert(tree->right,ele);

return(tree);

}

void preorder(node*tree)

{

if(tree!=NULL)

{

cout<<tree->data;

preorder(tree->left);

preorder(tree->right);

getch();

}

}

void inorder(node*tree)

{

if(tree!=NULL)

{

inorder(tree->left);

cout<<tree->data;

inorder(tree->right);

getch();

}

}

void postorder(node*tree)

{

if(tree!=NULL)

{

postorder(tree->left);

postorder(tree->right);

cout<<tree->data;

getch();

}

}

SAMPLE INPUT AND OUTPUT:

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 10

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 20

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 1

ENTER THE ELEMENT: 30

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 2

PREORDER TRAVERSAL OF A BINARY TREE: 10 20 30

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 3

INORDER TRAVERSAL OF A BINARY TREE: 20 10 30

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 4

POSTORDER TRAVERSAL OF A BINARY TREE: 20 30 10

BINARY SEARCH TREE

1. INSERT A NODE IN A BINARY TREE

2. PREORDER TRAVERSAL

3. INORDER TRAVERSAL

4. POSTORDER TRAVERSAL

5. EXIT

ENTER YOUR CHOICE: 5

Result:

Thus the C++ program for implementing the Binary Search Tree has been

executed successfully and the output was verified.

Ex No: 11Date: Heap Sort

Aim:

To write a C++ program to sort a given numbers using Heap Sort.

Algorithm:

1) Start the program.

2) In main (), read the elements in the array.

3) Call the function fnSortHeap(), to sort the given numbers in an ascending order.

4) Display the elements in the sorted order.

5) Stop the program.

// PROGRAM FOR HEAP SORT

#include<stdio.h>

#include<conio.h>

#include<iostream.h>

void fnsortheap(int[],int);

void main()

{

int i,arr_num_items;

int arr[6];

clrscr();

cout<<"\n\n\t\t HEAP SORT \n\n";

cout<<"\n\n\n ENTER THE ELEMENTS : "<<endl;

for(i=0;i<6;i++)

cin>>arr[i];

// TOTAL NUMBER OF ITEMS IN ARRAY

arr_num_items=6;

//CALL FNSORTHEAP FUNCTION FOR(ARR_NUM_ITEMS - 2)TIMES

for(i=arr_num_items;i>1;i--)

{

fnsortheap(arr,i-1);

}

//PRINT THE SORTED ARRAY

cout<<"\n\n\n THE SORTED ARRAY IS : "<<endl;

for(i=0;i<arr_num_items;i++)

cout<<arr[i]<<"\n";

getch();

return;

}

// SORT HEAP

void fnsortheap(int arr[],int arr_ubound)

{

int i,o;

int lchild,rchild,mchild,root,temp;

//FIND THE ROOT ELEMENT OF THE CURRENT ELEMENT

root=(arr_ubound-1)/2;

//CREATING THE HEAP

for(o=root;o>=0;o--)

{

for(i=root;i>=0;i--)

{

lchild=(2*i)+1;

rchild=(2*i)+2;

if((lchild<=arr_ubound)&&(rchild<=arr_ubound))

{

if(arr[rchild]>=arr[lchild])

mchild=rchild;

else

mchild=lchild;

}

else

{

if(rchild>arr_ubound)

mchild=lchild;

else

mchild=rchild;

}

if(arr[i]<arr[mchild]) // SWAP ELEMENTS

{

temp=arr[i];

arr[i]=arr[mchild];

arr[mchild]=temp;

}

}

}

temp=arr[0]; //MOVE THE MAXIMUM ELEMENT TO THE END OF THE

ARRAY

arr[0]=arr[arr_ubound];

arr[arr_ubound]=temp;

return;

}

HEAP SORT

ENTER THE ELEMENTS:

56

87

6987

45

3

1

THE SORTED ARRAY IS:

1

3

45

56

87

6987

Result:

Thus the C++ program for sorting a given numbers using Heap Sort has been

executed successfully and the output was verified.

Ex No: 12Date: Quick Sort

Aim:

To write a C++ program to sort a given numbers using Quick Sort.

Algorithm:

1) Start the program.

2) In main (), read the elements in the array.

3) Display the elements in the unsorted manner.

4) Define the function partition () and Quick Sort (), to sort the given numbers in an

ascending order.

5) Display the elements in the sorted order.

6) Stop the program.

// PROGRAM FOR QUICK SORT

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

int partition(int low,int high,int arr[]);

void quick_sort(int low,int high,int arr[]);

void main()

{

int *a,n,low,high,i;

clrscr();

cout<<"\n\n\t\t QUICK SORT \n";

cout<<"\n ENTER THE NUMBER OF ELEMENTS : ";

cin>>n;

a=new int[n];

cout<<"\n ENTER THE ELEMENTS : ";

for(i=0;i<n;i++)

cin>>a[i];

cout<<"\n\n\n INITIAL ORDER OF ELEMENTS : ";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

cout<<" ";

high=n-1;

low=0;

quick_sort(low,high,a);

cout<<"\n\n\n FINAL ARRAY OF SORTING : ";

for(i=0;i<n;i++)

cout<<a[i]<<" ";

getch();

}

/* FUNCTION FOR PARTITIONING THE AREA */

int partition(int low,int high,int arr[])

{

int i,high_vac,low_vac,pivot/*,itr*/;

pivot=arr[low];

while(high>low)

{

high_vac=arr[high];

while(pivot<high_vac)

{

if(high<=low)

break;

high--;

high_vac=arr[high];

}

arr[low]=high_vac;

low_vac=arr[low];

while(pivot>low_vac)

{

if(high<=low)

break;

low++;

low_vac=arr[low];

}

arr[high]=low_vac;

}

arr[low]=pivot;

return low;

}

void quick_sort(int low,int high,int arr[])

{

int piv_index,i;

if(low<high)

{

piv_index=partition(low,high,arr);

quick_sort(low,piv_index-1,arr);

quick_sort(piv_index+1,high,arr);

}

}

QUICK SORT

ENTER THE NUMBER OF ELEMENTS: 6

ENTER THE ELEMENTS: 2

45

1

897

6

4

INITIAL ORDER OF ELEMENTS: 2 45 1 897 6 4

FINAL ARRAY OF SORTING: 1 2 4 6 45 897

Result:

Thus the C++ program for sorting a given numbers using Quick Sort has been

executed successfully and the output was verified.

Recommended