0% found this document useful (0 votes)
4 views

Practical Data Structure

Uploaded by

amrsherif813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Practical Data Structure

Uploaded by

amrsherif813
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 10

*********************** Array ***********************

void display(int inputArr[], int size)


{
if (size == 0)
{
cout << "The array is empty";
return;
}

cout << "The array :"<<" ";


for (int i = 0; i < size; i++)
{
cout << inputArr[i] << " ";
}
}

void search(int inputArr[], int size, int value)


{
if (size == 0)
{
cout << "The array is empty";
return;
}
for (int i = 0; i < size; i++)
{
if (inputArr[i] == value)
{
cout << "The element is founded ";
return;
}
}
cout << "The element is not found";
}

void sort(int inputArr[], int size)


{
if (size == 0)
{
return;
}
int temp;
bool swapped = true;
while (swapped)
{
swapped = false;
for (int i = 0; i < size - 1; i++)
{
if (inputArr[i] > inputArr[i + 1])
{
temp = inputArr[i + 1];
inputArr[i + 1] = inputArr[i];
inputArr[i] = temp;
swapped = true;
}
}
}
cout << "Sorted\n";
}
int remove(int inputArr[], int size, int value)
{
bool isFound = false;
for (int i = 0; i < size; i++)
{
if (inputArr[i] == value)
{
isFound = true;
for (int j = i; j < size - 1; j++)
{
inputArr[j] = inputArr[j + 1];
}
cout << "Deleted";
break;
}
}
if (isFound == true)
{
size--;
return size;
}
cout << "The value is not found";
return size;
}

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;
}

-----------------------------------------------------------------------------------
-------------------------------------------------------

*********************** String ***********************

#include <iostream>

using namespace std;

int main()
{
isPlaindrom(Samy)
return 0;
}

bool isPlaindrom(string str)


{
int head = 0, tail = str.length() - 1;
bool isP = true;
while (head < tail)
{
if (str[head] != str[tail])
{
isP = false;
break;
}
head++;
tail--;
}
return true;
}

-----------------------------------------------------------------------------------
-------------------------------------------------------

*********************** Linked list ***********************

#include <iostream>

using namespace std;

struct node {
int data;
struct node *next;
};

node* create_linked_list(struct node *start) {


struct node *new_node, *ptr;
int num;
cout << endl << "Enter -1 to end" << endl;
cout << "Enter the data : " << "";
cin >> num;
while (num != -1) {
node* new_node = new node;
new_node -> data = num;
new_node -> next = NULL;

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;
}

void display(struct node *start) {


struct node *ptr;
ptr = start;
if (ptr == NULL)
cout << "The linked list is Empty.";
else {
cout << "The linked list is : {" << " ";
while (ptr != NULL) {
cout << ptr -> data << " ";
ptr = ptr -> next;
}
cout << "}";
}
}

node* insert_Begin(node* start, int n_val) {


node* new_node = new node();
new_node -> data = n_val;
new_node -> next = start;
start = new_node;
return start;
}

node* insert_End(node* start, int n_val) {


node* new_node = new node();
new_node -> data = n_val;
new_node -> next = NULL;
if (start == NULL) {
start = new_node;
} else {
node* ptr = start;
while (ptr -> next != NULL) {
ptr = ptr -> next;
}
ptr -> next = new_node;
}
return start;
}

node* insert_After(node* start, int n_val, int s_val) {


node* ptr = start;
while (ptr != NULL && ptr -> data != s_val) {
ptr = ptr -> next;
}

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;
}

if (start -> data == s_val) {


new_node -> next = start;
start = new_node;
return start;
}

node* ptr = start;

while (ptr -> next != NULL && ptr -> next -> data != s_val) {
ptr = ptr -> next;
}

if (ptr -> next == NULL) {


cout << "Node " << s_val << " not found." << endl;
delete new_node;
} else {
new_node -> next = ptr -> next;
ptr -> next = new_node;
cout << "Node " << n_val << " inserted before node " << s_val << "
successfully." << endl;
}

return start;
}

node* Delete_beginning (node* start){


if(start == NULL)
cout << "The Linked list is Empty";
else {
node* ptr = start;
start = start -> next;
delete ptr;
return start;
}
}

node* Delete_end (node* start){


if(start == NULL){
cout << "The Linked list is Empty";
return start;
}

if(start -> next == NULL){


delete start;
start = NULL;
return start;
}
node* temp = start;
while(temp -> next != NULL && temp -> next -> next != NULL){
temp = temp -> next;
}
delete temp -> next;
temp -> next = NULL;
return start;
}

node* Delete_node (node* start , int target){


if(start == NULL){
cout << "The Linked list is Empty";
return start;
}
if(start -> data == target){
Delete_beginning(start);
}
node* ptr = start;
while(ptr -> next != NULL && ptr -> next -> data != target){
ptr = ptr -> next;
}

if(ptr -> next == NULL){


cout << "The node is not founded !";
return start;
}

if(ptr -> next -> data == target){


node* temp = ptr -> next;
ptr -> next = ptr -> next -> next;
delete temp;
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;
}

-----------------------------------------------------------------------------------
-------------------------------------------------------

*********************** Stack ***********************

#include <iostream>
#define Size 10
using namespace std;

int arr[Size];
int Top = -1;

void push(int val){


if(Top == Size - 1)
cout << "The Stack is full !";
else
Top ++;
arr[Top] = val;
return;
}

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;
}

-----------------------------------------------------------------------------------
-------------------------------------------------------

*********************** Queue ***********************

#include <iostream>
#define Size 10
using namespace std;

int arr[Size];
int Front = -1;
int Rear = -1;

void Enqueu(int val){


if(Rear == Size - 1)
cout << "The Queue is full !";
else
Rear ++;
arr[Rear] = val ;
return;
}

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy