-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
106 lines (93 loc) · 2.51 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// A complete working C program to demonstrate all insertion methods
// on Linked List
#include <stdio.h>
#include <stdlib.h>
using namespace std;
// A linked list node
struct Node
{
int data;
struct Node *next;
};
/* Given a reference (pointer to pointer) to the head of a list and
an int, inserts a new node on the front of the list. */
void push(struct Node** head_ref, int new_data)
{
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
if(*head_ref == NULL)
{
*head_ref = new_node;
(*head_ref)->next = NULL;
}
else
{
new_node->next = *head_ref;
*head_ref = new_node;
}
}
/* Given a node prev_node, insert a new node after the given
prev_node */
void insertAfter(struct Node* prev_node, int new_data)
{
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref, int new_data)
{
struct Node *new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
if(*head_ref == NULL)
{
*head_ref = new_node;
(*head_ref)->next = NULL;
}
else
{
struct Node *temp = *head_ref;
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
new_node->next = NULL;
}
}
// This function prints contents of linked list starting from head
void printList(struct Node *node)
{
if(node == NULL)
{
printf("list is empty\n");
}
else
{
struct Node *temp = node;
while(temp != NULL)
{
printf("%d\n",temp->data);
temp = temp->next;
}
}
}
/* Driver program to test above functions*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
// Insert 6. So linked list becomes 6->NULL
append(&head, 6);
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
push(&head, 7);
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
push(&head, 1);
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
append(&head, 4);
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);
printf("\n Created Linked list is: ");
printList(head);
return 0;
}