forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.cpp
72 lines (64 loc) · 1.42 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
#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
void push ( struct Node** head, int nodeData )
{
struct Node* newNode1 = new Node;
newNode1 -> data = nodeData;
newNode1 -> next = (*head);
(*head) = newNode1;
}
void insertAfter ( struct Node* prevNode, int nodeData )
{
if ( prevNode == NULL )
{
cout << "the given previous node is required,cannot be NULL";
return;
}
struct Node* newNode1 =new Node;
newNode1 -> data = nodeData;
newNode1 -> next = prevNode -> next;
prevNode -> next = newNode1;
}
void append ( struct Node** head, int nodeData )
{
struct Node* newNode1 = new Node;
struct Node *last = *head;
newNode1 -> data = nodeData;
newNode1 -> next = NULL;
if ( *head == NULL )
{
*head = newNode1;
return;
}
while ( last -> next != NULL )
last = last -> next;
last -> next = newNode1;
return;
}
void displayList ( struct Node *node )
{
while ( node != NULL )
{
cout << node -> data << "-->";
node = node -> next;
}
if ( node== NULL)
cout<<"null";
}
int main ()
{
struct Node* head = NULL;
append ( &head, 15 );
push ( &head, 25 );
push ( &head, 35 );
append ( &head, 45 );
insertAfter ( head -> next, 55 );
cout << "Final linked list: " << endl;
displayList (head);
return 0;
}