You are on page 1of 78

Ex.

No:1a

DEFAULT ARGUMENTS

Aim: To implement function with default arguments.

Algorithm: 1. Declare the default function. 2. Invoke the default function 3. Display the result

Program:

#include<iostream.h> Void printLine(char=_,int=70); Void main() { printLine(); printLine(/); printLine(*,40); printLine(R,55); } Void printLine(char ch, int Repeatcount) { int I; Cout<<endl; for(i=0;i<Repeatcount;i++) cout<<ch; } Result: Thus the Given Program function with Default Arguments was implemented successfully.

Ex.No:1b

CLASS WITH STATIC DATA MEMBER

Aim: To implement static data member in class

Algorithm:

1. Create class ITEM with static data member as count. 2. Create a member function to increment the count 3. Declare the static datamember using scope resolution operator 4. Display the count value.

Program:

#include<iostream.h> Class item { Static int count; Int num; Public: Void getdata(int a) { Num=a; Count++; Cout<<Number<<num; } Void showcount() { Cout<<count; Cout<<count<<\n; }

}; Int item::count; Int main() { Item a,b,c; a.showcount(); b.showcount(); c.showcount(); a.getdata(20); b.getdata(30); a.getdata(40); a.showcount(); b.showcount(); c.showcount(); }

Output:

Count Count Count Number Number Number Count Count Count

0 0 0 20 30 40 0 0 0

Result: Thus the Given Program static data member in class was implemented successfully.

Ex.No:1c

CLASS WITH STATIC MEMBER FUNCTION

Aim: To implement static member function in class.

Algorithm:

1. 2. 3. 4.

Create class ITEM with static data member as count. Create a member function to increment the count. Declare the static data member using scope resolution operation Display the count value.

Program:

#include<iostream.h> Class test { int code; Static int count; Public: Void setcode() { cout<<Object Number.<<code<<\n; } }; int test::count; int main() { test t1,t2;

t1.setcount(); t2.setcount(); test::showcount(); test t3; t1.showcode(); t2.showcode(); t3.showcode(); return(0); }

Output: count count Object Number Object Number Object Number 2 3 1 2 3

Result: Thus the Given Program static member function in class was implemented successfully.

Ex.No:2

IMPLEMENTATION OF VARIOUS LIST OPERATIONS USING ARRAYS

Program: #include<iostream.h> #include<stdio.h> #include<string.h> #include<conio.h> class arr_list { /*data structure for list using array*/ private: struct node { int data; int next; }a[10]; public: int head; arr_list(); int create(); void display(int); void insert(); void delete(); void search(); }; arr_list::arr_list() { for(int i=0;i<10;i++) { a[i].data=-1; }

} int arr_list::create() { int head,I; cout<<\nEnter the index for first node; cin>>I; head=i; while(i!=-1) { cout<<\nenter the data and index of the first element; cin>>a[i].next; i=a[i].next; } return head; } void arr_list::display(int i) { while(i!=-1) { if(a[i].data==-1) cout<<; else { cout<<a[i].data<<->; } i=a[i].next; } cout<<NULL; } void arr_list::insert() {

int I,new_data,temp; cout<<\n Enter the new data which is to be inserted; cin>>new_data; cout<<\n Enter the data after which you want to insert; cin>>temp; for(i=0;i<10;i++) { if(a[i].data==temp) break; } if(a[i+1].data==-1)/*next location is empty*/ { a[i+1].next=a[i].next; a[i].next=i+1; a[i+1].data=new_data; } } void arr_list::delete() { int I,temp,current,new_next; cout<<\nEnter the node to deleted; cin>>temp; for(i=0;i<10;i++) { if(a[i].data==temp) { if(a[i].next==-1) { a[i].data=-1; /*writing -1 means deleting the element*/ } current=I; /* marking the index of an array at which record is placed*/

new_next=a[i].next; /*storing the next pointer at that index*/ } } for(i=0;i<10;i++) { if(a[i].next==current) { a[i].next=new_next; a[current].data=-1; } } } void arr_list::search() { int I,temp,flag=0; cout<<\n Enter the node to be searched; cin>>temp; for(i=0;i<10;i++) { if(a[1].data==temp) { flag=1; /* flag 1 means the element is present*/ break; } } if(flag==1) cout<<\n the<<temp<<node is present is the list; else cout<<\n the node is not pesent; } void main()

