-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.cpp
85 lines (61 loc) · 1.29 KB
/
linkedlist.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<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void traverselist(struct Node *n) //For traversing the list
{
while(n!=NULL)
{
cout << n->data << "\n";
n=n->next;
}
}
void insert(struct Node **n1,int new_data) //For inserting new node at start
{
struct Node* new_n=(struct Node*) malloc(sizeof(struct Node));
new_n->data=new_data;
new_n->next = (*n1);
(*n1) = new_n;
}
void insertlastnode(struct Node **n2 , int new_data1)
{
struct Node* new_n1=(struct Node*)malloc(sizeof(struct Node));
struct Node * last = *n2;
new_n1->data=new_data1;
new_n1->next=NULL;
while(last->next != NULL)
{
last=last->next;
}
last->next = new_n1;
return ;
}
void reverse(node*&head)
{
node*p;
node*c;
}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
struct Node *head = NULL;
struct Node *second =NULL;
struct Node *third=NULL;
head=(struct Node*)malloc(sizeof(struct Node));
second=(struct Node*)malloc(sizeof(struct Node));
third=(struct Node*)malloc(sizeof(struct Node));
head->data=20;
head->next=second;
second->data=25;
second->next=third;
third->data=30;
third->next=NULL;
insert(&head,45);
insertlastnode(&head,67);
traverselist(head);
return 0;
}