Professional Documents
Culture Documents
مذكرة عملي هياكل (تم الحفظ تلقائيًا)
مذكرة عملي هياكل (تم الحفظ تلقائيًا)
sa
* C++ Stack - Example Program of addition to
Stack
#include<iostream>
using namespace std;
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 main()
{
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;
}
4
int pop(int stack[], int &top)
{
int ret;
if(top==-1)
{
return -1;
}
else
{
ret=stack[top];
top--;
}
return ret;
}
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()
{
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;
}
7
#include<iostream>
using namespace std;
int delete_from_queue(int []);
int insert_in_queue(int [], int);
void display(int [], int, int);
int queue[SIZE];
int front=-1;
int rear=-1;
int main()
{
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;
}
9
}
return 0;
}
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;
}
}
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()
{
while(ch=='y' || ch=='Y')
{
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;
}
16
}
output
Enter Information for the new node: 1
Creating new node..!!..Press any key to continue..
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()
{
while(ch=='y' || ch=='Y')
{
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;
}
19
rear = np;
}
}
void delete_node()
{
if(start == NULL)
{
cout<<"Underflow...!!\n";
}
else
{
ptr = start;
start = start->next;
delete ptr;
}
}
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()
{
}
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()
{
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