Sie sind auf Seite 1von 8

#include<iostream>

using namespace std;


void traversing (int*,int);
void searching (int*,int,int);
void insertion (int*,int,int,int);
int deletion (int*,int,int);
int main()
{
int array[30],choice,location,item,i,length,position;
cout<<"\nEnter length of array : ";
cin>>length;
cout<<"Enter element of array \n";
for(i=0;i<=length-1;i++)
{ cin>>array[i];
}
again:
cout<<"Travers= 1\nSearch = 2\nInsert = 3\nDelete = 4\nExit
cout<<"Enter choice: ";
cin>>choice;
switch(choice)
{ case 1:
traversing(array,length);
break;
case 2:
searching(array,item,length);
break;
case 3:
cout<<"Enter Item and Position to add \n";
cin>>item>>position;
insertion(array,length,item,position);
for(i=0;i<=length;i++)
{cout<<array[i]<<endl;
}
break;
case 4:
cout<<"Enter position to delete \n";
cin>>position;
length =deletion(array,length,position);
cout<<"New array after delete \n";
for(i=0;i<length;i++)
{cout<<array[i]<<"\n";}
break;
case 5:
break;
default:
cout<<"wrong value\n";
goto again;
}
return 0;
}
void traversing(int array[],int length)
{ for(int i=0;i<=length-1;i++)
{cout<<array[i]<<" ";}
}
void searching(int array[],int item,int length)
{ int i;
cout<<"\nEnter item to search \n";
cin>>item;
int location =-1;
for( i=0;i<=length-1;i++)
{ if ( array[i]==item)
{ location=i;
}

= 5 \n";

}
if (location==-1)
{cout<<"\nItem not found ";
}
else{cout<<" \nItem is present at (index value): "<<location;
}
}
void insertion(int array[],int length,int item,int position)
{ int j=length-1;
while(j>=position)
{ array[j+1]=array[j];
j=j-1;
}
array[position]=item;
length=length+1;
}
int deletion(int array[],int length,int position)
{
while(position<length)
{ array[position]=array[position+1];
position=position+1;
}
length=length-1;
return length;
}
/* Write C++ programs to implement the Stack ADT using an array */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class stack
{
int stk[5];
int top;
public:
stack()
{
top=-1;
}
void push(int x)
{
if(top > 4)
{
cout <<"stack over flow";
return;
}
stk[++top]=x;
cout <<"inserted" <<x;
}
void pop()
{
if(top <0)
{
cout <<"stack under flow";

return;
}
cout <<"deleted" <<stk[top--];
}
void display()
{
if(top<0)
{
cout <<" stack empty";
return;
}
for(int i=top;i>=0;i--)
cout <<stk[i] <<" ";
}
};
main()
{
int ch;
stack st;
while(1)
{
cout <<"\n1.push 2.pop 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1: cout <<"enter the element";
cin >> ch;
st.push(ch);
break;
case 2: st.pop(); break;
case 3: st.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUTS
1.push 2.pop 3.display 4.exit
Enter ur choice2
stack under flow
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element2
inserted2
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element3
inserted3
1.push 2.pop 3.display 4.exit
Enter ur choice2
deleted3
1.push 2.pop 3.display 4.exit
Enter ur choice1
enter the element5
inserted5

1.push 2.pop 3.display 4.exit


Enter ur choice3
5 2
1.push 2.pop 3.display 4.exit
Enter ur choice4
/* Write C++ programs to implement the Queue ADT using an array */
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class queue
{
int queue1[5];
int rear,front;
public:
queue()
{
rear=-1;
front=-1;
}
void insert(int x)
{
if(rear > 4)
{
cout <<"queue over flow";
front=rear=-1;
return;
}
queue1[++rear]=x;
cout <<"inserted" <<x;
}
void delet()
{
if(front==rear)
{
cout <<"queue under flow";
return;
}
cout <<"deleted" <<queue1[++front];
}
void display()
{
if(rear==front)
{
cout <<" queue empty";
return;
}
for(int i=front+1;i<=rear;i++)
cout <<queue1[i]<<" ";
}
};
main()
{
int ch;

queue qu;
while(1)
{
cout <<"\n1.insert 2.delet 3.display 4.exit\nEnter ur choice";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element";
cin >> ch;
qu.insert(ch);
break;
case 2: qu.delet(); break;
case 3: qu.display();break;
case 4: exit(0);
}
}
return (0);
}
OUTPUT
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element21
inserted21
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element22
inserted22
1.insert 2.delet 3.display 4.exit
Enter ur choice1
enter the element16
inserted16
1.insert 2.delet 3.display 4.exit
Enter ur choice3
21 22 16
1.insert 2.delet 3.display 4.exit
Enter ur choice2
deleted21
1.insert 2.delet 3.display 4.exit
Enter ur choice3
22 16
1.insert 2.delet 3.display 4.exit
Enter ur choice
cqueue
#include <iostream.h>
class cqueue
{
private :
int *arr ;
int front, rear ;
int MAX;
public :

cqueue( int maxsize = 10 ) ;


void addq ( int item ) ;
int delq( ) ;
void display( ) ;
} ;
cqueue :: cqueue( int maxsize )
{
MAX = maxsize ;
arr = new int [ MAX ];
front = rear = -1 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void cqueue :: addq ( int item )
{
if ( ( rear + 1 ) % MAX == front )
{
cout << "\nQueue is full" ;
return ;
}
rear = ( rear + 1 ) % MAX;
arr[rear] = item ;
if ( front == -1 )
front = 0 ;
}
int cqueue :: delq( )
{
int data ;
if ( front == -1 )
{
cout << "\nQueue is empty" ;
return NULL ;
}
data = arr[front] ;
arr[front] = 0 ;
if ( front == rear )
{
front = -1 ;
rear = -1 ;
}
else
front = ( front + 1 ) % MAX;
return data ;
}
void cqueue :: display(
{
cout << endl ;
for ( int i = 0
cout <<
cout << endl ;
}
void main( )
{
cqueue a ( 10 )
a.addq ( 14 ) ;
a.addq ( 22 ) ;
a.addq ( 13 ) ;
a.addq ( -6 ) ;
a.addq ( 25 ) ;

)
; i < MAX ; i++ )
arr[i] << " " ;

cout << "\nElements in the circular queue: " ;


a.display( ) ;
int i = a.delq( ) ;
cout << "Item deleted: " << i ;
i = a.delq( ) ;
cout << "\nItem deleted: " << i ;
cout << "\nElements in the circular queue after deletion: " ;
a.display( ) ;
a.addq ( 21 ) ;
a.addq ( 17 ) ;
a.addq ( 18 ) ;
a.addq ( 9 ) ;
a.addq ( 20 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
a.addq ( 32 ) ;
cout << "Elements in the circular queue after addition: " ;
a.display( ) ;
}

delete
void SinglyLinkedList::removeFirst() {
if (head == NULL)
return;
else {
SinglyLinkedListNode *removedNode;
removedNode = head;
if (head == tail) {
head = NULL;
tail = NULL;
} else {
head = head->next;
}
delete removedNode;
}
}
void SinglyLinkedList::removeLast() {
if (tail == NULL)
return;
else {
SinglyLinkedListNode *removedNode;
removedNode = tail;
if (head == tail) {
head = NULL;
tail = NULL;
} else {
SinglyLinkedListNode *previousToTail = head;
while (previousToTail->next != tail)
previousToTail = previousToTail->next;
tail = previousToTail;
tail->next = NULL;
}
delete removedNode;
}
}

void SinglyLinkedList::removeNext(SinglyLinkedListNode *previous) {


if (previous == NULL)
removeFirst();
else if (previous->next == tail) {
SinglyLinkedListNode *removedNode = previous->next;
tail = previous;
tail->next = NULL;
delete removedNode;
} else if (previous == tail)
return;
else {
SinglyLinkedListNode *removedNode = previous->next;
previous->next = removedNode->next;
delete removedNode;
}
}

Das könnte Ihnen auch gefallen