{ char ans; int choice; arr_list obj; do { clrscr(); cout<<\t program for implementing list using array; cout<<\n main menu; cout<<\n 1.creation; cout<<\n 2.display; cout<<\n 3.insertion of element in the list; cout<<\n 4.deletion of the element in the list; cout<<\n 5.Searching of the element in the list; cout<<\n 6.Exit; cout<<\n Enter your choice; switch(choice) { case 1: obj.head=obj.create(); break; case 2: obj.display(oj.head); break; case 3: obj.insert(); break; case 4: obj.delete(); break; case 5:

obj.search(); break; case 6: exit(0); } cout<<\n do you want to go to main menu?; ans=getch(); } while(ans==Y || ans==y) getch(); }

Output:

main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit Enter your choice 1 enter the index for the first node 4 Enter the data and index of the first element 101 Enter the data and index of the first element 206 Enter the data and index of the first element 307 Enter the data and index of the first element 40-1

do you want to go to main menu? y program for implementing list using array main menu

1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 2 10->20->30->40->null

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 3 enter the new data which is to inserted 21 enter the data after which you want to insert 20

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list

5.Searching of the element in the list 6.exit

enter your choice 4 enter the node to be deleted 30

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 2 10->20->21->40->null

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 5

enter the node to be searched 20 the 20 is present in the list

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 5 enter the node to searched 30 the node is not present

do you want to go to main menu? y program for implementing list using array main manu 1.creation 2.display 3.insertion of element in the list 4.deletion of the element in the list 5.Searching of the element in the list 6.exit

enter your choice 6

Result: Thus the Array implementation of List ADT program was executed successfully.

Ex.No:3

LINKED LIST IMPLEMENTATION USING LIST ADT

Aim: To implement a linked list and do all operations on it.

Algorithm: Step1: Start the process Step2: Initialize and declare variaples Step3: Enter the choice Step4: If choice is INSERT then a. Enter the element to be inserted b. Get a new node and set DATA[NEWNODE]=ITEM c. Find the node after which the new is to be inserted. d. Adjust the link fields. e. Print the linked list after insertion

Step5: If choices DELETE then a. Enter the element to be deleted. b. Find the node containing the element (LOC) and its preceding node (PAR). c. Set ITEM =DATA[LOC] and delete the LOC. d. Adjust the link fields so that PAR points to the next element. ie LINK[PAR]=LINK[LOC]. e. Print the linked list after deletion .

Step 6: Stop the process.

Program:

Demonstration program to perform various operations on singly link list. //List of include files

#include<iostrem.h> #include<conio.h> #define TRUE 1 #define FALSE 0 //class definition { class sll { private: struct node { int data; struct node *next; }*head; public: sll(); void create(); void display(); void search(int key); void insert_head(); void insert_after(); void insert_last(); void dele(); -sll(); }; /*the constructor defined*/ sll::sll() { head=NULL; //initialize head to NULL } /*destructor defined*?

