Comments

Tuesday, December 25, 2012

OOP: Linkist

Posted by at 8:49 AM Read our previous post
Object:


Create linklist of four digits taking input from the user.


Source Code :

#include <iostream>
using namespace std;

class Node{
    public:
        int data;
    Node * next;
    Node(int x)
        {
        data = x;
        next = NULL;
        }
    Node(int x, Node * y)
        {
        data = x;
        next = y;
        }
    };


class linkedList{
Node *head;
public:
    linkedList()
        {
        head = NULL;
        }
    void addNode(int value)
    {
        Node *ptr;
        if(head == NULL)
            head = new Node (value, NULL);
        else{
            ptr=head;
            while(ptr->next !=NULL)
                ptr=ptr->next;
            ptr->next = new Node (value, NULL);
            }
        }

    void print()
    {
        Node * ptr1;
        ptr1 = head;
       
        while(ptr1 != NULL)
        {
            cout << ptr1->data << "\n";
            ptr1 = ptr1->next;
        }
    }
};


void main()
{
//////////////////// simple crete link list //////////////
linkedList test;
int num;
int input;

cout<<"linklist of four digits \n";
for(num=0;num<4;num++)
{

cout<<"input value "<<num+1<<" : ";
cin>>input;
test.addNode(input);


}
test.print();

}
Output:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template