-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListFuncs.cpp
executable file
·161 lines (119 loc) · 4.24 KB
/
linkedListFuncs.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <string>
#include <sstream> // for ostringstream
#include <cassert>
#include "linkedList.h"
#include "linkedListFuncs.h"
// Pre-condition:'a' is a valid array with
// zero or more elements, specified by 'size'
// Post-condition: A linked-list with 'size' number of nodes
// If size is zero, a valid 'empty' list is returned
LinkedList * arrayToLinkedList(int *a, int size) {
LinkedList *list = new LinkedList;
list->head = list->tail = NULL;
for(int i=0; i< size; i++){
Node* p = new Node;
p->data = a[i];
p->next = NULL;
if(i==0){
list->head = p;
}else{
list->tail->next = p;
}
list->tail = p;
}
return list;
}
// free up every node on this linked list
// Precondition: A non-NULL(valid) pointer to a linked-list on heap memory
// Postcondition: Every element of the linked-list is deleted
// The list structure is also deleted
void freeLinkedList(LinkedList * list) {
assert(list);//Program terminates is list is NULL (false)
Node *p, *q;
p= list->head;
while(p){
q=p;
p = p->next;
delete q;
}
delete list;
}
// Precondition: A valid linked-list on heap memory, which
// may be possibly empty
// Postcondition: Returns a string representation of the elements
// of the linked-list
std::string linkedListToString(LinkedList *list) {
std::string result="";
if(list == NULL)
return result;
for (const Node * p=list->head; p!=NULL; p=p->next) {
result += "[" + intToString(p->data) + "]->";
}
result += "null";
return result;
}
// Precondition: A valid linked-list that may possibly be empty
// and an integer value
// Postcondition: A new node with provided value is added to the
// the end of the list. The head and tail pointers are appropriately
// updated
void addIntToEndOfList(LinkedList *list, int value) {
assert(list!=NULL); // if list is NULL, we can do nothing.
Node *p; // temporary pointer
// TODO:
// (1) Allocate a new node. p will point to it.
p = NULL; // THIS IS PLACE-HOLDER LINE OF CODE. DELETE IT AND REPLACE IT.
// (2) Set p's data field to the value passed in
// (3) Set p's next field to NULL
if (list->head == NULL) {
// (4) Make both head and tail of this list point to p
} else {
// Add p at the end of the list.
// (5) The current node at the tail? Make it point to p instead of NULL
// (6) Make the tail of the list be p now.
}
}
// Precondition: A valid linked-list on the heap and an integer value
// Postcondition: A new node is created with provided value and is
// added to the start of the list. The head and tail pointers are
// appropriately updated
void addIntToStartOfList(LinkedList *list, int value) {
assert(list!=NULL); // if list is NULL, we can do nothing.
// Add code for this.
// HINTS:
// You will need to allocate a new Node.
// You will need two cases just as in addIntToEndOfList,
// one for when list->head is NULL and another for when it is not.
// You need to consider how to make sure that list->head
// so that it points to the new node that you allocated.
// And you will need to make sure that when you are done,
// that if the new node is now the ONLY thing on the list,
// that tail points to it also,
// and that the new node is pointing to NULL.
// Otherwise, you'll need to be sure that when you are done
// the head points to the new node you added, and that
// the new node points to the node that *used* to be the head.
// The order in which you do things matters.
}
// intToString converts an int to a string
std::string intToString(int i) {
// creates a stream like cout, cerr that writes to a string
std::ostringstream oss;
oss << i;
return oss.str(); // return the string result
}
// Precondition: A valid array 'a' with non-negative size
// Postcondition: A string representation of the elements of the array
std::string arrayToString(int a[], int size) {
std::ostringstream oss;
// fencepost problem; first element gets no comma in front
oss << "{";
if (size>0)
oss << intToString(a[0]);
for (int i=1; i<size; i++) {
oss << "," << intToString(a[i]);
}
oss << "}";
return oss.str();
}