sll::-sll() { node *temp,*temp1; temp=head->next; delete head; while(temp!=NULL) //free the memory allocated { temp1=temp->next; delete temp; temp=temp1; } } /*create function*/ void sll::create() { node *temp,*new; int val,flag; char ans=y; flag=TRUE; do { cout<<\n Enter the data; cin>>val; //allocate memory to new node new=new node; if(new==NULL) cout<<unable to allocate memory\n; new->next=NULL; if(flag==true)//executed only for the first time { head=new;

temp=new; flag=FALSE; } else { /* temp last keeps track of the most recently created node*/ temp->next=new; temp=new; } cout<<\n do you want to enter more elements?(y/n); ans=getch(); } while(ans==y||ans==Y); cout<<\n the singly linked list is created\n; getch(); clrscr(); } /* the display function */ void sll::display() { node *temp; if(temp==NULL) { cout<<\n the list is empty\n; getch(); clrscr(); return; } while(temp!=NULL) { cout<<temp->data<<;

temp=temp->next; } getch(); } void sll::search(int key) { node *temp; int found; temp=head; if(temp==NULL) { cout<<Linked list is empty\n; getch(); clrscr(); } found=FALSE; while(temp!=NULL && found==FALSE) { if(temp->data!=key) temp=temp->next; else found=TRUE; } if(found==TRUE) { cout<<\n the element is present in the list\n; getch(); } else { cout<<\n the element is not present in the list\n;

} } /* the delete function*/ void sll::dele() { node *temp,*prev; int key; temp=head; clrscr(); cout<<\n Enter data of the node you want to delete; cin>>key; while(temp!=NULL) { if(temp->data==key) // traverse till required node to delete break; // is found prev=temp; temp=temp->next; } if(temp==NULL) cout<<\n node not found; else { if(temp==head) //first node head=temp->next; else prev->next=temp->next; //intermediate or end node delete temp; cout<<\nthe elements is deleted\n; } getch(); }

// function to insert at end void sll::insert_last() { node *new,*temp; cout<<\n Enter the element which you want to insert; cin>>new->data; if(head==NULL) head=new; else { temp=head; while(temp->next!=NULL) temp=temp->next; temp->next=new; new->next=NULL; } } /* function to insert after a node*/ void sll::insert_after() { int key; node *temp,*new; cout<<\n Enter the element which you want to insert; cin>>new->data; if(head==NULL) { head=new; } else { cout<<\n Enter the element after which you want to insert the node;

cin>>key; temp=head; do { if(temp->data==key) { New->next=temp->next; temp->next=New; break; } else temp=temp->next; } } void sll::insert_head() { node *New,*temp; New=new node; cout<<\n Enter the element which you want to insert; cin>>New->data; if(head==NULL) head=New; else { temp=head; New->next=temp; head=New; } } void main() {

sll s; int choice,val ch1; char ans=y; do { clrscr(); cout<<\n program to perform various operations on linked list; cout<<\n 1.create; cout<<\n2.display; cout<<\n3.search; cout<<4.insert an element in a list; cout<<\n 5.delete an element from list; cout<<\n 6.quit; cout<<\n Enter your choice(1-6); cin>>choice; switch(choice) { case 1: s.create(): break; case 2: s.display(); break; case 3: cout<< Enter the element you want to serch; cin>>val; s.search(val); break; case 4: clrscr(); cout<<\n the list is \n;

s.display(); cout<<\n menu; cout<<\n 1.insert at beginning \n2.insert after \n3.insert at end \n Enter your choice; cin>>ch1; switch(ch1) { case 1: s.insert_head(); break; case 2: s.insert_after(); break; default: cout<<\n invalid choice; } break; case 5: s.dele(); break; default: cout<<\n invalid choice; } cout<<\n continue?; cin>>ans; } while(ans==y || ans==Y) getch(); return; }

Output: Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 1

enter the data: 10

do you want to enter more elements?(y/n)y enter the data: 20

do you want to enter more elements?(y/n)y enter the data: 30

do you want to enter more elements?(y/n)y enter the data: 40

do you want to enter more elements?(y/n)y enter the data: 50

do you want to enter more elements?(y/n)n

Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list

5.delete an element from list 6.quit enter your choice(1-6) 2 10 20 30 40 50 continue?

Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 3 enter the element you want to search 30

the element is present in the list

continue? Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 4

the list is: 10 20 30 40 50 menu

1.insert at beginning 2.insert after 3.insert at end enter your choice 1

enter the element which you want to insert 9

continue? Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 4

the list is: 9 10 20 30 40 50 menu 1.insert at beginning 2.insert after 3.insert at end enter your choice 2

enter the element which you want to insert 31 enter the element after which you want to insert the node 30

continue? Program to perform various operations on linked list 1.create

2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 4

the list is: 9 10 20 30 31 40 50 menu 1.insert at beginning 2.insert after 3.insert at end enter your choice 3

enter the element which you want to insert 60

continue? Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 2 9 10 20 30 31 40 50 60

continue? Program to perform various operations on linked list 1.create

2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 5 enter the data of the node you want to delete: 10 the element is deleted

continue? Program to perform various operations on linked list 1.create 2.display 3.search 4.insert an element in a list 5.delete an element from list 6.quit enter your choice(1-6) 2 9 20 30 31 40 50 60

Result: Thus the given program Linked list implementation of the list ADT was executed successfully.

Ex.No:4

CURSOR IMPLEMENTATION LIST ADT

Aim: To write a C++ program for cursor implementation of list ADT.

Algorithm:

1. Start the program 2. Create a node with two fields data and link field. i) ii) Allocate space for the node dynamically. Create link between the created nodes and left the last node be with NULL. iii) Insert the input data in the data field and press 1 to stop the state. 3. Get the choice of operations either insertion or deletion 4. For insertion get the position in which insertion is to be done and the element to be inserted . Check for the start , middle or end position of insertion . Insert the node and change its link accordingly. 5. For deletion get the position in which deletion is to be done. Delete the node and then link it to the next node. Before deletion check whether there is data in the list to be deleted. 6. Using display option list the elements of the list. 7. Stop the program. Program:

#include<iostream.h> #include<conio.h> #include<stdio.h> #define MAX 20 class LIST { private: int list[MAX]; public: int create(); void display(int); void reverse(int);

int search(int); void delet(int); }; int LIST::create() { int n,I; clrscr(); cout<<\n how many elements you want in the list; cin>>n; if(n>MAX) cout<<\n Error Number of elements exceeds the limit; for(i=0;i<n;i++) { cout<<\n Enter the element number<<i+1<<:; cin>>list[i]; } cout<<\n The List is successfully created\n; getch(); return(n); } void LIST::display(int n) { int I; clrscr(); cout<<\n the list is\n; for(i=0;i<n;i++) cout<<\n<<list[i]; cout<<\n press any key to continue\n; getch(); } void LIST::reverse(int n)

