-
Notifications
You must be signed in to change notification settings - Fork 0
/
LISTNODE.CPP
85 lines (83 loc) · 1.25 KB
/
LISTNODE.CPP
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct listnode {
char name[20];
int data;
listnode *next;
};
class linklist {
listnode *start;
public:
linklist() { start=NULL; }
~linklist();
void create();
void show();
};
/////////////////////////////////////////
void linklist::create()
{
char nam[20];
int n;
listnode *prev,*cur;
listnode *newnode;
newnode=new listnode;
cout<<"Enter your id: ";
cin>>n;
newnode->data=n;
cout<<"Enter your name: ";
cin>>nam;
strcpy(newnode->name,nam);
if (!start)
{
start=newnode;
start->next=NULL;
return;
}
prev=NULL;
cur=start;
while (cur && cur->data<newnode->data)
{
prev=cur;
cur=cur->next;
}
if (!prev)
{
newnode->next=start;
start=newnode;
}
else {
prev->next=newnode;
newnode->next=cur;
}
}
/////////////////////////////////////////
void linklist::show()
{
while (start) {
cout<<start->data<<"\t"<<start->name<<"\n";
start=start->next;
}
}
//////////////////////////////////////////
linklist::~linklist()
{
listnode *cur;
while (start)
{
cur=start;
start=start->next;
delete cur;
}
}
/////////////////////////////////////////
void main()
{
linklist list;
clrscr();
for (int i=0;i<2;i++)
list.create();
list.show();
//~linklist();
getch();
}