#include<iostream>
using namespace std;
struct Node
{
int val; Node *next;
}*q;
int main()
{
Node *p, *t; int num;
int loopCount = 0; int maxCount = 5;
cout<<"Enter "<<maxCount<<" numbers for your linked list."<<endl;
while(loopCount!=maxCount)
{
loopCount++;
cin>>num;
cout<<endl;
if(q == NULL)
{
q = new Node;
q->val = num;
q->next = NULL;
}
else
{
p = q;
while(p->next!=NULL)
p = p->next;
t = new Node; t->val = num; t->next = NULL; //set next node to NULL
p->next = t;
}
}
cout<<"Here is your linked list"<<endl;
cout<<endl;
for(p = q; p != NULL; p = p->next) {
cout<<p->val<<endl;
}
cout<<endl;
cin.get();
return 0;
}
Tidak ada komentar:
Posting Komentar