{ int I; clrscr(); cout<<\n the reversed list is \n; for(i=n-1;i>=0;i--) cout<<\n<<list[i]; cout<<\n press any key to continue\n; getch(); } int LIST::search(int n) { int I,key; clrscr(); cout<<\n enter the number you want to search?; cin>>key; for(i=0;i<n;i++) { if(list[i]==key) { cout<<\n given number is at position<<i<<\n; getch(); return I; } } cout<<\n the given number is at position:<<i<<\n; getch(); return I; }} cout<<\n given number is not in the list\n; getch(); return -1;

}} void LIST::delet(int n) { int I; i=search(n); list[i]=-1; cout<<\n the element is now deleted\n; cout<<\n we put -1 to indicate empty location; getch(); } void main() { LIST obj; int choice,len,position; char ans; do { clrscr(); cout<<\n program to perform operations on ordered list; cout<<\n 1.create; cout<<\n 2.display; cout<<\n 3.search for a number; cout<<\n 4.reverse; cout<<\n 5.delete; cout<<\n 6.Quit; cout<<\n enter your choice(1-6); cin>>choice; switch(choice) { case 1: len=obj.create();

break; case 2: obj.display(len); break; case 3: position=obj.search(len); break; case 4: obj.reverse(len); break; case 5: obj.delet(len); break; case 6: cout<<\n do you want to exit(y/n)?; ans=getche(); if(ans==y) exit(0); else break; default; clrscr(); cout<<\n invalid choice, try again; getch(); }} while(choice!-6) }

Output program to perform operations on ordered list 1.create 2.display

3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 1 how many elements you want in the list: 3 Enter the element number 1 :10 Enter the element number 1 :20 Enter the element number 1 :30

program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 2 the list is.. 10 20 30 press any key to continue program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.Quit

Enter your choice(1-6) 3 enter the number you want to search? 20 the given number is position 1 program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 4 the reversed list is . 30 20 10 press any key to continue program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 5 enter the number you want to search? 20 the given number is at position 1 the element is now deleted! we put -1 to indicate empty location program to perform operations on ordered list 1.create 2.display

3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 2 the list is 10 -1 30 press the key to continue program to perform operations on ordered list 1.create 2.display 3.search for a number 4.reverse 5.delete 6.Quit Enter your choice(1-6) 6 do you want to exit(y/n)?n

Result: Thus the given Program Cursor implementation of list was executed successfully.

Ex.No:5a

STACK ADT USING ARRAY IMPLEMENTATION

Aim: To write a program for stack using array implementation Algorithm: Step1: Define a array which stores stack elements. Step2: The operations on the stack are a) PUSH data into the stack b) POP data out of stack

Step3: PUSH DATA INTO STACK a) Enter the data to be inserted into stack b) If TOP is NULL the input data is the first node in stack the link of the node is NULL Top points to that node. c) If Top is NOT NULL the link of the TOP points to the new node. TOP points to that node Step4: POP DATA FROM STACK a) If TOP is NULL the stack is empty b) If TOP is NOT NULL the link of TOP is the current TOP the previous TOP is popped from stack Step5: The stack represented by linked list is traversed to display its connect Program: #include<iostrem.h> #include<conio.h> #include<stdlib.h> #define size 5 class stack_class {

private: struct stack { int s[size]; int top; }st; public: stack_class(); int stfull(); void push(int item); int stempty(); int pop(); void display(); }; stack_class::stack_class() { st.top=-1; for(int i=0;i<size;i++) st.s[i]=0; } int stack_class::stfull() { if(st.top>=size-1) return 1; else return 0; } void stack_class::push(int item) { st.top++; st.s[st.top]=item; }

int stack_class::stempty() { if(st.stop==-1) return 1; else return 0;

}
int stack_class::pop() { int item; item=st.s[st.stop]; st.top--; return(item); } void stack_class::display() { int i; if(stempty()) cout<<\n stack is empty!; else { for(i=st.top;i>0;i--) cout<<\n<<st.s[i]; } } void main(void) { int item,choice; char ans; stack class obj; clrscr();

cout<<\n\t\t implementation of stack; do { cout<<\n main menu; cout<<\n 1.push\n2.pop\n3.display\n4.exit; cout<<\n enter your choice; cin>>choice; switch(choice) { case 1: cout<<\n enter the item to be pushed; cin>>item; if(obj.stfull()) cout<<\n stack is full; else obj.push(item); break; case 2: if(obj.stempty()) cout<<\n empty stack! underflow!! else { item=obj.pop(); cout<<\n the poped element is <<item; } break; case 3: obj.display(); break; case 4: exit(0); } cout<<\n do you want to continue?;

ans=getche(); } while(ans==Y || ans==y) getch(); } Output: main menu 1.push 2.pop 3.display 4.exit enter your choice 1

