-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog_wait.cpp
126 lines (99 loc) · 2.38 KB
/
prog_wait.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
using namespace std;
// A linked list node
struct Node
{
double bla[20000];
int data;
struct Node *next;
};
//insert a new node in front of the list
void
push (struct Node **head, int node_data)
{
/* 1. create and allocate node */
struct Node *newNode = new Node;
/* 2. assign data to node */
newNode->data = node_data;
/* 3. set next of new node as head */
newNode->next = (*head);
/* 4. move the head to point to the new node */
(*head) = newNode;
}
//insert new node after a given node
void
insertAfter (struct Node *prev_node, int node_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout << "the given previous node is required,cannot be NULL";
return;
}
/* 2. create and allocate new node */
struct Node *newNode = new Node;
/* 3. assign data to the node */
newNode->data = node_data;
/* 4. Make next of new node as next of prev_node */
newNode->next = prev_node->next;
/* 5. move the next of prev_node as new_node */
prev_node->next = newNode;
}
/* insert new node at the end of the linked list */
void
append (struct Node **head, int node_data)
{
/* 1. create and allocate node */
struct Node *newNode = new Node;
struct Node *last = *head; /* used in step 5 */
/* 2. assign data to the node */
newNode->data = node_data;
/* 3. set next pointer of new node to null as its the last node*/
newNode->next = NULL;
/* 4. if list is empty, new node becomes first node */
if (*head == NULL)
{
*head = newNode;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = newNode;
return;
}
// display linked list contents
void
displayList (struct Node *node)
{
//traverse the list to display each node
while (node != NULL)
{
// cout<<node->data<<"-->";
node = node->next;
}
}
/* main program for linked list*/
int
main ()
{
/* empty list */
struct Node *head = NULL;
// Insert 10.
append (&head, 10);
// getchar();
// Insert 20 at the beginning.
push (&head, 20);
// Insert 30 at the beginning.
push (&head, 30);
// Insert 40 at the end.
append (&head, 40); //
// Insert 50, after 20.
insertAfter (head->next, 50);
//cout<<"Final linked list: "<<endl;
displayList (head);
getchar();
//pause();
return 0;
}