My objective is to:
1. Create a linked list 014 (done)
2. Remove the first element (done)
3. Remove the last element (done)
4. Add another linked list of 09 at the beginning of the first linked list
5. Add another linked list of 09 after the first node having
value 7 in the current linked list.
I'm having trouble adding the linked list to the beginning. Here's my code so far:
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

/*
* File: main.cpp
* Author: wingning
*
* Basic linked list
*/
#include <cstdlib>
#include <iostream>
using namespace std;
/*
*
*/
// data element type, for now it is int
typedef int element_type;
// basic node
struct Node
{
element_type elem; // data part
Node * next; // pointer to the next node
Node * prev; // pointer to the previous node, not used for lab 6
};
// show the value of a singly linked list, one node per line
void show(Node * head){
Node * current = head > next;
if (current == NULL)
cout << "It is an empty list!" << endl;
int i=1;
while (current != NULL) {
cout << "node " << i << "\telem: " << '\t' << current > elem << "\tnode address value: " << current << "\tnext value: " << current > next << endl;
current = current>next;
i++;
}
}
int main(int argc, char** argv) {
const int size = 15;
Node * head;
head = new Node;
Node * current = head;
// making a linked list of size many nodes
for (int i = 0; i < size; i++){
current > next = new Node;
current = current > next;
current > elem = i;
}
// make sure the next field of the last node is NULL
current > next = NULL;
//step 1
current = head;
Node * p = head;
current = current > next;
p = current > next;
delete current;
current = NULL;
head > next = p;
current = head;
while(current > next > next != NULL)
{
current = current > next;
}
delete current > next;
current > next = NULL;
//end of step 1
show(head);
for (int i = 0; i < 10; i++){
current > next = new Node;
current = current > next;
current > elem = i;
}
// free all space
current = head > next;
delete head;
while (current != NULL) {
head = current;
current = current>next;
delete head;
}
system("PAUSE");
return 0;
}
 
Now I'm not sure how to go about adding the linked list. Every time I use the loop
1 2 3 4 5

for (int i = 0; i < size; i++){
current > next = new Node;
current = current > next;
current > elem = i;
}
 
I get a compiler error. It compiles and will output, but it gives me various error messages.
To add to the beginning of a linked list, do you create a pointer variable to store the head of the first linked list, create the second linked list, and make the last element of the second linked list point to the new pointer (that points to head) that you created? Is that how it's implemented? It doesn't seem to be working.
Any help is appreciated! Thank you!