Practical Data Structure
Practical Data Structure
int main()
{
int array[MAX_SIZE];
int size = 0;
int choice, value;
do
{
cout << "\n\n****************** MENU ********************\n";
cout << "1. Insert\n";
cout << "2. Search\n";
cout << "3. Display\n";
cout << "4. Sort\n";
cout << "5. Delete\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the value you want to insert: ";
cin >> value;
size = insert(array, size, value);
break;
case 2:
if (size == 0)
{
cout << "The array is already empty";
break;
}
cout << "Enter the value : ";
cin >> value;
search(array, size, value);
break;
case 3:
display(array, size);
break;
case 4:
sort(array, size);
display(array, size);
break;
case 5:
if (size == 0)
{
cout << "The array is already empty";
break;
}
cout << "Enter the value you want to delete: ";
cin >> value;
size = remove(array, size, value);
break;
case 6:
cout << "BYE";
break;
default:
cout << "you can only enter numbers from 1 to 6 ";
break;
}
}
while (choice != 6);
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include <iostream>
int main()
{
isPlaindrom(Samy)
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include <iostream>
struct node {
int data;
struct node *next;
};
if (start == NULL) {
start = new_node;
} else {
ptr = start;
while (ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
}
cout << "Enter the data :" << " ";
cin >> num;
}
return start;
}
if (ptr == NULL) {
cout << "Node " << s_val << " not found" << endl;
} else {
node* new_node = new node();
new_node -> data = n_val;
new_node -> next = ptr -> next;
ptr -> next = new_node;
cout << "Node " << n_val << " inserted after node " << s_val << "
successfully." << endl;
}
return start;
}
node* insert_Before(node* start, int n_val, int s_val) {
node* new_node = new node();
new_node -> data = n_val;
if (start == NULL) {
cout << "The list is empty." << endl;
delete new_node;
return start;
}
while (ptr -> next != NULL && ptr -> next -> data != s_val) {
ptr = ptr -> next;
}
return start;
}
int main() {
int option, new_val, search_val;
struct node *start = NULL;
do {
cout << endl << endl <<
"---------------------------------------------------------" << endl;
cout << " ***** MAIN MENU *****" << endl;
cout << "1: Create a list" << endl;
cout << "2: Display the list" << endl;
cout << "3: Insert at the beginning" << endl;
cout << "4: Insert at the end" << endl;
cout << "5: Insert before a node" << endl;
cout << "6: Insert after a node" << endl;
cout << "7: Delete from the beginning" << endl;
cout << "8: Delete from the end" << endl;
cout << "9: Delete Any node" << endl;
cout << "10: EXIT" << endl;
cout << "---------------------------------------------------------" << endl
<< endl;
cout << "Enter an Option : " << " ";
cin >> option ;
switch (option) {
case 1:
start = create_linked_list(start);
cout << endl << "LINKED LIST CREATED";
break;
case 2:
display(start);
break;
case 3:
cout << "Enter value to insert at the beginning : ";
cin >> new_val;
start = insert_Begin(start, new_val);
break;
case 4:
cout << "Enter value to insert at the end: ";
cin >> new_val;
start = insert_End(start, new_val);
break;
case 5:
cout << "Enter the search node: ";
cin >> search_val;
cout << "Enter value to insert before the search node: ";
cin >> new_val;
start = insert_Before(start, new_val, search_val);
break;
case 6:
cout << "Enter the search node: ";
cin >> search_val;
cout << "Enter value to insert after the search node: ";
cin >> new_val;
start = insert_After(start, new_val, search_val);
break;
case 7:
start = Delete_beginning(start);
break;
case 8:
start = Delete_end (start);
break;
case 9:
cout << "Enter the node to delete it :" << " ";
cin >> search_val;
start = Delete_node(start,search_val);
break;
}
} while (option != 10);
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include <iostream>
#define Size 10
using namespace std;
int arr[Size];
int Top = -1;
void pop(){
if(Top == -1)
cout << "The Stack is Empty";
else
arr[Top--];
return;
}
void display(){
if(Top == -1)
cout << "The Stack is Empty";
else
cout << "The Element is :" << " ";
for(int i = Top ; i >= 0 ; i--){
cout << arr[i] << " ";
}
}
void peek(){
if(Top == -1)
cout << "The Stack is Empty";
else
arr[Top];
return;
}
int main()
{
push(15);
push(50);
push(30);
pop();
display();
return 0;
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include <iostream>
#define Size 10
using namespace std;
int arr[Size];
int Front = -1;
int Rear = -1;
void Dequeu(){
if(Rear == Front || Rear == -1)
cout << "The Queue is Empty !";
else
Front ++;
arr[Front];
return;
}
void display(){
if(Rear == Front || Rear == -1)
cout << "No Element";
else
cout << "The Element of Queue is :" << " ";
for(int i = ++Front ; i <= Rear ; i++){
cout << arr[i] << " ";
}
}
void Peek(){
if(Rear == Front || Rear == -1)
cout << "The Queue is Empty !";
else
cout << arr[Front] ;
}
int main()
{
Enqueu(20);
Enqueu(13);
Enqueu(50);
Dequeu();
display();
cout << endl ;
Peek();
cout << endl ;
Dequeu();
display();
return 0;
}