patterncppMinor
Simulating a linear linked list using an array
Viewed 0 times
lineararraysimulatingusinglistlinked
Problem
A program to simulate a linear linked list using an array:
Specs:
A y : Create a new node with data value y, and append this node to L
I x y : Find the node t with value x, create a new node p with data
value y, and insert node p after t in L
D y : Find the node with data value y, and delete that node from L
R : Reverse L
T : Output all data values in L
Sample Input/Output:
My implementation:
```
// A program to simulate a linear linked list using an array
/***/
#include
#include
using namespace std;
struct node {
int data;
int prev;
int next;
// Constructor:
node() {
data = -1;
prev = -1;
next = -1;
}
};
node A[100]; // Array of nodes used to simulate the linked list
int slot = 1; // Array index of the next free space (beginning from index 1 initially)
int head = -1; // Array index of the starting element in the list (-1 implies no entries yet)
int tail = -1; // Array index of the tail in the list
int elements = 0; // Number of elements in the list
// Function prototypes:
void insert(int);
void insertAfter(int,int);
void deleteElement(int);
void reverse();
void print();
int findIndex(int);
int main() {
int a, b;
char c;
cout > c;
switch(c) {
case 'A': cin >> a;
insert(a);
break;
case 'I': cin >> a >> b;
insertAfter(a,b);
break;
case 'D': cin >> a;
deleteElement(a);
break;
case 'R': reverse();
cout << "\nLinked list successfully reversed\n\n";
break;
Specs:
A y : Create a new node with data value y, and append this node to L
I x y : Find the node t with value x, create a new node p with data
value y, and insert node p after t in L
D y : Find the node with data value y, and delete that node from L
R : Reverse L
T : Output all data values in L
Sample Input/Output:
A 5
A 1
I 5 4
I 1 9
A 7
I 9 8
D 9
D 8
T
5 4 1 7
R
T
7 1 4 5My implementation:
```
// A program to simulate a linear linked list using an array
/***/
#include
#include
using namespace std;
struct node {
int data;
int prev;
int next;
// Constructor:
node() {
data = -1;
prev = -1;
next = -1;
}
};
node A[100]; // Array of nodes used to simulate the linked list
int slot = 1; // Array index of the next free space (beginning from index 1 initially)
int head = -1; // Array index of the starting element in the list (-1 implies no entries yet)
int tail = -1; // Array index of the tail in the list
int elements = 0; // Number of elements in the list
// Function prototypes:
void insert(int);
void insertAfter(int,int);
void deleteElement(int);
void reverse();
void print();
int findIndex(int);
int main() {
int a, b;
char c;
cout > c;
switch(c) {
case 'A': cin >> a;
insert(a);
break;
case 'I': cin >> a >> b;
insertAfter(a,b);
break;
case 'D': cin >> a;
deleteElement(a);
break;
case 'R': reverse();
cout << "\nLinked list successfully reversed\n\n";
break;
Solution
-
For assigning data members in a constructor, it's more common to use an initializer list:
Moreover, you don't really need to initialize
-
Try to avoid global variables unless they're absolutely necessary. In this case, you can wrap them in a
-
Don't use single-character variable names:
It shouldn't be up to the reader to deduce the usage, plus you may eventually forget this as well. Always use meaningful names for variables to avoid this.
For loop counter variables, on the other hand, single letters are okay (and would be initialized within the
-
There's no need for
-
Error messages such as "no elements to delete" or "no free space available" should be printed to
-
If you're going to hold the user to a static array size, then you should also state this size to the user. A "no free space" warning may instead scare the user into thinking that there's no more memory available to use.
-
There is some duplication in
-
Don't put that first print statement in
-
Whether or not you're allowed to use library functions, do know that you can just have
Note:
For assigning data members in a constructor, it's more common to use an initializer list:
node()
: data(-1)
, prev(-1)
, next(-1)
{}Moreover, you don't really need to initialize
data as well. It may even be misinterpreted as an actual data value at the start, which you may not want.-
Try to avoid global variables unless they're absolutely necessary. In this case, you can wrap them in a
class and make each variable private. You can then probably have that node struct within that class.-
Don't use single-character variable names:
int a, b;
char c;It shouldn't be up to the reader to deduce the usage, plus you may eventually forget this as well. Always use meaningful names for variables to avoid this.
For loop counter variables, on the other hand, single letters are okay (and would be initialized within the
for loop anyway).-
There's no need for
return 0 at the end of main(). This will be done automatically from just this function.-
Error messages such as "no elements to delete" or "no free space available" should be printed to
std::cerr instead.-
If you're going to hold the user to a static array size, then you should also state this size to the user. A "no free space" warning may instead scare the user into thinking that there's no more memory available to use.
-
There is some duplication in
deleteElement(). Since each case will end with a return, use if/else if (starting with the second if) and remove each return statement starting from there. With that, you can just have one elements-- at the very end.-
Don't put that first print statement in
print(). Users may not expect that and not even want it. If it's desired, then they'll put it in their own driver file. In addition, replace the endls with '\n' so that they're not forced to have the buffer flushed as well.-
Whether or not you're allowed to use library functions, do know that you can just have
reverse() call std::reverse():void reverse() {
std::reverse(std::begin(A), std::end(A));
}Note:
std::begin()/std::end() are under `` and require C++11.Code Snippets
node()
: data(-1)
, prev(-1)
, next(-1)
{}int a, b;
char c;void reverse() {
std::reverse(std::begin(A), std::end(A));
}Context
StackExchange Code Review Q#81903, answer score: 5
Revisions (0)
No revisions yet.