Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

r.khalid@tu.edu.

sa
* C++ Stack - Example Program of addition to
Stack

#include<iostream>
using namespace std;

int push(int [], int &, int);


void display(int [], int);
const int SIZE = 3;

int main()
{
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{

cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";

exit(1);
}
cout<<"Element inserted
successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n)..
";
cin>>ch;
}

1
int push(int stack[], int &top, int elem)
{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
void display(int stack[], int top)
{
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{

cout<<stack[i]<<"\n";
}

2
* C++ Stack - Example Program of C++ Stack
* This C++ program demonstrates the concept
* of Popping from the stack-array in C++ */

#include<iostream>
using namespace std;

int pop(int [], int &);


int push(int [], int &, int);
void display(int [], int);
const int SIZE = 50;

int main()
{

int stack[SIZE], item, top=-1, res;


char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{

cout<<"Overflow..!!..Aborting..Press a key
to exit..\n";

exit(1);
}
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n)..
";
cin>>ch;
}
cout<<"Now the deletion of elements
starts..\n";
ch='y';

3
while(ch=='y' || ch=='Y')
{
res = pop(stack, top);
if(res==-1)
{

cout<<"\nUnderflow..!!..Aborting..!!..Press
a key to exit..\n";

exit(2);
}
else
{
cout<<"\nElement deleted is:
"<<res<<endl;
cout<<"\nThe Stack now is:\n";
display(stack, top);
}
cout<<"Want to delete more ? (y/n)..
";
cin>>ch;
}

int push(int stack[], int &top, int elem)


{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}

4
int pop(int stack[], int &top)
{
int ret;
if(top==-1)
{
return -1;
}
else
{
ret=stack[top];
top--;
}
return ret;
}

void display(int stack[], int top)


{
if(top==-1)
{
return;
}
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

5
/* C++ Queue - Example Program of C++ Queue
* This program demonstrates the concept of
* insertion in an array queue in C++ */
#include<iostream>
using namespace std;
int insert_in_queue(int [], int);
void display(int [], int, int);
const int SIZE = 50;
int queue[SIZE];
int front=-1;
int rear=-1;
int main()
{

int item, check;


char ch='y';

while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{

cout<<"\nOverflow..!!..Aborting..!!..Press
a key to exit..\n";

exit(1);
}
cout<<"Item inserted
successfully..!!\n";
cout<<"\nNow the Queue
(Front...to...Rear) is:\n";
display(queue, front, rear);
cout<<"\nWant to insert more ? (y/n)..
";
cin>>ch;
}
}

6
int insert_in_queue(int queue[], int elem)
{
if(rear == SIZE-1)
{
return -1;
}
else if(rear == -1)
{
front = rear = 0;
queue[rear] = elem;
}
else
{
rear++;
queue[rear] = elem;
}
return 0;
}

void display(int queue[], int front, int rear)


{
if(front == -1)
{
return;
}
for(int i=front; i<rear; i++)
{
cout<<queue[i]<<" <- ";
}
cout<<queue[rear]<<"\n";
}

7
#include<iostream>
using namespace std;
int delete_from_queue(int []);
int insert_in_queue(int [], int);
void display(int [], int, int);

const int SIZE = 50;

int queue[SIZE];
int front=-1;
int rear=-1;

int main()
{

int item, check;


char ch='y';

while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{

cout<<"\nOverflow..!!..Aborting..!!..Press
a key to exit..\n";

exit(1);
}
cout<<"Item inserted
successfully..!!\n";
cout<<"\nNow the Queue
(Front...to...Rear) is:\n";
display(queue, front, rear);
cout<<"\nWant to insert more ? (y/n)..
";
cin>>ch;
}

8
cout<<"Now deletion of elements starts...\n";
ch='y';
while(ch=='y' || ch=='Y')
{
check = delete_from_queue(queue);
if(check == -1)
{

cout<<"\nUnderflow..!!..Aborting..!!..Pres
a key to exit..\n";
exit(2);
}
else
{
cout<<"\nElement deleted is:
"<<check<<"\n";
cout<<"Now the Queue
(Front...to...Rear) is:\n";
display(queue, front, rear);
}
cout<<"\nWant to delete more ?
(y/n)... ";
cin>>ch;
}

int insert_in_queue(int queue[], int elem)


{
if(rear == SIZE-1)
{
return -1;
}
else if(rear == -1)
{
front = rear = 0;
queue[rear] = elem;
}
else
{
rear++;
queue[rear] = elem;

9
}

return 0;
}

int delete_from_queue(int queue[])


{
int retn;
if(front == -1)
{
return -1;
}
else
{
retn = queue[front];
if(front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
return retn;
}
void display(int queue[], int front, int rear)
{
if(front == -1)
{
return;
}
for(int i=front; i<rear; i++)
{
cout<<queue[i];" -< "<<
}
cout<<queue[rear]<<"\n;"

10
11
#include<iostream>
using namespace std ;
struct node
{
int info;
node *next;
* }start, *newptr, *save, *ptr;

node *create_new_node(int);
void insert_at_beg(node *);
void display(node *);
int main)(
{
start = NULL;
int inf;
char ch='y;'

while(ch=='y' || ch=='Y')
{
cout<<"Enter Information for the new
node;" :
cin>>inf;
cout<<"\nCreating new node..!!..Press
any key to continue;"..
newptr = create_new_node(inf);
if(newptr != NULL)
{
cout<<"\n\nNew node created
successfully..!!\n;"
cout<<"Press any key to
continue;"..

}
else
{
cout<<"\nSorry..!!..cannot create
new node..!!..Aborting;"!!..
cout<<"\nPress any key to
exit;"..

exit;) (
}

12
cout<<"\n\nNow inserting this node at the
beginning of the list..\n";
cout<<"Press any key to continue..\n";
insert_at_beg(newptr);
cout<<"\nNode successfully inserted at
the beginning of the list.\n";
cout<<"Now the list is:\n";
display(start);
cout<<"\nWant to enter more nodes ?
(y/n)..";
cin>>ch;
}
}
node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}
void insert_at_beg(node *np)
{
if(start==NULL)
{
start = np;
}
else
{
save = start;
start = np;
np->next = save;
}
}

void display(node *np)


{
while(np != NULL)
{
cout<<np->info<<" -> ";
np = np->next;
}
cout<<"!!\n";
}

13
14
* C++ Linked Lists - Example Program of Linked
Lists
* Insertion in the end of the list */
#include<iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr, *rear;

node *create_new_node(int);
void insert_in_end(node *);
void display(node *);

int main()
{

start = rear = NULL;


int inf;
char ch='y';

while(ch=='y' || ch=='Y')
{

cout<<"Enter Information for the new


node: ";
cin>>inf;
cout<<"\nCreating new node..!!..Press
any key to continue..";

newptr = create_new_node(inf);
if(newptr != NULL)
{
cout<<"\n\nNew node created
successfully..!!\n";
cout<<"Press any key to
continue..";

15
}
else
{
cout<<"\nSorry..!!..cannot create
new node..!!..Aborting..!!";
cout<<"\nPress any key to
exit..";

exit(1);
}
cout<<"\n\nNow inserting this node in
the end of the list..\n";
cout<<"Press any key to continue..\n";

insert_in_end(newptr);
cout<<"\nNode successfully inserted in
the end of the list.\n";
cout<<"Now the list is:\n";
display(start);
cout<<"\nWant to enter more nodes ?
(y/n)..";
cin>>ch;
}
}

node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}

void insert_in_end(node *np)


{
if(start==NULL)
{
start = rear = np;
}
else
{
rear -> next = np;
rear = np;
}

16
}

void display(node *np)


{
while(np != NULL)
{
cout<<np->info<<" -> ";
np = np->next;
}
cout<<"!!\n";
}

output
Enter Information for the new node: 1
Creating new node..!!..Press any key to continue..

New node created successfully!!..


Press any key to continue..

Now inserting this node in the end of the list..


Press any key to continue..

Node successfully inserted in the end of the list.


Now the list is:
!! >-

Want to enter more nodes ? (y/n)..y


Enter Information for the new node: 32
Creating new node..!!..Press any key to continue..

New node created successfully!!..


Press any key to continue..

Now inserting this node in the end of the list..


Press any key to continue..

Node successfully inserted in the end of the list.


Now the list is:
!! >- >-

Want to enter more nodes ? (y/n)..

17
* C++ Linked Lists - Example Program of Linked
Lists
* Deletion from the beginning of the list.
* This program first creates the linked list,
then
* allows user to delete nodes from the beginning
* of the list */
// Online C++ compiler to run C++ program online

#include<iostream>
using namespace std;
struct node
{
int info;
node *next;
} *start, *newptr, *save, *ptr, *rear;

node *create_new_node(int);
void insert_node(node *);
void display_node(node *);
void delete_node();

int main()
{

start = rear = NULL;


int inf;
char ch='y';

while(ch=='y' || ch=='Y')
{

cout<<"Enter Information for the new


node: ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{

18
cout<<"\nSorry..!!..cannot create new
node..!!..Aborting..!!";
cout<<"\nPress any key to
exit..";
exit(1);
}
insert_node(newptr);
cout<<"\nWant to enter more nodes ?
(y/n)..";
cin>>ch;
}

do
{
cout<<"The list now is:\n";
display_node(start);
cout<<"\nWant to delete first node ?
(y/n)..";
cin>>ch;
if(ch=='y' || ch=='Y');
{
delete_node();
}
}while(ch=='y' || ch=='Y');

node *create_new_node(int n)
{
ptr = new node;
ptr->info = n;
ptr->next = NULL;
return ptr;
}

void insert_node(node *np)


{
if(start==NULL)
{
start = rear = np;
}
else
{
rear -> next = np;

19
rear = np;
}
}

void delete_node()
{
if(start == NULL)
{
cout<<"Underflow...!!\n";
}
else
{
ptr = start;
start = start->next;
delete ptr;
}
}

void display_node(node *np)


{
while(np != NULL)
{
cout<<np->info<<" -> ";
np = np->next;
}
cout<<"!!\n";
}

20
*/C++ Program - Linear Search/*
#include <iostream>
using namespace std;
int main)(
{
int arr[10], i, num, n, c=0, pos;
cout<<"Enter the array size;" :
cin>>n;
cout<<"Enter Array Elements;" :
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the number to be search;" :
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
{

21
cout<<"Number not found;"!!..
}
else
{
cout<<num<<" found at position "<<pos;
}
return 0;
}

22
#include<iostream>
using namespace std;
int main()
{

int n, i, arr[50], search, first, last,


middle;
cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" number :";
for (i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter a number to find :";
cin>>search;
first = 0;
last = n-1;
middle = (first+last)/2;
while (first <= last)
{
if(arr[middle] < search)
{
first = middle + 1;

}
else if(arr[middle] == search)
{
cout<<search<<" found at location
"<<middle+1<<"\n";
break;
}

23
else
{
last = middle - 1;
}
middle = (first + last)/2;
}
if(first > last)
{
cout<<"Not found! "<<search<<" is not
present in the list.";
}
return 0;

24
#include<iostream>
using namespace std;
int main()
{

int n, i, arr[50], j, temp;


cout<<"Enter total number of elements :";
cin>>n;
cout<<"Enter "<<n<<" numbers :";
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using bubble sort
technique...\n";
for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"Elements sorted successfully..!!\n";
cout<<"Sorted list in ascending order :\n";
for(i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
return 0;

25
26
/*C++ Program - Selection Sort */
/* C++ Program - Binary Search */

#include<iostream>
using namespace std;
int main()
{
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection
sort...\n";
for(i=0; i<size; i++)
{
for(j=i+1; j<size; j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Now the Array after sorting is :\n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}

return 0;

27
28
/*C++ Program - Insertion Sort */
#include<iostream>
using namespace std;
int main()
{
int size, arr[50], i, j, temp;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter Array Elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Sorting array using selection sort
... \n";
for(i=1; i<size; i++)
{
temp=arr[i];
j=i-1;
while((temp<arr[j]) && (j>=0))
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=temp;
}
cout<<"Array after sorting : \n";
for(i=0; i<size; i++)
{
cout<<arr[i]<<" ";
}

return 0;
}

29
30

You might also like