This repository has been archived by the owner on Mar 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDS_LINKLIS03.cpp
85 lines (78 loc) · 1.68 KB
/
DS_LINKLIS03.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
// implementing linked list and its operations
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *next;
};
void insert(struct node **head, int data)
{
struct node *new_node=(struct node *)malloc(sizeof(struct node));
while(*head!=NULL)
{
head=&((*head)->next);
}
new_node->data=data;
new_node->next=NULL;
*head=new_node;
}
void print(struct node *head)
{
while (head != NULL)
{
cout << head->data << endl;
head = head->next;
}
}
void insert_before(struct node **head, int data, int before)
{
struct node *new_node=(struct node *)malloc(sizeof(struct node));
while(*head!=NULL)
{
if((*head)->data==before)
{
new_node->data=data;
new_node->next=*head;
*head=new_node;
break;
}
head=&((*head)->next);
}
}
void insert_after(struct node **head, int data, int after)
{
struct node *new_node=(struct node *)malloc(sizeof(struct node));
while(*head!=NULL)
{
if((*head)->data==after)
{
new_node->data=data;
new_node->next=(*head)->next;
(*head)->next=new_node;
break;
}
head=&((*head)->next);
}
}
int main(){
struct node *head = NULL;
int temp,temp2;
cin >> temp;
insert(&head, temp);
cin >> temp;
insert(&head, temp);
cin >> temp;
insert(&head, temp);
cin >> temp2;
cin >> temp;
insert_before(&head, temp2, temp);
cin >> temp;
insert(&head, temp);
cin >> temp;
insert(&head, temp);
cin >> temp2;
cin >> temp;
insert_after(&head, temp2, temp);
print(head);
}