0% found this document useful (0 votes)
7 views13 pages

Data Structure Programs

The document details various implementations of data structures including Array, Linked List, Stack, and Queue ADTs in C++. It provides code snippets for creating, inserting, deleting, searching, and displaying elements in these data structures. Additionally, it includes examples of applications such as balancing parentheses using stacks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Data Structure Programs

The document details various implementations of data structures including Array, Linked List, Stack, and Queue ADTs in C++. It provides code snippets for creating, inserting, deleting, searching, and displaying elements in these data structures. Additionally, it includes examples of applications such as balancing parentheses using stacks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Jobin S Thomas/AP/ECE

RGCE, Sriperumbudur
Program II Array Implementation of List ADT {
#include<iostream.h> if (b[i]==d)
#include<conio.h> {
#include<process.h> b[i]=0; }
classarraylist cout<<"number deleted"<<endl; }
{ }
inta,b[20],n,d,e,f,i; voidarraylist:: search()
public: {
void create(); cout<<"enter the number"<<endl;
void insert(); cin>>e;
void deletion(); for (i=0;i<n;i++)
void search(); {
void display(); if(b[i]==e); }
}; cout<<"value found the position"<<b[i]<<endl;
voidarraylist::display() }
{ void main()
for(i=0;i<n;i++) { {
cout<<"\n\n\n"<<b[i]; int c;
}} char g='y';
voidarraylist::create() clrscr();
{ arraylist a;
cout<<"how many number you want to enter"<<endl; do
cin>>n; {
cout<<"enter the number"<<endl; cout<<"\n main menu";
for(i=0;i<n;i++) cout<<"\n 1.create \n 2.delete \n 3.search \n 4.insert \n
{ 5.display \n 6. exit"<<endl;
cin>>b[i]; cout<<"enter your choice"<<endl;
}} cin>>c;
voidarraylist:: insert() switch(c)
{ {
cout<<" enter how many number you want to case 1:a.create();break;
insert"<<endl; case 2:a.deletion();break;
cin>>f; case 3:a.search();break;
cout<<"insert the number"<<endl; case 4:a.insert();break;
for(i=0;i<f;i++) case 5:a.display();break;
{ case 6:exit(0);break;
cin>>b[n++];} default:
} cout<<"the given is not between 1-5 \n";
voidarraylist::deletion() }
{ cout<<"\n do you want to continue \n";
cout<<"Enter the number you want to delete"<<endl; cin>>g;
cin>>d; clrscr();
for(i=0;i<n;i++) }
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
while(g=='y'||g=='y'); intch;
getch(); clrscr();
} cout<<"LINKED LIST"<<endl;
cout<<"\n 1.Add \n 2.Delete \n 3. display\n
Program III Linked List Implementation of List 4.exit"<<endl;
ADT do
#include<iostream.h> {
#include<conio.h> cout<<" \n Enter the choice"<<endl;
#include<process.h> cin>>ch;
#include<malloc.h> switch(ch)
typedefstruct list //structure for creating node {
{ case 1:
int data; //data field add ();
struct list*next; //link field break;
}node; case 2: del();
node*newnode,*first,*temp; break;
void add () case 3:display();
{ break;
int d; case 4:exit(0);
newnode=(node*)malloc(sizeof (node)); //dynamic break;
memory allocation for new node }}
cout<<"Enter the value of be added"<<endl; while(ch!=4);
cin>>d; getch();
newnode->data=d; }
newnode->next=first; Program IV Cursor Implementation of List ADT
first=newnode; #include<iostream.h>
} #include<conio.h>
void del() #include<stdlib.h>
{ # define max 20
temp=first; class LIST
cout<<"Deleted item is:"<<temp->data<<endl; {
first=temp->next; private:
} int List[max];
void display() public:
{ int create();
temp=first; void display(int);
cout<<"The standard data are:"<<endl; void reverse(int);
while(temp!=NULL) int search(int);
{ voiddelet(int);
cout<<temp->data<<endl; };
temp=temp->next; int LIST::create()
}} {
void main() intn,i;
{ clrscr();
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
cout<<"\n How many element you want in the cout<<"\n The given number is at
list:"<<endl; position:"<<i<<endl;
cin>>n; getch();
if(n>max) returni;
cout<<"\n error: number exceeds the limit:"<<endl; }}
for(i=0;i<n;i++) cout<<"\n The given is number not in the list"<<endl;
{ cout<<"\n Enter the element number:"<<i+1; getch();
cin>>List[i]; return -1;
} }
cout<<"\n the list is suceesfully created \n"<<endl; void LIST::delet(int n)
getch(); {
return(n); inti;
} i=search(n);
void LIST:: display(int n) List[i]=-1;
{ cout<<" \n The element is now deleted"<<endl;
inti; cout<<"-1 indicates empty location"<<endl;
clrscr(); getch();
cout<<"\n The list is....\n"<<endl; }
for(i=0;i<n;i++) void main()
cout<<"\n"<<List[i]; {
cout<<"\n Press any key to continue \n"<<endl; LIST obj;
getch(); intchoice,len,position;
} charans;
void LIST::reverse(int n) do
{ {
inti; clrscr();
clrscr(); cout<<"\n List of a opertations"<<endl;
cout<<"The reversed list....\n"<<endl; cout<<" \n 1.create\n2.display\n 3.search for a
for(i=n-1;i>=0;i--) number\n 4.reverse\n 5.delete\n 6.quit"<<endl;
cout<<"\n"<<List[i]; cout<<"\n Enter your choice;"<<endl;
cout<<"\n Press any key to continue...\n"<<endl; cin>>choice;
getch(); switch(choice)
} {
int LIST::search(int n) case 1:len=obj.create();
{ break;
inti,key; case 2:obj.display(len);
clrscr(); break;
cout<<"Enter the number you want to case 3:position=obj.search(len);
search?"<<endl; break;
cin>>key; case 4:obj.reverse(len);
for(i=0;i<n;i++) break;
{ case 5:obj.delet(len);
if(List[i]==key) break;
{ case 6:
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
cout<<" Do you want to exit(y/n)?"; intstack_class::stfull()
ans=getch(); {
if(ans=='y') if(node.top>=size-1)
exit(0); return 1;
else else
break; return 0;
default: }
clrscr(); voidstack_class::push(int item)
cout<<"\n Invalid choice,try again"; {
getch(); node.top++;
}} node.a[node.top]=item;
while(choice!=6); }
} intstack_class::empty()
{
Program Va Stack ADT-Array Implementation if (node.top==-1)
#include<iostream.h> return 1;
#include<conio.h> else
#include<stdio.h> return 0;
#include<process.h> }
#define size 5 intstack_class::pop()
classstack_class {
{ int item;
private: item=node.a[node.top];
struct stack node.top--;
{ return(item);
int a[size]; }
int top; voidstack_class::display()
}node; {
inti;
public: if(empty())
stack_class(); cout<<"\n stack is empty"<<endl;
intstfull(); else
void push(int item); {
int empty(); for(i=node.top;i>=0;i--)
int pop(); cout<<"\n"<<node.a[i];
void display(); }
}; }
voidstack_class::stack_class() //constructor for void main()
initialization {
{ intitem,choice;
node.top=-1; charans;
for(inti=0;i<=size;i++) stack_classobj;
node.a[i]=-1; clrscr();
}
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
do {
{ int data;
cout<<"\n MAIN MENU"<<endl; snode *next;
cout<<"\n1.push \n2.pop \n3.display \n4.exit"<<endl; }*top=0; //structure variable top
cout<<"\n enter the choice"<<endl; void push()
cin>>choice; {
switch(choice) snode *newnode;
{ int d;
case 1: cout<<"\n Enter the data";
cout<<" enter the item to be pushed"<<endl; cin>>d;
cin>>item; newnode=((snode*)malloc(sizeof (snode)));
if(obj.stfull()) newnode->data=d;
cout<<"\n stack is full"; newnode->next=0;
else if(top==0)
obj.push(item); top=newnode;
break; else
case 2: {
if(obj.empty()) newnode->next=top;
cout<<"\n empty stack under flow"; top=newnode;
else }}
{ void pop()
item=obj.pop(); {
cout<<"\n the popped element is:"<<item<<endl; if(top==0)
} cout<<"\n stack is empty";
break; else
case 3: {
obj.display(); top->data=0;
break; top=top->next;
case 4: cout<<"\n top element deleted";
exit(0); }
} getch();
cout<<"\n do you want to continue"<<endl; }
ans=getch(); void display()
} {
while(ans=='y'||ans=='Y'); structsnode *t;
getch(); if(top==0)
} cout<<"\n stack is empty";
Program Vb Stack ADT- Linked List else
Implementation {
#include<conio.h> for(t=top;t!=0;t=t->next)
#include<iostream.h> cout<<"->"<<t->data;
#include<malloc.h> }
#include<process.h> getch();
structsnode //structure for creating node
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
} intqueuefull();
void main() };
{ int queue::queuefull()
intch,d; {
clrscr(); if (node. rear >=size-1)
cout<<"\n1.push\n2.pop\n3.display\n4.exit"; return 1;
do else
{ return 0;
cout<<"\nEnetr your choice"; }
cin>>ch; void queue::enqueue(int item)
switch(ch) {
{ node. rear++;
case 1:push(); node.a[node.rear]=item;
break; }
case 2:pop(); int queue::empty()
break; {
case 3:display(); if(node.front>node. rear)
break; return 1;
default:exit(0); break; else
}} return 0;
while(ch<4); }
getch(); int queue::dequeue()
} {
Program VI a Queue ADT- Array Implementation int item;
#include<iostream.h> item=node.a[node.front];
#include<conio.h> node.front++;
#include<process.h> return(item);
#include<malloc.h> }
#define size 5 void queue::display()
class queue {
{ inti;
private: if(empty())
struct que cout<<"\n queue is empty";
{ else
int a[size]; {
intrear,front; for(i=node.front;i<=node.rear;i++)
}node; cout<<"\n "<<node.a[i];
public: }}
queue(); void queue::queue()
voidenqueue(int); {
intdequeue(); node.front=0;
void display(); node.rear=-1;
int empty(); for(inti=0;i<=size;i++)
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
node.a[i]=-1; getch();
} }
void main()
{ Program VI b Queue ADT- Linked List
intitem,choice; Implementation
charans; #include<iostream.h>
queueobj; #include<conio.h>
clrscr(); #include<stdio.h>
do #include<malloc.h>
{ #include<process.h>
cout<<"\nMAIN MENU"; structqnode
cout<<"\n1.enqueue\n2.dequeue\n3.display\n4.exit"; {
cout<<"\n enter your choice"; int data;
cin>>choice; qnode *next;
switch(choice) }*rear=NULL,*front=NULL;
{ voidenqueue()
case 1: {
cout<<"\n Enter the element"; qnode *newnode;
cin>>item; int d;
if(obj.queuefull()) cout<<"enter the data\n";
cout<<"\n queue is full"; cin>>d;
else newnode=(qnode*)malloc(sizeof (qnode));
obj.enqueue(item); newnode->data=d;
break; newnode->next=0;
case 2: if(front==NULL)
if (obj.empty()) {
cout<<"\n empty queue under flow"; front=newnode;
else rear=newnode;
{ cout<<"first element inserted \n";
item=obj.dequeue(); }
cout<<"\n the deleted element is :"<<item; else
} {
break; rear->next=newnode;
case 3: rear=newnode;
obj.display(); cout<<"first element inserted \n";
break; }
case 4: getch();
exit(0); }
} voiddequeue()
cout<<"\n do you want to continue"; {
ans=getch(); if(rear ==NULL)
} cout<<"queue is empty \n";
while(ans=='y'||ans=='Y'); else
{
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
front->data=0; getch();
front=front->next; }
cout<<"top element deleted \n"; Program VII Stack Application- Array
} Implementation
getch(); Balancing Parathesis- Using Files
} /*Crate a header file named stack1.h
void display() #include<iostream.h>
{if(front==NULL) #include<conio.h>
cout<<"stack is empty\n"; #include<process.h>
else #include<malloc.h>
{ #define size 5
qnode*temp; class stack
temp=front; {private:
while(temp!=NULL) structstak
{ {int a[size];
cout<<"->"<<temp->data; int top;
temp=temp->next; }node;
}} public:
getch(); stack();
} void push(int);
void main() int pop();
{ void display();
intch,d; int empty();
clrscr(); intstackfull()
cout<<" \n 1.enqueue \n 2.dequeue \n 3.display \n {if(node.top>=size-1)
4.exit \n"; return 1;
do else
{cout<<"enter your choice\n"; return 0;
cin>>ch; } };
switch(ch) void stack:: push(int item)
{ {
case 1: node.top++;
enqueue(); node.a[node.top]=item;
break; }
case 2: int stack:: empty()
dequeue(); {if(node.top==-1)
break; return 1;
case 3: else
display(); return 0;
break; }
default:exit(0);
break; int stack:: pop()
}} {
while(ch<4); int item;
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
item=node.a[node.top]; obj.pop();
node.top--; i++;
return(item); }
} }
while(bracket[i]!='$');
void stack::stack() if(!obj.empty())
{ cout<<"\n the expression is invalid";
node.top=-1; else
for (inti=0;i<=size;i++) cout<<"\n the expression has well formed
node.a[i]=-1; parenthesis";
} }
getch();
//stack application program. }
#include"D:\TC\stack1.h" Program VIII Stack Application- Linked List
Implementation
void main()
Balancing Parathesis- Using Files
{
//create a header file named stack2.h
char item;
#include<iostream.h>
charans,bracket[10];
#include<stdio.h>
stackobj;
#include<conio.h>
inti;
#include<malloc.h>
clrscr();
#include<process.h>
cout<<"\n\t Program for Stack Application Using
Separate Header File\n"; structsnode // create structure
cout<<"\t {
----------------------------------------------------------\n"; int data; //to store data
cout<<"\n Enter the expression and put $ at end "; snode *next; //pointer to next element
cin>>bracket; }*top=0; //top(header) initialised to zero
i=0;
if(bracket[i]==')') void push(int d) // function to insert elements
cout<<"\n the expressin is invalid"; {
else snode *newnode; // creating new node
{ newnode=((snode*)malloc(sizeof(snode))); //
do memory space for new node
{ newnode->data=d; // assigning
element
while(bracket[i]=='(')
newnode->next=0;
{
if(top==0) // if top is zero stack is empty
if(obj.stackfull())
top=newnode; // assigning newnode as top
cout<<"stack is full";
else // if it is not empty
else
{
obj.push(bracket[i]);
newnode->next=top; // value of top assingning as
i++;
the pointer of newnode
}
top=newnode; // now newnode is in top
while(bracket[i]==')') position.
{ }}
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
void pop() if(top==0)
{ {cout<<"\n the expression has well formed
if(top==0) // if top is zero stack is empty parenthesis" ;}
{ else
cout<<"stack is empty"; cout<<"expression is not balanced";
} getch();
else }}
{
top->data=0; //data of first element is zero Program IX Search Tree ADT- Binary Search
Tree
top=top->next; // top= second element ie; top-
>next #include<iostream.h>
} #include<conio.h>
getch(); #include<process.h>
} #include<malloc.h>
//stack application program. classbintree //class declaration
#include"d:\tc\stack2.h" { // creating binary tree
void main(void) typedefstructbst
{ { // data of BT
charans,bracket[10]; int data;
char data ,item; structbst *left,*right; //left and right pointer of BT
int choice; }
inti; node; //node is a variable of type BST
clrscr(); node *root,*New,*temp,*parent;
//rootnode,newnode,temporarynode,parentnode
cout<<"\t\t enter the expression and put $ at the end \
n"; public:
cin>>bracket; bintree() //constructor-automatically calls
when object created
i=0;
{
if(bracket[i]==')')
root=NULL;
{
}
cout<<"\n the expression is invalid";
void create();
getch();
void display();
}
voiddelet();
else
void find();
{
void insert(node *,node*);
do
voidinorder(node *);
{
void search(node**,int,node **);
if(bracket[i]=='(')
void del(node *,int);
{push(bracket[i]);}
};
else if(bracket[i]==')')
{ pop();
voidbintree::create() //creating a BST
}
{
i++;
New=(bst*)malloc(sizeof(bst)); //dynamic m/y
}
allocation for newnode
while(bracket[i]!='$');
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
New->left=NULL; //initializing left child as {
null inorder(temp->left);
New->right=NULL; cout<<"\t"<<temp->data;
cout<<"\n enter the element"; inorder(temp->right); //recursively calling
cin>>New->data; //entering new data }}
if(root==NULL) voidbintree::find()
root=New; //if tree is null first data will {
be root
int key;
else
cout<<"\n enter the element which you want to
insert(root,New); //calling insert function search";
} cin>>key;
temp=root;
voidbintree::insert(node *root,node *New) search(&temp,key,&parent); //calling search
//definition of insert function function and passing 3 parameters
{ if(temp==NULL)
if(New->data<root->data) //if data is smaller than cout<<"\n element is not present";
root
else
{
cout<<"\n parent of node"<<temp->data<<"
if(root->left==NULL) is:"<<parent->data;
root->left=New; }
else
insert(root->left,New); //recursion voidbintree::search(node **temp,intkey,node
} **parent)
if(New->data>root->data) //if data is larger than root {
{ if(*temp==NULL)
if(root->right==NULL) cout<<endl<<" tree is not created"<<endl;
root->right=New; else
else {
insert(root->right,New); //recursion while(*temp!=NULL)
}} {
if((*temp)->data==key)
voidbintree::display() {
{ cout<<"\n the "<<(*temp)->data<<" element is
if(root==NULL) present";
cout<<"tree is not created"; break;
else }
{ *parent=*temp; //stores the parent value
cout<<"\n the tree is: "; if((*temp)->data>key)
inorder(root); //calling inorder function *temp=(*temp)->left;
}} else
voidbintree::inorder(node *temp) //definition of *temp=(*temp)->right;
inorder function }}
{ return;
if(temp!=NULL) }
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
temp=NULL;
voidbintree::delet() delete temp;
{ cout<<" now deleted it!";
int key; return;
cout<<"\n enter the element you want to delete"; }
cin>>key; if(temp->left==NULL&&temp->right!=NULL)
if(key==root->data) {
{ if(parent->left==temp)
bintree(); // assigning a value NULL to root parent->left=temp->right;
} else
else parent->right=temp->right;
del(root,key); temp=NULL;
} delete temp;
cout<<" now deleted it!";
voidbintree::del(node *root,int key) return;}
{ /*deleting a node which is having no child*/
node *temp_succ; if(temp->left==NULL&&temp->right==NULL)
if(root==NULL) {
cout<<" tree is not created"; if(parent->left==temp)
else parent->left=NULL;
{ else
temp=root; parent->right=NULL;
search(&temp,key,&parent); cout<<" now deleted it!";
if(temp->left!=NULL&&temp->right!=NULL) return;
{ }} }
parent=temp; void main()
temp_succ=temp_succ->left; {
while(temp_succ->left!=NULL) int choice;
{ charans='N';
parent=temp_succ; bintree tree;
temp_succ=temp_succ->left; clrscr();
} cout<<"\n\t program for binary search tree";
temp->data=temp_succ->data; cout<<"\n1.create\n2.search\n3.delete\n4.display\
temp->right=NULL; n5.exit";
cout<<" now deleted it!"; do
return; {
} cout<<"\n\n enter your choice:";
if(temp->left!=NULL&&temp->right==NULL) cin>>choice;
{ switch(choice)
if(parent->left==temp) {
parent->left=temp->left; case 1:
else do
parent->right=temp->left; {
tree.create();
Jobin S Thomas/AP/ECE
RGCE, Sriperumbudur
cout<<"do you want to enter more elements: }
(y/n)"<<endl; getch( );
ans=getch(); return 0;
} }
while(ans=='y'); void swap(long &element1,long &element2)
break; {
case 2:tree.find(); long temp=element1;
break; element1=element2;
case 3: element2=temp;
tree.delet(); }
break; voidquick_sort(long array[],intfirst,int last)
case 4: {
tree.display(); if(first>=last)
break; {
case 5: }
exit(0); else
}} {
while(choice!=5); int middle=array[last];
} inti=first-1;
int j=last;
Program X Quick Sort while(i<j)
# include <iostream.h> {
# include <conio.h> do
void swap(long &,long &); {
voidquick_sort(long [],int,int); i++;
main( ) }
{ while(array[i]<middle);
clrscr( ); do
constint size=10; {
long array[size]={0}; j--;
cout<<" \n\n***** Quick Sort ******"<<endl; }
cout<<"\n * Array size = 10"<<endl; while(j>=0 && array[j]>middle);
cout<<" * Data Type = long"<<endl; if(i<j)
cout<<" Enter the array : "<<endl<<endl; swap(array[i],array[j]);
for(inti=0;i<size;i++) }
{
cout<<"\t Element["<<i<<"] = "; swap(array[i],array[last]);
cin>>array[i]; quick_sort(array,first,i-1);
} quick_sort(array,i+1,last);
quick_sort(array,0,size-1); }}
cout<<" \n\nSortedArray : \n\n";
for(int j=0;j<size;j++)
{
cout<<"\tElement["<<j<<"] = "<<array[j]<<endl;

You might also like