(Traverse ) Link List in c++ Part two

2) Traverse Or Read Data From Link List

Now we want to see the information stored inside the linked list. We create node *temp1. Transfer the address of*head to *temp1. So *temp1 is also pointed at the front of the linked list. Linked list has 3 nodes.
We can get the data from first node using temp1->data. To get data from second node, we shift *temp1 to the second node. Now we can get the data from second node.

while(temp!=NULL)
      {
                     
     
      cout << temp->data<<"  ";
       temp=temp->next;        
           
      }

image007.gif
This process will run until the linked list’s next is NULL.

 Combine Example of Part One And Part Two:

#include<iostream>
#include<conio.h>
using namespace std;

typedef struct node
{
int data;
node *next;
        
        
};

int main()
{    
     
      node *head = NULL;    // because   Link List is empty.....
    int linkdata;
     char ch;
   do {
   
  
    cout << "\n\nEnter Your Choice... "<< endl;
    cout << "1 For insert data " << endl;
    cout << "2 For read Data....." << endl<<endl;
     cout << "......................................." << endl<<endl;
     cin >> ch;
   
        switch(ch)
        {
    
    case '1':
    cout << "Enter Number ....";
    cin >> linkdata;
  
    // first creat temp memory for store data.......
    
    node *temp;
    temp = (node*)malloc(sizeof(node));
    temp->data=linkdata;
    temp->next=head;
    head=temp;
    
      cout << "your Value succesfully Stored in LinkList \n\nYour Value is ....." << linkdata << endl;
      break;
      case '2':
           if(temp==NULL)
           {
                         cout <<"sorry  link list is empty....." << endl;
                         
           }
      else
       cout <<"data in link list is : ";
      while(temp!=NULL)
      {
                     
     
      cout << temp->data<<"  ";
       temp=temp->next;        
           
      }
      break;
      }
      }
      while(ch!='0');
      getch();
}