enter the item to be pushed 20 do you want to continue? y main menu 1.push 2.pop 3.display 4.exit enter your choice 2 the popped element is 30

do you want to continue? y main menu 1.push 2.pop 3.display 4.exit enter your choice 3 20 10

do you want to continue? y main menu 1.push 2.pop 3.display 4.exit enter your choice 2 the popped element is 20

do you want to continue? y main menu 1.push 2.pop 3.display 4.exit enter your choice 2 the popped element is 10

do you want to continue? y main menu 1.push 2.pop 3.display 4.exit enter your choice 2 empty stack! Underflow!! do you want to continue n

Result: Thus the given program stack ADT using array implemented successfully.

Ex.No:5b Aim:

STACK ADT USING LINKED LIST IMPLEMENTING

To write a program for stack ADT using linked list implementation Algorithm: Step1: Define a struct for each node in the stack. Each node in the stack contains data and link to the next node. TOP pointer points to last node inserted in the stack. Step2: The operations on the stack are a) PUSH data into the stack b) POP data out of stack Step3: PUSH DATA INTO STACK a) Enter the data to be inserted into stack b) If TOP is NOT NULL the input data is the first node in the stack. the link of the node is NULL TOP points to that node. Step4: POP DATA FROM STACK a) If TOP is NULL the stack is empty b) If TOP is NOT NULL the link of TOP is the current TOP. the previous TOP is popped from stack. Step5: The stack represented by linked list is traversed to display its content. Program: #include<iostream.h> #include<conio.h> #include<stdlib.h> class lstack { private: typedef struct stack

{ int data; struct stack *next; }node; node *top; public: lstack(); -lstack(); void create(),remove(),show(); void push(int,node**); void display(node**); int pop(node**); int sempty(node**); }; lstack::lstack() { top=NULL; } lstack::-lstack() { node *temp; temp=top; if(temp==NULL) delete temp; else { while(temp!=NULL) { temp=temp->next; top=NULL; top=temp;

} delete temp; }} void lstack::create() { int data; cout<<\n enter the data; cin>>data; push(data,&top); } void lstack::remove() { int item; if(sempty(top)) cout<< stack underflow!; else { item=pop(&top); cout<<\n the popped node is<<item; }} void lstack::show() { display(&top); } void lstack::push(int item,node **top) { node *New; New=new node; New->next=NULL; New->data=item; New->next=*top;

*top=New; } int lstack::sempty(node *temp) { if(temp==NULL) return 1; else return 0; } int lstack::pop(node **top) { node *temp; item=(*top)->data; temp=*top; *top=(*top)->next; delete temp; return(item); } void lstack::display(node **head) { node *temp; temp=*head; is(sempty(temp)) cout<<\n the stack is empty!; else { while(temp!=NULL) { cout<<<<temp->data; temp=temp->next; }}

getch(); } void main() { int choice; char ans,ch; lstack st; clrscr(); cout<<\n\t\t stack using linked list; do { cout<<\n\n the main menu; cout<<\n1.push\n2.pop\n3.display\n4.exit; cout<<\n enter your choice; cin>>choice; switch(choice) { case 1: st.create(); break; case 2: st.remove(); break; case 3: st.show(); break; case 4: exit(0); } cout<<\n do you want to continue; ans=getche(); getch(); clrscr(); }

while(ans==Y || ans==y) getch(); } Output: the main menu 1.push 2.pop 3.display 4.exit enter your choice 1 enter the data 10

do you want to continue?y the main menu 1.push 2.pop 3.display 4.exit enter your choice 1 enter the data 20

do you want to continue?y the main menu 1.push 2.pop 3.display 4.exit enter your choice 1 enter the data 30 do you want to continue?y the main menu

1.push 2.pop 3.display 4.exit enter your choice 1 enter the data 40

do you want to continue?y the main menu 1.push 2.pop 3.display 4.exit enter your choice 3 40 30 20 10

do you want to continue?y the main menu 1.push 2.pop 3.display 4.exit enter your choice 2 the popped node 40

do you want to continue?y the main menu 1.push 2.pop 3.display 4.exit

enter your choice 4 Result: Thus the Given Program for stack ADT using linked list implementation was executed successfully.

Ex.No:6

PROGRAM SOURCE FILES FOR STACK APPLICATION1

STACK IMPLEMENTED AS ARRAYS(USING THE HEADER FILE OF STACK OPERATIONS) Algorithm: Step1: Create a header file named stack.h. in this file we will declare the class and all stack operations. the implementation of stack is using arrays.

