Posted by MindBreaker at 8:49 AM
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:
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: