You are on page 1of 5

#include<iostream>

#include<conio.h>

using namespace std;

struct node

char name[20]; // Name of up to 20 letters

int age;

float height; // In metres

node *nxt;// Pointer to next node

};

struct node *start_ptr=NULL;

void add_begin();

void add_end();

void display();

void delete_start_node();

void delete_end_node();

int main(){

int op;

do{

cout<<"press 1) to add at the begining the list\n";

cout<<"press 2) to add at the end the list\n";

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

cout<<"press 4) to delete at begining of the list\n";

cout<<"press 5) to delete at end of the list\n";

cout<<"press 7) to Exit from this application\n";

cin>>op;

if(op==1){

add_begin(); }

else if(op==2){
add_end();}

else if(op==3){

display(); }

else if(op==4){

delete_start_node();}

else if(op==5){

delete_end_node();}

else{

cout<<"we will add more functionality soon.\n thank you for work with us";}

}while(op!=7);

void add_begin(){

node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

// temp->nxt = NULL;

// Set up link to this node

if (start_ptr == NULL){

start_ptr = temp;

temp->nxt=start_ptr; }

else

{ temp2 = start_ptr;

temp->nxt=start_ptr;
start_ptr=temp;

///temp2->nxt=start_ptr;

// Move to next link in chain

}}}

void add_end(){

node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data

temp = new node;

cout << "Please enter the name of the person: ";

cin >> temp->name;

cout << "Please enter the age of the person : ";

cin >> temp->age;

cout << "Please enter the height of the person : ";

cin >> temp->height;

//temp->nxt = NULL;

// Set up link to this node

if (start_ptr == NULL){

start_ptr = temp;

temp->nxt=start_ptr;}

else

{ temp2 = start_ptr;

// We know this is not NULL - list not empty!

while (temp2->nxt !=start_ptr){

temp2 = temp2->nxt;}

temp2->nxt=temp;

// temp->nxt=start_ptr;

// Move to next link in chain

}}

cout<<"\n insertion at the end sucess";


}

void display(){

node *temp;

//temp = start_ptr;

do{

if (start_ptr==NULL)

cout << "End of list" << endl;

else

temp = start_ptr;

// Display details for what temp points to

cout << "Name : " << temp->name << endl;

cout << "Age : " << temp->age << endl;

cout << "Height : " << temp->height << endl;

cout << endl; // Blank line

// Move to next node (if present)

temp = temp->nxt;

} while (temp->nxt!= start_ptr);}

void delete_start_node(){

node *temp;

if(start_ptr==NULL){

cout<<"\n The List Is Empty\n";}

else{

temp = start_ptr;

if(temp->nxt=start_ptr){

start_ptr=NULL;

delete temp;}

else{
start_ptr = start_ptr->nxt;

delete temp;}

cout<<"\n Deletion Sucess!!!";} }

void delete_end_node(){

node *temp,*temp2;

if(start_ptr==NULL){

cout<<"\n The List Is Empty\n"; }

else{

temp = start_ptr;

if(temp->nxt=start_ptr){

start_ptr=NULL;

delete temp; }

else{

while(temp->nxt!=start_ptr){

temp2=temp;

temp=temp->nxt; }

temp2->nxt=start_ptr;

delete temp; }

cout<<"\n Deletion Sucess!!!";}}

You might also like