stack.h #define size 10 class stk_class { private: struct stack { char s[size]; int top; }st; puplic: void push(char item); int stempty(); char pop(); }; stk_class::stk_class() {

st.top=-1; } void stk_class::push(char item) { st.top++; st.s[st.top]=item; } int stk_class::stempty() { inr(st.top==-1) return 1; else return 0; } int stk_class::stfull() { if(st.top==size) return 1; else return 0; } char stk_class::pop() { char item; item=st.s[st.top]; st.top--; return(item); }

Step2: Now we will create a stack application program. we have chosen an application as checking well form endness of parenthesis for this applications we will use stack

operations . These operations are used from the external file stack.h. Hence we will include this file in the include file section. Here is an application program.

#include<iostream.h> #include<conio.h> #include<stdlib.h> # include d:\stack.h #define size 10 void main(void) { char item; char ans,breaket[10]; stk_class obj; int I; clrscr(); cout<<\n\t\t program stack application using separate header file; cout<<\n Enter the expression and put $at end; cin>>bracket; i=0; if(bracket[i]==)) cout<<\n the expression is invalid; else { do { while(bracket[i]==() { obj.[ush(bracket[i]); i++; } while(bracket[i]==))

{ item=obj.pop(); i++; } } while(bracket[i]!=$) if(!obj.stempty()) cout<<\n The expression is invalid; else cout<<\n The expression has well formed parathesis; } getch(); }

Step3: Execute above program and following output can be obtained. output(run 1) Program for stack application using separate header file Enter the expression and put $ at end (()()$ The expression has well formed parenthesis Output(run 2) Program for stack application using separate header file Enter the expression and put $ at the end (()()$

The expression is invalid

Result: Thus the given program checking well formedness of parenthesis stack implemented as arrays was executed successfully.

B) STACK IMPLEMENTED AS LINKED LIST USING THE HEADER FILE OF STACK OPERATION

Step1: Create a header file named stack. In this file we declare the class and the stack operations . The implementation of stack using linked list.

class stk_class { private: typedef struct stack { char data; struct stack *next; }node; node *top; public: stk_class(); void push(char item); int sempty(); void pop(); }; stk_class::stk_class() { top=NULL; } void stk_class::push(char item) { node *New; New=new node; if(New==NULL) cout<<\n memory cannot be allocated\n; else {

New->data=item; new->next=top; top=New; }} int stk_class::sempty() { if(top==NULL) return 1; else return 0; } void stk_class::pop() { node *temp; temp=top; top=top->next; delete temp; }

Step2: Now we will create a stack application program. We have chosen an application as checking well formedness of parenthesis for this application we will use stack operations. These operations are used from the external file stack.h. Hence we will include this file in the include file section. Here is an application program.

#include<iostream.h> #include<conio.h> #include<process.h> #include<stdlib.h> #included:\stack.h void main()

{ char ans,bracket[10]; char data,item; stk_class obj; int choice; int I; clrscr(); cout<<\n\t\t Enter the expression and put $ at the end; cin>>bracket; i=0; if(bracket[i]==)) cout<<\n the Expression is invalid; else { do { if(breaket[i]==() { obj.push(bracket[i]); } else if(breacket[i]==)) { if(obj.sempty()) { cout<<\n the expression is invalid ; getch(); exit(0); } obj.pop(); } i++;

} while(breacket[i]!=$); if(obj.sempty()) cout<<\n the expression has well formed paranthesis; } getch(); } Step3: The above program will be executed to get output as follows

Output(run1) Enter the expression and put $ at the end ()()$ the expression has well formed parenthesis Output(run2) Enter the expression and put $ at the end (()()$ the expression is invalid. Result: Thus the given program checking well formedness of parentesis stack implemented at Linked List was executed successfully.

Ex.No:6

PROGRAM SOURCE FILES FOR STACK APPLICATION2

application 2: Evaluation of postfix expression. A.USING STACK( IMPLEMENTATION AS ARRAY) STEP1: #define Max 10; class stk_class { struct stack { double s[MAX];

int top; }st; public: stk_class(); void push(double val); double pop(); }; stk_class::stk_class() { st.top=0; } void stk_class::push(double val) { if(st.top+1>=MAX) cout<<\n stack is full; st.top++; st.s[st.top]=val; } double stk_class::pop() { double val; if(st.top==-1) cout<<\n stack is empty\n; st.top--; return(val);l }

Step2: #include<iostream.h> #include<conio.h> #include<stdlib.h>

