- Joined
- Nov 8, 2013
- Messages
- 12
i need to find if a linked list is empty, return head and tail and to find the length of the linked list, i need the menu to let me choose as many times as i want from it, and not to exit automatically
#include<iostream>
using namespace std;
void create_node();
void check_if_node_empty();
void return_head();
void return_tail();
int length();
void display();
struct node{
char name[15];
int age;
float height;
node *next;
};
node *head= 0;
void main(){
int option;
cout<<"---MENU---"<<endl;
cout<<"1. Create Node"<<endl;
cout<<"2. Check if linked list is empty"<<endl;
cout<<"3. Return head"<<endl;
cout<<"4. Return tail"<<endl;
cout<<"5. Calculate linked list length"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Choose: ";
cin>>option;
switch (option){
case 1:create_node();break;
case 2:check_if_node_empty();break;
case 3:return_head();break;
case 4:return_tail();break;
case 5:length();break;
case 6:display();break;
case 7:exit(0);break;
default: cout<<"Choose from 1 to 4";break;
}
system("pause");
}
void create_node(){
node *temp;
temp = new node;
cout<<"Please enter the following details"<<endl;
cout<<"Please enter name: ";
cin>>temp->name;
cout<<"Please enter age: ";
cin>>temp->age;
cout<<"Please enter height: ";
cin>>temp->height;
temp->next = 0;
}
void check_if_node_empty(){
cout<<"Checking if linked list is empty...."<<endl;
}
void return_head(){
cout<<"Returning head...."<<endl;
}
void return_tail(){
cout<<"Returning tail...."<<endl;
}
int length(){
cout<<"Calculating linked list length...."<<endl;
int len = 0;
while(head != 0)
{
len++;
head = head->next;
}
return len;
}
void display(){
cout<<"Displaying...."<<endl;
node *temp;
temp = head;
// Display details for what temp points to
cout<<"Name : "<<temp->name<<endl;
cout<<"Age : "<< temp->age<<endl;
cout<<"Height : "<< temp->height<<endl;
cout<<endl;
}
#include<iostream>
using namespace std;
void create_node();
void check_if_node_empty();
void return_head();
void return_tail();
int length();
void display();
struct node{
char name[15];
int age;
float height;
node *next;
};
node *head= 0;
void main(){
int option;
cout<<"---MENU---"<<endl;
cout<<"1. Create Node"<<endl;
cout<<"2. Check if linked list is empty"<<endl;
cout<<"3. Return head"<<endl;
cout<<"4. Return tail"<<endl;
cout<<"5. Calculate linked list length"<<endl;
cout<<"6. Display"<<endl;
cout<<"7. Exit"<<endl;
cout<<"Choose: ";
cin>>option;
switch (option){
case 1:create_node();break;
case 2:check_if_node_empty();break;
case 3:return_head();break;
case 4:return_tail();break;
case 5:length();break;
case 6:display();break;
case 7:exit(0);break;
default: cout<<"Choose from 1 to 4";break;
}
system("pause");
}
void create_node(){
node *temp;
temp = new node;
cout<<"Please enter the following details"<<endl;
cout<<"Please enter name: ";
cin>>temp->name;
cout<<"Please enter age: ";
cin>>temp->age;
cout<<"Please enter height: ";
cin>>temp->height;
temp->next = 0;
}
void check_if_node_empty(){
cout<<"Checking if linked list is empty...."<<endl;
}
void return_head(){
cout<<"Returning head...."<<endl;
}
void return_tail(){
cout<<"Returning tail...."<<endl;
}
int length(){
cout<<"Calculating linked list length...."<<endl;
int len = 0;
while(head != 0)
{
len++;
head = head->next;
}
return len;
}
void display(){
cout<<"Displaying...."<<endl;
node *temp;
temp = head;
// Display details for what temp points to
cout<<"Name : "<<temp->name<<endl;
cout<<"Age : "<< temp->age<<endl;
cout<<"Height : "<< temp->height<<endl;
cout<<endl;
}