Data Structure Lab 7



Source Code:

#include <iostream>
using namespace std;
struct Node{
int id;
Node*next;
// Node*head = NULL;

};
Node* head = NULL;
Node* temp=NULL;
//menu
void menu()
{
cout << "***************************************** \n";
cout << "1: Insert an Element at front \n";
cout << "2: delete an Elemnet from end \n";
cout << "3: Search an element \n";
cout << "4: Display list \n";
cout << "5: Is List empty \n";
cout << "6: Exit \n ";
cout << "***************************************** \n";
cout<< " Enter Your  choice: ";



}
//insert at start
void InsertAtStart(int x)
{
Node*newnode = new Node;
newnode->id = x;
newnode->next = NULL;
if (head == NULL)
{
head = newnode;
cout<<"Value is entered in linked list \n ";
}
else
{
newnode->next = head;
head = newnode;
cout<<"Value is entered in linked list \n ";

}

}
//del from end
void DeleteFromEnd(){
temp =  head;
Node*temp2;
while (temp->next != NULL)
{
temp2 = temp;
temp = temp->next;
}
temp2 -> next = NULL;
delete temp;
cout<<"Value is deleted from linked list \n ";

}
//search an element
void SearchAnElemnet(int value)
{
bool flag = false;
temp = head;
if (temp == NULL)
{
cout << "LInked List is EMpty\n";
}
else
{
while (temp->next != NULL)
{
if (temp->id == value){
cout << "VALue is found \n";
flag = true;
break;
}
else
{
temp = temp->next;

}
}
if(flag==false){
if (temp->id == value)
{
cout << "VALue is found \n";
flag = true;
}
}
if (flag == false)
{
cout << "Value is not found \n";
}
}

}
//display
void Display( )
{
temp = head;
if (temp == NULL)
{
cout << "LInked List is EMpty\n";
}
else
{
while (temp->next != NULL)
{
cout<<temp->id<<" ";
temp = temp->next;

}
}
cout<<temp->id<<" "<<endl;
}
//empty or not
void isempty()
{
 temp=head;
if (temp == NULL)
{
cout << "LInked List is EMpty\n";
}
else
cout << "Not empty \n";
}
int main()
{

int choice,value;

do
{

menu();
cin >> choice;
switch (choice)
{
case 1:{
   cout << "ENter VAlue: ";
   cin >> value;
   InsertAtStart(value);
   break;
}
case 2:
{
  DeleteFromEnd();

  break;

}
case 3:
{
  cout << "Search Value: ";
  cin >> value;
  SearchAnElemnet(value);
  break;
}
case 4:
{
  Display();
  break;
}
case 5:
{
  isempty();
  break;
}
case 6:
{
cout<<" Thank you!! \n";
break;
}
default:{
cout << "Invalid output \n";
break; }
}
} while (choice != 6);

system("pause");

}

Comments

Popular posts from this blog

Computer Programming Lab 5

Computer Programming lab 11

Computer Programming Lab 4