I am learning how to create my first linked list and trying different functions with popping/adding the first/last element from list.
When I try to pop the last item from list, I am unable to go to the 2nd last address of the list and pop the next address of the 2nd last address of the list which removes the last element.
node struct:
typedef struct Node
{
int data;
struct node* addressOfNextNode;
}node;
This is my linked list creator:
node * createLinkedList(int n)
{
int i = 0;
node * headNode = NULL;
node * address_node_to_be_inserted = NULL;
node * currentNode = NULL;
for(i=0;i<n;i )
{
//create individual isolated node
address_node_to_be_inserted = (node*)malloc(sizeof(node));
printf("\n Enter the data for node number:");
scanf("%d", &(address_node_to_be_inserted->data));
address_node_to_be_inserted->addressOfNextNode = NULL;
if(headNode == NULL) //if list is currently empty, then make address_node_to_be_inserted as first node
{
headNode = address_node_to_be_inserted;
printf("current node's data is %d and address is %p\n", headNode->data, headNode);
}
else
{
currentNode = headNode;
while(currentNode->addressOfNextNode != NULL)
{
currentNode = currentNode->addressOfNextNode;
}
currentNode->addressOfNextNode = address_node_to_be_inserted;
printf("current node's data is %d and address is %p\n", address_node_to_be_inserted->data, currentNode);
}
}
return headNode;
}
I would enter 5 nodes with data 1, 2, 3, 4, 5 and assign them to HEAD in the main function at the bottom of the post
and this is the function to try and remove the last element:
void remove_last(node * head)
{
//if there is only one item in the list, remove it
if (head->addressOfNextNode == NULL)
{
free(head);
}
node * currentNode = NULL;
currentNode = head;
while(currentNode->addressOfNextNode->addressOfNextNode != NULL)
{
currentNode = currentNode->addressOfNextNode;
}
currentNode->addressOfNextNode = NULL;
}
When I do currentNode->addressOfNextNode->addressOfNextNode, I get an error (error: dereferencing pointer to incomplete type 'struct node')
Why is that?
Since I couldn't do it in that format I did this instead:
void remove_last(node * head)
{
//if there is only one item in the list, remove it
if (head->addressOfNextNode == NULL)
{
free(head);
}
node * currentNode = NULL;
node * currentNode2 = NULL;
currentNode = head;
currentNode2 = head;
while(currentNode->addressOfNextNode != NULL) //reaches the last node
{
currentNode = currentNode->addressOfNextNode;
}
while(currentNode2->addressOfNextNode != currentNode) //reaches the 2nd last node by comparing it to != last node
{
currentNode2 = currentNode2->addressOfNextNode;
}
currentNode2->addressOfNextNode = NULL;
}
So this just iterates the list to the last node before NULL and get another iteration to compare to that last node before NULL to get to the 2nd last node.
My main function:
int main()
{
int n = 0;
node *HEAD = NULL;
printf("\n How many nodes?: "); //Asks user how many nodes to be created
scanf("%d", &n);
HEAD = createLinkedList(n); //input number of nodes in to Linked List creator
remove_last(HEAD);
return 0;
}
CodePudding user response:
Edit:
The compiler gives an error because there is a reference to struct node
before it has been defined.
typedef struct Node
{
int data;
struct node* addressOfNextNode; // <-- this is the error
} node;
In this statement, you haven't finished defining struct node
. The compiler is only aware of a struct Node
(notice the capital N). If you can correct it either by replacing that line with Node
:
struct Node* addressOfNextNode; // <-- renamed to Node
or by renaming the entire struct to node
, as so:
typedef struct node // <-- renamed to node
{
int data;
struct node* addressOfNextNode;
} node;
This StackOverflow question might be a useful link if you're interested.