#include
using namespace std;
struct node
{
char name[20];
int age;
node *next;
};
node *start_ptr=NULL;
node *current;
void add_node_end()
{
node *temp,*temp2;
temp = new node;
cout<<"name: ";cin>>temp->name;
cout<<"age: ";cin>>temp->age;
temp->next=NULL;
if (start_ptr==NULL)
{
start_ptr=temp;
current=start_ptr;
}
else
{
temp2 = start_ptr;
while (temp2->next!=NULL)temp2 = temp2->next;
temp2->next=temp;
}
}
void add_node_start()
{
node *temp,*temp2;
temp= new node;
cout<<"name: ";cin>>temp->name;
cout<<"age: ";cin>>temp->age;
if (start_ptr==NULL)
{
start_ptr=temp;
temp->next=NULL;
}
else
{
temp2 = start_ptr;
start_ptr=temp;
start_ptr->next=temp2;
}
current=start_ptr;
}
void remove_node_start()
{
node *temp;
temp=start_ptr;
if (start_ptr==NULL)cout<<"no node in your list.\n";
else
{
start_ptr=start_ptr->next;
delete temp;
}
}
void remove_node_end()
{
node *temp,*temp2;
temp = start_ptr;
if (start_ptr==NULL)cout<<"no node in your list.\n";
else
{
if (temp->next==NULL)
{
delete temp;
start_ptr=NULL;
}
else
{
while (temp->next!=NULL)
{
temp2=temp;
temp=temp->next;
}
delete temp;
temp2->next=NULL;
}
}
}
void move_curr_forw()
{
if (current->next==NULL)cout<<"current pointing to last node.\n";
else
{
current=current->next;
}
}
void move_curr_backw()
{
node *prev;
if (current==start_ptr)cout<<"current pointing to start node.\n";
else
{
prev = start_ptr;
while (prev->next!=current)prev=prev->next;
current=prev;
}
}
void remove_after_curr()
{
node *temp;
if (current->next==NULL)cout<<"current pointing to last node.\n";
else
{
temp=current->next;
current->next=temp->next;
delete temp;
}
}
void add_after_curr()
{
node *temp,*temp2;
temp = new node;
cout<<"name: ";cin>>temp->name;
cout<<"age: ";cin>>temp->age;
if (current->next==NULL)
{
current->next=temp;
temp->next=NULL;
}else
{
temp2 = current->next;
current->next=temp;
temp->next=temp2;
}
}
void change_data_curr()
{
cout<<"name: ";cin>>current->name;
cout<<"age: ";cin>>current->age;
}
void show_current()
{
cout<<"\nname: "<
cout<<"age: "<
}
void show_all()
{
node *temp;
temp=start_ptr;
cout<
do
{
if (temp==NULL)cout<<"end list"<
else
{
cout<<"name: "<
cout<<"age: "<
temp=temp->next;
}
}while (temp!=NULL);
}
int main()
{
int choose;
cout<<"|==========================================|"<
cout<<"| Single Link List |"<
cout<<"|==========================================|"<
do
{
cout<<"1. Exit program.\n";
cout<<"2. Add node at the end list.\n";
cout<<"3. Add node at the start list.\n";
cout<<"4. Remove node at the start list.\n";
cout<<"5. Remove node at the end list.\n";
cout<<"6. Move current pointer forward 1 node.\n";
cout<<"7. Move current pointer backward 1 node.\n";
cout<<"8. Remove 1 node after current.\n";
cout<<"9. Add 1 node after current.\n";
cout<<"10. Change data in the node pointed by current.\n";
cout<<"11. Show the node data pointed by current.\n";
cout<<"12. Show all data in the list.\n";
cout<<"Choose the option using index number: ";cin>>choose;
switch (choose)
{
case 2:add_node_end();break;
case 3:add_node_start();break;
case 4:remove_node_start();break;
case 5:remove_node_end();break;
case 6:move_curr_forw();break;
case 7:move_curr_backw();break;
case 8:remove_after_curr();break;
case 9:add_after_curr();break;
case 10:change_data_curr();break;
case 11:show_current();break;
case 12:show_all();break;
}
}while (choose!=1);
}
0 comments:
Posting Komentar