-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist
58 lines (53 loc) · 997 Bytes
/
linkedlist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
class llist
{
private:
struct node //two pointers one for start and one for end position
{
char data;
node * next;
}*p,*q;
public:
llist()
{
p=new node;
q=p;
}
void add(char item)
{
q->data=item;
q->next=new node;
q=q->next;
}
void display()
{
node *r=p;
while(r->next!=NULL)
{
std::cout<<r->data;
r=r->next;
if(r->next!=NULL)
std::cout<<"->";
}
}
~llist()
{
while(p!=NULL)
delete p;
}
};
int main(int argc, const char * argv[])
{
char *s1=(char *)malloc(sizeof(char)*100);
std::cout<<"Enter the string:";
std::cin>>s1;//Input string
int i=0;
llist* obj=new llist();
while(s1[i]!='\0') //adds input data to linked list
{
obj->add(s1[i]);
i++;
}
obj->display();
return 0;
}