0% found this document useful (0 votes)
18 views36 pages

Khaled Mamdooh Ahmed

Uploaded by

khaledmamdooh77
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)
18 views36 pages

Khaled Mamdooh Ahmed

Uploaded by

khaledmamdooh77
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/ 36

Khaled Mamdooh Ahmed

# include <iostream>

using namespace std;

struct node

int data;

node * next;

};

# define ELEMENTS 100

void errorMassage(){

cout<<"Error... Please enter number form the above !\n";

cout<<"=============================================\n\n";

int addNumber (string msg)

{ int number;

do

cout << msg << endl;

cin >> number;

}while(number <= 0);

return number;

void fillThearray (int arr [ELEMENTS], int &length)

length = addNumber("\n \n \tEnter the size of the array: ");

cout<<"Enter the elements of the array: \n";

for (int i = 0; i < length; i++)


cin>> arr[i];

cout<<"The element of the array is added successfully\n";

cout<<"==============================================\n";

void AccessByIndex (int arr[ELEMENTS], int lenght)

int index = 0;

cout<< "Enter the inedx of a number\n";

cin >> index ;

if (index < 0 || index >= lenght)

cout<< "The index is out of rang..\n";

cout<<"==========================================\n";

else {

cout << "the index "<< index << " has the value "<< arr[index] <<endl;

cout<<"==========================================\n";

int BinarySeach (int arr[ELEMENTS], int length)

int low = 0, high = length - 1;

int mid = 0, number = 0;

cout<<"Enter a number to search for \n";

cin>> number;

while (low <= high)

mid = (low + high) / 2 ;

if (number == arr [mid])


{

cout << "The number "<< number <<" found at index "<< mid << endl;

cout<<"==========================================\n";

return 0;

else if (number > arr[mid])

low = mid + 1 ;

else

high = mid - 1 ;

cout << "The number "<< number <<" is not in the array \n";

cout<<"==========================================\n";

return 1;

void linearSearch (int arr[ELEMENTS], int length){

if (length == 0){

cout<<"The Array is empty\n";

cout<<"====================\n";

else{

int search ;

cout<<"Enter a number to search\n";

cin>>search;

cout<<"==================\n";

for (int i = 0; i < length; i++)


{

if(arr[i] == search){

cout<<"The number "<<search <<" found at index " << i <<endl;

cout<<"==============================\n";

return ;

cout<<"The number is not in the array \n";

cout<<"==============================\n";

void insertNumber (int arr[ELEMENTS], int &length)

int index = 0, value = 0;

cout<< "Enter the index of the value \n";

cin >> index;

if (index < 0 || index > length )

cout<< "The index is out of rang..\n";

cout<<"=======================================\n";

else {

cout<< "Enter the value \n";

cin >> value;

for ( int i = length ; i > index ; --i )

arr [i] = arr [i - 1] ;

arr [index] = value;


length++;

cout<< "The number is added successfully\n";

cout<< "=================================\n";

short getindex (int arr[ELEMENTS], int length , int number )

for (int i = 0; i < length ; i++)

if (arr [i] == number)

return i;

cout<< "The number is not in the array \n";

cout<< "====================================\n";

return -101;

short deleteNumber (int arr[ELEMENTS], int &length)

short choies;

cout<< "Do you wnat to delete the number by the value or by tht index \n";

cout<< "Perss 0 to go with the index or any number to go with numbers \n";

cin>> choies;

int index = 0;

if (choies != 0)

{
back:

int number;

cout<< "Enter the number \n";

cin>> number;

index = getindex (arr , length ,number);

if(index == -101){

goto back;

else

cout<< "Enter the index to delete\n";

cin >> index;

if (index < 0 || index > length -1 )

cout<< "The index is out of rang..\n";

cout<<"==========================================\n";

return 1;

for (int i = index ; i < length ; ++i)

arr [i] = arr [i + 1];

length--;

cout<< "The number is deleted successfully\n";

cout<< "=====================================\n";

return 0 ;
}

void printArray (int arr[ELEMENTS], int length )

cout<< "===========================\n";

for (int i = 0 ; i < length ; i++)

cout<<arr [i] <<"\t";

cout<<endl;

cout<< "===========================\n\n";

void searchInArray (int arr[ELEMENTS], int length){

int choose ;

cout<<"Enter number 1 to go with linear search \n";

cout<<"Enter number 2 to go wiht binary search \n";

cin>> choose;

cout<<"=============\n";

switch (choose)

case 1:

linearSearch(arr ,length);

break;

case 2:

BinarySeach (arr ,length);

break;

default:

errorMassage();

break;

}
void startArray ()

cout<< "\tWelcome to the Array section: \n";

cout<< "\t=============================";

int arr[ELEMENTS], length =0;

fillThearray(arr , length);

short howMany = addNumber ("How many Operation you want? ");

int counter = 0 ;

while(counter < howMany)

int num = 0;

cout<< "Enter number 1 to access a number in the array\n";

cout<< "Enter number 2 to search for number in the array\n";

cout<< "Enter number 3 to insert a number in the array\n";

cout<< "Enter number 4 to delete a number in the array\n";

cout<< "Enter number 5 to print the array\n";

cout<< "Enter number 6 to start with new array\n";

cout<< "Enter number 7 to go to the main function \n";

cout<< "==================================================\n";

cin>> num;

switch (num)

case 1:

AccessByIndex (arr , length); break;

case 2:

searchInArray (arr ,length); break;

case 3:

insertNumber(arr, length); break;


case 4:

deleteNumber(arr, length); break;

case 5:

printArray (arr, length); break;

case 6:

fillThearray (arr, length); break;

case 7:

return;

default :

errorMassage();

break;

++counter;

void getnodes(node * &head){

node *newNode , *temp;

head = NULL ; int choose ;

cout<<"\t *Get nodes to start* \n\n";

do

newNode = new node ;

cout<<"Enter the data of the node \n";

cin>> newNode->data;

newNode ->next = NULL;

if (head == NULL)

head = temp = newNode;


else {

temp->next = newNode;

temp = newNode;

cout<<"node is added successfully \n";

cout<<"==========================\n";

cout<<"Do you to add a new node (press 0 if you want to exit)\n";

cin>>choose;

if (choose == 0)

temp = head;

cout<<"==========================\n";

while (temp != NULL)

cout<< temp ->data << "\t";

temp = temp ->next;

cout<<"\n==========================\n";

} while (choose);

int getLinkedListLngth(node * head){

int counter = 0;

node * temp;

temp = head;

while (temp != NULL)

counter++;
temp = temp->next;

return counter;

void insterFromBeginning (node * &head, node * &newNode){

newNode->next = head;

head = newNode;

cout<<"node is added successfully \n";

cout<<"==========================\n";

void insterFromEnd (node * &head, node * newNode){

node * temp;

temp = head;

if (temp == NULL){

head = newNode;

cout<<"node is added successfully \n";

cout<<"==========================\n";

}else{

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

cout<<"node is added successfully \n";

cout<<"==========================\n";

void insterFromGivenPosition (node * &head, node * &newNode){

int counter = getLinkedListLngth(head);

node *temp;

int position , i = 1;
cout<<"Enter the position that you want to insertion at \n";

cin>> position;

if (position > counter +1 )

cout<<"Error... Invalid position \n";

cout<<"=======================\n";

else if (position == 1)

insterFromBeginning (head, newNode);

else {

temp = head;

while (i < position -1)

temp = temp->next;

i++;

newNode->next = temp->next;

temp->next = newNode;

cout<<"node is added successfully \n";

cout<<"==========================\n";

void insertionLinkedList (node * &head){

int number;

cout<<"Enter the value of the node \n";

cin>>number;

node * newNode = new node;


newNode->data = number;

newNode->next = NULL;

int choose ;

cout<<"===============\n";

cout<<"press 1 to insert from the beginning \n";

cout<<"press 2 to insert from the given position \n";

cout<<"press 3 to insert from the end \n";

cin>> choose;

switch (choose)

case 1:

insterFromBeginning(head, newNode);

break;

case 2:

insterFromGivenPosition(head, newNode);

break;

case 3:

insterFromEnd(head, newNode);

break;

default:

errorMassage();

break;

void deleteFromBeginning (node * &head, node * &temp){

if (head == NULL){

cout<<"There is no element to delete \n";

cout<<"==============================\n";
}

else{

temp = head;

head = head->next;

delete(temp);

cout<<"node is deleted successfully \n";

cout<<"==========================\n";

void deleteFromGivenPosition (node * &head, node * temp){

node * pre ;

int position , i = 1;

int counter = getLinkedListLngth(head);

pre = head;

cout<< "Enter the position of the node \n";

cin >> position;

if (position > counter)

cout<<"Error... Invalid index \n";

cout<<"=======================\n";

else{

while (i < position -1 )

pre= pre ->next;

i++;

if (head == NULL){

cout<<"There is no element to delete \n";


cout<<"==============================\n";

else if (position == 1)

deleteFromBeginning (head,temp);

else{

temp = pre->next;

pre->next = temp ->next;

delete(temp);

cout<<"node is deleted successfully \n";

cout<<"==========================\n";

void deleteFromEnd (node *&head, node * &temp){

node * pre;

temp = head;

if (head == NULL){

cout<<"There is no element to delete \n";

cout<<"==============================\n";

return ;

while (temp->next != NULL)

pre = temp;

temp = temp ->next;

}
if(temp == head){

head = NULL;

delete(temp);

cout<<"node is deleted successfully \n";

cout<<"==========================\n";

else{

pre ->next = NULL;

delete(temp);

cout<<"node is deleted successfully \n";

cout<<"==========================\n";

void deletionLinkedList (node * &head){

node * temp;

int choose ;

cout<<"=================\n";

cout<<"press 1 to delete from the beginning \n";

cout<<"press 2 to delete from the given position \n";

cout<<"press 3 to delete from the end \n";

cin>> choose;

cout<<"=================\n";

switch (choose)

case 1:

deleteFromBeginning(head ,temp);

break;

case 2:
deleteFromGivenPosition(head ,temp);

break;

case 3:

deleteFromEnd(head ,temp);

break;

default:

errorMassage();

break;

void displaylinkList (node * head){

if (head == NULL){

cout<<"The linked list is empty\n";

cout<<"=======================\n";

else{

node * temp;

temp = head;

while (temp != NULL)

cout<< temp ->data << "\t";

temp = temp ->next;

cout<<"\n======================\n";

void searchLinkedList (node * head){

int number , counter = 1;

node * temp;
cout<<"Enter the value \n";

cin>>number;

temp = head;

while (temp != NULL)

if (number == temp->data){

cout<<"The number " << temp->data <<" found at the node number " << counter <<endl;

cout<< "====================================\n";

return ;

temp = temp->next;

counter++;

cout<< "The number is not in the linked list \n";

cout<< "====================================\n";

void statrLinkedList(){

cout<<"\t Welcome to linked list \n";

cout<<"\t ====================\n";

node *head ; int choose;

getnodes(head);

while (true)

cout<<"press 1 to insert a node \n";

cout<<"press 2 to delete a node \n";

cout<<"press 3 to display the linked list \n";

cout<<"press 4 to search in linked list \n";

cout<<"press 5 to go back to main \n";

cin>>choose;
switch (choose)

case 1:

insertionLinkedList(head);

break;

case 2:

deletionLinkedList(head);

break;

case 3:

displaylinkList(head);

break;

case 4:

searchLinkedList(head);

break;

case 5:

return;

default:

errorMassage();

break;

void enqueueArray (int queue [ELEMENTS],int &front ,int &rear){

int number ;

cout <<"Enter the value \n";

cin>> number;

if (rear == ELEMENTS -1 ){

cout<<"Overflow \n";
cout<<"============\n";

else if (front == -1 && rear == -1){

front = rear = 0;

queue[rear] = number;

cout<<"The number is added successfully\n";

cout<<"==================================\n";

else {

queue[++rear] = number;

cout<<"The number is added successfully\n";

cout<<"==================================\n";

void dequeueArray (int queue [ELEMENTS],int &front ,int &rear){

if (front == -1){

cout <<"The Queue is undreflow\n";

cout <<"========================\n";

else if(front == rear){

cout<<"The delete element is "<<queue[front];

cout<<"\n===========================\n";

front = rear = -1 ;

else{

cout<<"The delete element is "<<queue[front++];

cout<<"\n===========================\n";

}
void displayArrayQueue (int queue [ELEMENTS],int front ,int rear){

if (front == -1){

cout <<"The queue is empty \n";

cout <<"====================\n";

else{

for (int i = front; i <= rear; i++)

cout<<queue[i] <<"\t";

cout<<"\n====================\n";

void peekArrayQueue (int queue [ELEMENTS],int &front ){

if(front == -1 ){

cout <<"The queue is empty \n";

cout <<"====================\n";

else{

cout<<"The front of the queue is "<< queue[front];

cout<<"\n============================\n";

int startQueueArray (int queue [ELEMENTS]){

cout<<"You are in Queue as an array \n";

cout<<"============================== \n\n";

int front = -1 , rear = -1;

int choose = 0 ;

while (true)

cout <<"Enter number 1 to enqueue the queue \n";


cout <<"Enter number 2 to dequeue the queue \n";

cout <<"Enter number 3 to display the queue \n";

cout <<"Enter number 4 to peek the front \n";

cout <<"Enter number 5 to choose Array or Linked list \n";

cout <<"Enter number 6 to go back to main \n";

cin >> choose;

switch (choose)

case 1:

enqueueArray (queue , front , rear);

break;

case 2:

dequeueArray (queue , front , rear);

break;

case 3:

displayArrayQueue (queue , front , rear);

break;

case 4:

peekArrayQueue (queue , front );

break;

case 5:

return 1;

case 6:

return -101;

default:

errorMassage();

break;

}
}

return 0;

void enqueueLinkedList (node * &front, node * &rear){

int number ;

cout<<"Enter the value \n";

cin>> number;

node * newNode = new node;

newNode->data = number;

newNode->next = NULL;

if (front == NULL){

front = rear = newNode;

cout<<"The number is added successfully\n";

cout<<"==================================\n";

else{

rear->next = newNode;

rear = newNode;

cout<<"The number is added successfully\n";

cout<<"==================================\n";

void dequeueLiskedList (node * &front){

if (front == NULL){

cout<< "The Queue is underflow\n";

cout<< "========================\n";

else{

cout<<"The delete node is "<< front->data ;


cout<<"\n==========================\n";

node * temp;

temp = front;

front = front->next;

delete(temp);

void displayListQueue (node * front){

if (front == NULL){

cout<<"The Queue is empty\n";

cout<<"====================\n";

else{

node * temp;

temp = front;

while (temp != NULL)

cout<< temp->data <<"\t";

temp = temp->next;

cout<<"\n====================\n";

void peekqueueQueue (node * front){

if (front == NULL){

cout<<"The Queue is empty\n";

cout<<"====================\n";

else {
cout<<"The front of the queue is " << front->data;

cout<<"\n============================\n";

int startQueueLinkedList(){

cout<<"You are in Queue as a linked list \n";

cout<<"============================== \n\n";

node * front = NULL ;

node * rear = NULL;

int choose = 0 ;

while (true)

cout <<"Enter number 1 to enqueue the queue \n";

cout <<"Enter number 2 to dequeue the queue \n";

cout <<"Enter number 3 to display the queue \n";

cout <<"Enter number 4 to peek the front \n";

cout <<"Enter number 5 to choose Array or Linked list \n";

cout <<"Enter number 6 to go back to main \n";

cin >> choose;

switch (choose)

case 1:

enqueueLinkedList (front,rear);

break;

case 2:

dequeueLiskedList (front);

break;

case 3:

displayListQueue (front);
break;

case 4:

peekqueueQueue(front);

break;

case 5:

return 1;

case 6:

return -101;

default:

errorMassage();

break;

return 0;

void startQueue(){

int queue [ELEMENTS];

int choose = 0 ,back;

cout << " \t Wlecom to Queue \n";

cout << " \t ===============\n";

while (true)

cout <<"Enter number 1 to go with array \n";

cout <<"Enter number 2 to go with linked list \n";

cout <<"Enter number 3 to go back to main \n";

cout <<"=====================================\n";

cin >> choose;

switch (choose)
{

case 1:

back = startQueueArray (queue);

if (back == -101){

return ;

break;

case 2:

back = startQueueLinkedList();

if (back == -101){

return ;

break;

case 3:

return ;

default:

errorMassage();

break;

void pushArray (int stack [ELEMENTS],int &top){

int number ;

if(top == ELEMENTS - 1 ){

cout<<"Stack Overflow \n";

cout<<"===================\n";
}

else {

cout <<"Enter the value \n";

cin>> number;

stack [++top] = number;

cout<<"Number is added successfully\n";

cout<<"==============================\n";

void popaArray (int stack [ELEMENTS],int &top){

if (top == -1){

cout<<"Stack Underflow\n";

cout<<"=================\n";

else{

cout<<"The deleted element is ";

cout<<stack[top--] ;

cout<<"\n=========================\n";

void displayArrayStack (int stack [ELEMENTS],int top) {

if (top == -1){

cout<< "The stack is empty";

else{

for (int i = top; i >= 0; i--){

cout<<stack[i];

cout<<"\t";

}
}

cout<<"\n=================\n";

void peekArrayStack (int stack [ELEMENTS],int top){

if (top == -1){

cout<< "The stack is empty\n";

cout<< "====================\n";

else{

cout<<"The top is ";

cout<<stack[top];

cout<<"\n===============\n";

void startStackArray (int stack [ELEMENTS]){

cout<<"You are in stack as an array \n";

cout<<"============================== \n\n";

int top = -1;

int choose = 0 ;

while (true)

cout <<"Enter number 1 to push the stack \n";

cout <<"Enter number 2 to pop the stack \n";

cout <<"Enter number 3 to display the stack \n";

cout <<"Enter number 4 to peek the top \n";

cout <<"Enter number 5 to go back to main stack \n";

cin >> choose;

switch (choose)
{

case 1:

pushArray (stack , top);

break;

case 2:

popaArray (stack , top);

break;

case 3:

displayArrayStack (stack, top);

break;

case 4:

peekArrayStack (stack , top);

break;

case 5:

return ;

default:

errorMassage();

break;

void pushList (node * &top){

node * newNode;

int number ;

cout<< "Enter the value\n";

cin>> number;

newNode = new node;

newNode->data = number ;
newNode->next = top ;

top = newNode;

cout<<"Node is added successfully \n";

cout<<"=============================\n";

void popList (node * &top){

node * temp ;

temp = top;

if (top == NULL){

cout<<"Stack Underflow\n";

cout<<"=================\n";

else{

cout<<"The deleted node is " <<top->data;

cout<<"\n======================\n";

top = top->next;

delete(temp);

void displayListStack (node * top){

if (top == NULL){

cout<<"Stack is empty\n";

cout<<"=================\n";

else{

node * temp;

temp = top;

while (temp != NULL)

{
cout<<temp->data << "\t";

temp = temp ->next;

cout<<"\n=====================\n";

int peekListStack (node * top){

if (top == NULL){

cout<<"Stack is empty top is ";

return 0;

else{

cout<<"The top is ";

return top->data;

void startStackLinkedList (){

cout<<"You are in Stack as a linked list \n";

cout<<"============================== \n\n";

node * top = NULL;

int choose = 0 ;

while (true)

cout <<"Enter number 1 to push the stack \n";

cout <<"Enter number 2 to pop the stack \n";

cout <<"Enter number 3 to display the stack \n";

cout <<"Enter number 4 to peek the top \n";

cout <<"Enter number 5 to go back to main \n";

cin >> choose;


switch (choose)

case 1:

pushList (top);

break;

case 2:

popList (top);

break;

case 3:

displayListStack (top);

break;

case 4:

cout << peekListStack (top);

cout << "\n===================\n";

break;

case 5:

return ;

default:

errorMassage();

break;

void startStack (){

int stack [ELEMENTS];

int choose = 0 ;

cout << " \t Wlecom to Stack \n";

cout << " \t ===============\n";

while (true)
{

cout <<"Enter number 1 to go wiht array \n";

cout <<"Enter number 2 to go wiht linked list \n";

cout <<"=====================================\n";

cin >> choose;

switch (choose)

case 1:

startStackArray (stack);

return ;

case 2:

startStackLinkedList();

return;

default:

errorMassage();

break;

void startProgram (){

cout << "\n\n\t\tThe Data Structure Project :\n";

cout << "\n\t\t**************************\n\n";

while (true)

cout<< "=======================================\n";

cout<< "Enter number 1 to go with Array \n";

cout<< "Enter number 2 to go with Linked List \n";

cout<< "Enter number 3 to go with stack \n";


cout<< "Enter number 4 to go with queue \n";

cout<< "Enter number 5 to exit form the program \n";

cout<< "=======================================\n";

short number = 0 ;

cin >> number;

switch (number)

case 1:

startArray();

break;

case 2:

statrLinkedList();

break;

case 3:

startStack();

break;

case 4:

startQueue();

break;

case 5:

exit(0);

break;

default:

errorMassage();

break;

}
int main ()

startProgram();

return 0;

You might also like