#include<string.h> #include<math.h> #define size 80; void main() { char exp[size]; int len; double result; double post(char exp[]) clrscr(); cout<<enter the postfix expression\n; cin>>exp; len=strlen(exp); exp[len]=$; result=post(exp); cout<<the value of the expression is<<result; getch(); exit(0); } double post(char exp[]) { stk_class obj; char ch,*type; double result,val,op1,op2; int i; i=0; ch=exp[i]; while(ch!=$) { if(ch>=0 && ch<=9) type=operand;

else if(ch==+ || ch==- || ch==^) type=operator; if(strcmp(type,operand)==0) { val=ch-48; obj.push(val); } else if(strcmp(type,operator)==0) { op2=obj.pop(); op1=obj.pop(); switch(ch) { case +: result=op1+op2; break; case -: result=op1-op2; break; case *: result=op1*op2; break; case /: result=op1/op2; break; case ^: result=pow(op1,op2); break; } obj.push(result); } i++; ch=exp[i]; } result-obj.pop();

return(result); } Output: Enter the postfix expression 12+3* The value of the expression is 9 Result: Thus the given program Evaluation of postfix expression stack implemented as arrays was executed successfully.

B. STACK IMPLEMENTED AS LINKED LIST (USE OF SEPARATE HEADER FILE FOR STACK OPERATIONS)

STEP1: class stk_class { typedef struct stack { char data; struct stack *next; }node; public: node *top; stk_class(); void push(char item); char pop; }; stk_class::stk_class() { top=NULL; } Void stk_class::push(char item)

{ node *New; New=new node; New->data=item; New->next=top; top=New; } Char stk_class::pop() { char item; node *temp; item=top->data; temp=top; delete temp; return item; }

STEP2:

#include<iostream.h> #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<conio.h> #included:stack.h #define size 80 Void main() { char exp[size]; int len;

double result; double post(char exp[]) clrscr(); cout<<enter the postfix expression\n; cin>>exp; len=strlen(exp); cin>>exp; len=strlen(exp); exp[len]=$; result=post(exp); cout<<the value of the expression is<<result; getch(); exit(0); } double post(char exp[]) { char ch,*type; double result,val,op1,op2; int l; stk_class obj; i=0; ch=exp[i]; while(ch!=$) { if(ch>=0 && ch<=9) type=operand; else if(ch==+ || ch==- || ch==/ || ch==^) type=operator; if(strcmp(type,operator)==0) { op2=obj.pop();

op1=obj.pop(); switch(ch) { case +: result=op1+op2; break; case -: result=op1-op2; break; case *: result=op1*op2; break; case /: result=op1/op2; break; case ^: result=pow(op1,op2); break; } obj.push(result); } i++; ch=exp[i]; } result-obj.pop(); return(result); } output: enter the expression 12+3* the value of the expression is 9

Result: Thus the given program Evaluation of postfix expression stack implemented as Linked List was executed successfully.

Ex.No:7a Aim:

QUEUE ADT USING ARRAY IMPLEMENTATION

To write a program for Queue using array implementation. Algorithm: Step1: Define a array which stores queue elements. Step2: The operations on the queue are a) INSERT data into the queue b) DELETE data out of queue Step3: INSERT DATA INTO queue a) Enter the data to be inserted into queue. b) If TOP is NULL the input data is the first node in queue. the link of the node is NULL. TOP points to that node. c) If TOP is NOT NULL the link of TOP points to the new node. TOP points to that node. Step4: DELETE DATA FROM queue a) If TOP is NULL the queue is empty b) If TOP is NOT NULL the link of TOP is the current TOP. the previous TOP is popped from queue. Step5: The queue represented by linked list is traversed to display.

Program: #include<iostream.h> #include<stdio.h> #include<conio.h> #define size 5 class myq

