Provide the pointer-based implementation for singly list
Q->U->E->S->T->I->O->N->S->Null
#include<iostream>
using namespace std;
struct Node
{
char ch;
Node* next;
Node(char _ch):ch(_ch),next(NULL){}
};
class LinkList
{
Node* head=NULL;
public:
void AddNode(char c)
{
Node* temp=new Node(c);
if(head==NULL)
head=temp;
else
{
Node* p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=temp;
}
}
void Display()
{
Node* p=head;
while(p!=NULL)
{
cout<<p->ch<<"->";
p=p->next;
}
cout<<"Null";
}
};
int main()
{
LinkList a;
a.AddNode('Q');
a.AddNode('U');
a.AddNode('E');
a.AddNode('S');
a.AddNode('T');
a.AddNode('I');
a.AddNode('O');
a.AddNode('N');
a.AddNode('S');
a.Display();
}
Comments
Leave a comment