Structure of the Problem Requirements
Write a Code for Link List in C++. In which Provide following functionality.
1: Insert On Head
2: Insert On Tail
3: Display
4: Count
5: Remove From Head
6: Remove From Tail
7: Remove Target
SOURCE CODE
Main File:
#include <iostream>
#include "node.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
node *head=NULL;
double a=13;
system("color 3a");
char answer;
cout<<"\n\n\t************** Linked List ****************\n\n\n";
do
{
cout<<"Press 1 : Head Insert Number "<<endl;
cout<<"Press 2 : Tail Insert Number "<<endl;
cout<<"Press 3 : Display List "<<endl;
cout<<"Press 4 : Count Number "<<endl;
cout<<"Press 5 : Head Remove "<<endl;
cout<<"Press 6 : Tail Remove "<<endl;
cout<<"Press 7 : Remove Target "<<endl;
cout<<"Press Q : Exit"<<endl;
cout<<"\nChoice : ";
cin>>answer;
switch(answer)
{
case '1':
{
cout<<"\n Enter Entry to insert: ";
cin>>a;
list_head_insert(head,a);
break;
}
case '2':
{
cout<<"\n Enter Entry to insert: ";
cin>>a;
list_end_insert(head,a);
break;
}
case '3':
{
display(head);
break;
}
case '4':
{
cout<<"\n Count = "<<count(head)<<endl;
break;
}
case '5':
{
list_head_remove(head);
break;
}
case '6':
{
list_end_remove(head);
break;
}
case '7':
{
cout<<"\n Enter Target to Remove: ";
cin>>a;
remove_target(head,a);
break;
}
default:
{
cout<<"\n\n Invalid Option \n\n"<<endl;
break;
}
}
system("pause");
system("cls");
}while(tolower(answer)!='q');
return 0;
}
node.h:
#include <cstdlib>
class node
{
public:
typedef int value_type;
node(const value_type &init_data=0, node *init_link=NULL);
void set_data(const value_type &new_data) { data=new_data; }
value_type get_data() const { return data; }
void set_link(node *new_link) { link=new_link; }
const node* get_link() const { return link; }
node* get_link() { return link; }
private:
value_type data;
node *link;
};
void display(const node *head_ptr);
void list_head_insert(node*& head_ptr, const node::value_type& entry);
void list_end_insert(node*& head_ptr,const node::value_type& entry);
void list_head_remove(node*& head_ptr);
void list_end_remove(node*& head_ptr);
void remove_target(node*& head_ptr, node::value_type target);
int count(const node* head_ptr);
node.cpp:
#include "node.h"
#include <iostream>
using namespace std;
node::node(const value_type &init_data, node *init_link)
{
data=init_data;
link=init_link;
}
void display(const node *head_ptr)
{
const node *temp=head_ptr;
for(;temp!=NULL;temp=temp->get_link())
{
cout<<temp->get_data()<<endl;
}
}
int count(const node *head_ptr)
{
const node *temp=head_ptr;
int count=0;
for(;temp!=NULL;temp=temp->get_link())
{
count=count+1;
}
return count;
}
void list_head_insert(node *&head_ptr, const node::value_type& entry)
{
head_ptr = new node(entry,head_ptr);
/*node *insert_ptr,*head;
head=head_ptr;
insert_ptr=new node;
insert_ptr->set_data(entry);
insert_ptr->set_link(head);
head_ptr=insert_ptr;*/
}
void list_end_insert(node *&head_ptr, const node::value_type& entry)
{
node *temp;
node *insert_ptr=NULL;
temp=head_ptr;
while(temp->get_link()!=NULL)
{
temp=temp->get_link();
}
insert_ptr= new node(entry);
temp->set_link(insert_ptr);
}
void list_head_remove(node *&head_ptr)
{
if(head_ptr!=NULL)
{
node *remove_ptr = head_ptr;
head_ptr = remove_ptr ->get_link();
delete remove_ptr;
}
}
void list_end_remove(node *&head_ptr)
{
node *remove_ptr = head_ptr;
node *previous;
while(remove_ptr!=NULL)
{
if(remove_ptr->get_link() == NULL)
{
previous=remove_ptr;
}
remove_ptr = remove_ptr->get_link();
}
previous = new node(previous->get_data(),NULL);
delete remove_ptr;
}
void remove_target(node*& head_ptr, node::value_type target)
{
node *prev_ptr = head_ptr, *temp=head_ptr;
while(head_ptr!=NULL)
{
if(head_ptr->get_data()==target)
{
prev_ptr = head_ptr->get_link();
break;
}
prev_ptr = head_ptr;
head_ptr = prev_ptr -> get_link();
}
delete head_ptr;
head_ptr = temp;
}
Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.
He uses a class for the node, but doesn't use a class for the entire linked list... good shittyprogramming!
ReplyDelete