forked from anrg0039/codeItDown-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12 may 2020 linked list
More file actions
62 lines (57 loc) · 1.06 KB
/
Copy path12 may 2020 linked list
File metadata and controls
62 lines (57 loc) · 1.06 KB
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
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
};
node *addnode(node *head,int val)
{
node *newnode=new node;
newnode->data=val;
if(head==NULL)
{
newnode->next=NULL;
return newnode;
}
newnode->next=head;
return newnode;
}
void print(node* head){
node *curr=head;
while(curr!=NULL)
{
cout<<curr->data<<" ";
curr=curr->next;
}
}
node* addnodeatlast(node* head,int val)
{
node *newnode=new node;
newnode->data=val;
newnode->next=NULL;
if(head==NULL)
{
return newnode;
}
node* curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=newnode;
return head;
}
int main() {
node* head=NULL;
head=addnode(head,10);
head=addnode(head,20);
head=addnode(head,30);
head=addnode(head,40);
head=addnodeatlast(head,50);
head=addnodeatlast(head,60);
head=addnodeatlast(head,70);
print(head);
// cout<<head->data<<" ";
// cout<<head->next->data<<" ";
}