{ private: struct queue { int que[size]; int front,rear; }q; public: myq(); int qfull(); int insert(int); int qempty(); int delet(); void display(); }; myq::myq() { q.front=-1; q.rear=-1; } int myq::qfull() { if(q.rear>=size-1) return 1; else return 0; } int myq::insert(int item) { if(q.front==-1) q.front++;

q.que[++q.rear]=item; return q.rear; } int myq::qempty() { if(q.front==-1 || (q.front>q.rear)) return 1; else return 0; } int myq::delet() { int item; item=q.que[q.front]; q.front++; cout<<\nthe deleted item is<<item; return q.front; } void myq::display() { int l; for(i=q.front;i<=q.rear;i++) cout<<<<q.que[i]; } void main(void) { int choice,item; char ans; myq obj; clrscr(); do

{ cout<<\n main menu; cout<<\n 1.insert\n 2.delete\n3.display; cout<<\n enter your choice; cin>>choice; switch(choice) { case 1: if(obj.qfull()) cout<<\n cannot insert the element; else { cout<<\n enter the number to be inserted; cin>>item; obj.insert(item); } break; case 2: if(obj.qempty()) cout<<\n queue underflow; else obj.delet(); break; case 3: if(obj.qempty()) cout<<\n queue is empty!; else obj.display(); break; default: cout<<\n do you want to continue;

ans=getche(); } while(ans==y || ans==Y) } Output:

Result: Thus the given Program for Queue using implementation was executed successfully. Ex.No:7b Aim: To write a C++ program for Queue using Linked implementation Algorithm: Step1: Define a struct for each node in the queue. Each node in the queue contains data and link to the next node. Front and rear pointer points to first and last. node inserted in the queue. Step2: The operations on the queue are a) INSERT data into the queue b) DELETE data out of queue Step3: INSERT DATA INTO queue a) Enter the data to be inserted into queue. b) If TOP is NULL the input data is the first node in queue the link of the node is NULL. TOP points to that node. c) If TOP is NOT NULL. the link of TOP points to the new node. TOP points to that node. Step4: DELETE DATA FROM queue a) If TOP is NULL. the queue is empty. QUEUE ADT OPERATIONS USING LINKED LIST

b) If TOP is NOT NULL. the link of TOP is the current TOP. the previous TOP is popped from queue. Step5: The queue represented by linked list is traversed to display its content. Program: #include<iostream.h> #include<stdio.h> #include<conio.h> class lqueue { private: tydef struct node { int data; struct node *next; }Q; Q *front,*rear; public: lqueue(); ~lqueue(); void create(),remove(),show(); void insert(); Q delet(); void display(Q*); }; lqueue::lqueue() { lqueue::lqueue() { front=NULL; rear=NULL;

} void lqueue::create() { insert(); } void lqueue::remove() { front=delet(); } void lqueue::show() { display(front); } void lqueue::insert() { char ch; clrscr(); temp=new Q; temp->next=NULL; cout<<\n\n\n insert the element in the queue\n; cin>>temp->data; if(front==NULL) { front=temp; rear=temp; } else { rear->next=temp; rear=rear->next; }

} int qempty(Q *front) { if(front==NULL) return 1; else return 0; } Q *lqueue::delet() { Q *temp; temp=front; if(Qempty(front)) { cout<<\n\n\t\tsorry!the queue is empty\n; cout<<\n can not delete the element ; } else { cout<<\n \t the deleted element is<<temp->data; front=-front->next; temp->next=NULL; delete temp; } return front } void lqueue::display(Q *front) { if(Qempty(front)) cout<<\n the queue is empty\n; else

{ cout<<\n\t the display of queue is\n; for(;front!=rear->next,front=front->next) cout<<<<front->data; } getch(); } lqueue::lqueue() { if((front!=NULL) && (rear!=NULL)) { front=NULL; rear=NULL; delete front; delete rear; } } void main(void) { char ans; int choice; lqueue que; do { clrscr(); cout<<\n program for queue using linked list\n; cout<<\n main menu; cout<<\n Enter your choice; cin>>choice; switch(choice) {

case 1: que.create(); break; case 2: que.remove(); break; case 3: que.show(); break; default: cout<<\n you have entered wrong choice<<endl; break; } cout<<\n do you want main menu?(y/n)<<endl; ans=getch(); } while(ans==y || ans==Y); getch(); } Output: program for queue using linked list main menu 1.insert 2.delete 3.display enter your choice 1 insert the element in the queue 10 do you want main menu?(y/n) y

program for queue using linked list

main menu 1.insert 2.delete 3.display enter your choice 1 insert the element in the queue 20 do you want main menu?(y/n) y

program for queue using linked list main menu 1.insert 2.delete 3.display enter your choice 1 insert the element in the queue 30 do you want main menu?(y/n) y

program for queue using linked list main menu 1.insert 2.delete 3.display enter your choice 1 insert the element in the queue 40 do you want main menu?(y/n) y

program for queue using linked list main menu

1.insert 2.delete 3.display enter your choice 3 the display of queue is 10 20 30 40 do you want main menu?(y/n) y

program for queue using linked list main menu 1.insert 2.delete 3.display enter your choice 2 the deleted element is 10 do you want main menu?(y/n) y

program for queue using linked list main menu 1.insert 2.delete 3.display enter your choice 2 the deleted element is 20 do you want main menu?(y/n) y

program for queue using linked list main menu 1.insert 2.delete 3.display

enter your choice 3 the display of queue is 30 40 do you want main menu?(y/n) n

Result: Thus the given program Queue ADT operations using Linked List was executed successfully.

Ex.No:8 Aim:

BINARY SEARCH TREE

You might also like