Monday, 25 August 2014

Singly Linked List traversing

//#include "stdafx.h"
#include <iostream>
using namespace std;
struct node
{                                                              
      int data;            
      node *next;          
};
node *head;
int printList(node *ptr)
{
    if (ptr->next == NULL)
{
        return -1;
    }
    ptr=ptr->next;
    printList(ptr);
    cout <<ptr->data;
    return 0;
}
int main()
{
    node *ptr1 = NULL;
    node *ptr2 = NULL;    
    for (int i = 0; i < 10; i++)
{
        ptr1 = new node;
        cin>> ptr1->data;
        if (ptr2 == NULL)
{
            ptr2 = ptr1;
        }
        if (head != NULL)
{
            head->next =ptr1;
        }
        head =ptr1;
        head->next = NULL;
    }
    head=ptr2;
    printList(head);
    return 0;
}

No comments:

Post a Comment