I understand the logic when the struct Node *head is a global variable.
However, when I do the reverse using struct Node *head as a local variable in main(), I have to use double-pointer and pointer of Head, and I don't understand where exactly I have to place them.
void Reverse(struct Node *Head, struct Node **headpointer) {
struct Node *first;
// when the list is empty
if (Head == NULL)
return;
first = Head;
// when there is one node left
if (first->next == NULL) {
*headpointer = first;
return;
}
Reverse(first->next, headpointer);
first->next->next = first;
first->next = NULL;
}
I am unclear why I have to use...
first = Head;
*headpointer = first;
why can't I just use
Head = first?
Also, if first = Head line in front of the recursive function call Reverse(first->next, headpointer), don't the first->next value also equal to Head which points to the first node?
Is there any good logical diagram/pic/explanation/examples that can explain this difference?
I am unclear why I have to use... first = Head
Actually, the first variable isn't needed in this code at all. It is just a copy of Head, so to simplify you can just replace the variable with Head. Everything will work the same without it:
if (Head == NULL)
return;
if (Head->next == NULL) {
*headpointer = Head;
return;
}
Reverse(Head->next, headpointer);
Head->next->next = Head;
Head->next = NULL;
I am unclear why I have to use... *headpointer = Head
The Head variable is the pointer to the head of the list you want to reverse, and the function stores the head of the newly reversed list into *headpointer. It does this because Head is just a copy of the pointer that was passed in to the Reverse() function, so updating its value won't change the original pointer; that's why a separate double pointer variable is used.
why can't I just use Head = first?
Head is a copy of the pointer that was passed to the function. Updating the Head pointer will not update the original list pointer that you passed in. Also, as I said before, Head is the same as first, so an assignment like that does nothing.
Also, if first = Head line in front of the recursive function call Reverse(first->next, headpointer), don't the first->next value also equal to Head which points to the first node?
first->next (or Head->next) is just the next node in the list. The purpose of the function call is to reverse the rest of the list first, and then place Head (or first) at the end of the list (which is what the last two lines do).
Let's consider the function signature was like this:
void Reverse(struct Node* Head, struct Node* headpointer) {
And you call it like this
Reverse(myList);
myList is just an address to a Node, for example 0x1234. So it's equivalent to do:
Reverse(0x1234);
The address is copied to a new variable headpointer. When we modify headpointer we are modifying a local variable, not the myList we passed.
It's like if we did this:
struct Node* myList = 0x1234;
struct Node* headpointer = myList;
headpointer = 0xABCD;
// at this point myList is still 0x1234
So after the function returns, myList is still equal to 0x1234. That's not what we want because is should now point to the last node.
So how do we allow, the function to modify myList? We have to tell the function "hey, here's the address where you have to write to".
In C, in order to take the address of something we use the '&' operator:
Reverse(&myList);
And we must change the signature of the function accordingly:
void Reverse(struct Node* Head, struct Node** headpointer) {
Now headpointer is an address to an address to a Node. Or, as we say in C, a pointer to a pointer to a Node.
Here's a final example that can help understand.
struct Node* myList = 0x1234;
struct Node** headpointer = &myList; // now headpointer points to myList
*headpointer = 0xABCD;
// at this point myList is still 0xABCD
// we haven't changed headpointer, but the thing headpointer is pointing to!
It is necessary to be able to return the updated head node, but this could be done with a return value in lieu of using a double pointer.
Also, I believe that the function needs to be passed a pointer to the previous node so it can set that as the new next pointer.
So, I think your function would need three arguments (e.g.) cur, prev, headpointer.
If we return the head pointer, we can reduce this to two.
Here's a version that does that.
I've tried to annotate it as much as possible. I've added debug printf and some cross checking [which I used to debug it myself].
If you need to have a double pointer, this example could be adapted without too much trouble.
// revlist -- reverse singly linked list recursively
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node *next;
int data;
#ifdef CHECK
int visited; // 1=node visited (check for looped list)
#endif
} Node;
#ifdef DEBUG
int dbglvl; // nesting level -- debug only
#endif
int
prt(Node *node)
{
int data;
if (node != NULL)
data = node->data;
else
data = -1;
return data;
}
#ifdef DEBUG
#define dbgprt(_fmt ...) \
do { \
printf("DBG/%d: ",dbglvl); \
printf(_fmt); \
} while (0)
#else
#define dbgprt(_fmt ...) \
do { } while (0)
#endif
// reverse -- recursively reverse list
// RETURNS: pointer to head of reversed list
Node *
reverse(Node *cur,Node *prev)
// cur -- pointer to current node
// prev -- pointer to previous node
{
Node *next;
Node *head = NULL;
do {
// empty list
if (cur == NULL)
break;
next = cur->next;
dbgprt("BEG cur=%d prev=%d next=%d\n",
prt(cur),prt(prev),prt(next));
// at end of list -- set new head from tail node
if (next == NULL) {
head = cur;
head->next = prev;
dbgprt("SETHEAD head=%d head->next=%d\n",
prt(head),prt(head->next));
break;
}
#ifdef DEBUG
++dbglvl;
#endif
// process the next node and give the current node as the previous one
head = reverse(next,cur);
#ifdef DEBUG
--dbglvl;
#endif
// set the link pointer to the previous node
cur->next = prev;
dbgprt("POST cur->next=%d\n",prt(cur->next));
} while (0);
dbgprt("EXIT head=%d\n",prt(head));
return head;
}
// addnode -- add node to tail of list
Node *
addnode(Node *head,int data)
{
Node *newnode = malloc(sizeof(*newnode));
Node *cur;
Node *prev;
newnode->next = NULL;
newnode->data = data;
#ifdef CHECK
newnode->visited = 0;
#endif
// find the tail of the list
prev = NULL;
for (cur = head; cur != NULL; cur = cur->next)
prev = cur;
// add new node to list
if (prev != NULL)
prev->next = newnode;
else
head = newnode;
return head;
}
// list_print -- print list
void
list_print(Node *head,const char *reason)
{
Node *cur;
printf("%s:",reason);
for (cur = head; cur != NULL; cur = cur->next) {
printf(" %d",cur->data);
#ifdef CHECK
if (cur->visited) {
printf(" DUP\n");
exit(1);
}
cur->visited = 1;
#endif
}
printf("\n");
#ifdef CHECK
for (cur = head; cur != NULL; cur = cur->next)
cur->visited = 0;
#endif
}
// list_destroy -- destroy the list
void
list_destroy(Node *cur)
{
Node *next;
for (; cur != NULL; cur = cur->next) {
next = cur->next;
free(cur);
}
}
// dotest -- test reverse function
void
dotest(int max)
{
Node *head = NULL;
#ifdef DEBUG
dbglvl = 0;
#endif
// create a list
for (int idx = 1; idx <= max; ++idx)
head = addnode(head,idx);
// show the original list
list_print(head,"Forward");
// reverse the list
head = reverse(head,NULL);
// show the reversed list
list_print(head,"Reverse");
// destroy the test list
list_destroy(head);
}
int
main(void)
{
dotest(8);
return 0;
}
Here's the program output:
Forward: 1 2 3 4 5 6 7 8
Reverse: 8 7 6 5 4 3 2 1
Here's the program output with the debug printf enabled:
Forward: 1 2 3 4 5 6 7 8
DBG/0: BEG cur=1 prev=-1 next=2
DBG/1: BEG cur=2 prev=1 next=3
DBG/2: BEG cur=3 prev=2 next=4
DBG/3: BEG cur=4 prev=3 next=5
DBG/4: BEG cur=5 prev=4 next=6
DBG/5: BEG cur=6 prev=5 next=7
DBG/6: BEG cur=7 prev=6 next=8
DBG/7: BEG cur=8 prev=7 next=-1
DBG/7: SETHEAD head=8 head->next=7
DBG/7: EXIT head=8
DBG/6: POST cur->next=6
DBG/6: EXIT head=8
DBG/5: POST cur->next=5
DBG/5: EXIT head=8
DBG/4: POST cur->next=4
DBG/4: EXIT head=8
DBG/3: POST cur->next=3
DBG/3: EXIT head=8
DBG/2: POST cur->next=2
DBG/2: EXIT head=8
DBG/1: POST cur->next=1
DBG/1: EXIT head=8
DBG/0: POST cur->next=-1
DBG/0: EXIT head=8
Reverse: 8 7 6 5 4 3 2 1
Related
I am learning how to reverse a linked list recursively. I am confused with the last 4 lines.
node *reverse_linked_list_rec(node *head){
if (head->next==NULL){
return head;
}
node *smallans= reverse_linked_list_rec(head->next);
node *tail = head->next;
tail->next = head;
head->next = NULL;
return smallans;
}
Let's say I am reversing
1 2 3 NULL
by recursion, it reaches at 3 NULL and then by base case returns
2 3 NULL
here head=2, smallans=2 (not sure).
Why we are returning smallAns here and how it is changing?
smallans is a confusing variable name because it's actually the old tail being passed back through the list to become the new head which is ultimately returned to the caller.
Its next pointer changes when these lines execute in the parent function call:
// when head->next->next == NULL ...
node *tail = head->next; // ... `tail` points to the old tail (new head) ...
tail->next = head; // ... and this sets the new tail's next pointer to
// the old second-to-last node (new second node).
tail is a misleading name here--I associate a "tail" with a single node that terminates the entire list, not a previous node. new_prev or old_next seem more appropriate here depending on whether you want to name things relative to the node roles in the new list or the original list.
As a minor point, I recommend using if (!head || !head->next) to avoid a potential null pointer dereference.
I'd write the function as follows:
node *reverse_linked_list_rec(node *head) {
if (!head || !head->next) {
return head;
}
node *old_tail = reverse_linked_list_rec(head->next);
node *old_next = head->next;
old_next->next = head;
head->next = NULL;
return old_tail;
}
Aside from intellectual curiosity, recursion is a poor choice for linked list operations since it adds function call overhead, you can blow the stack and the logic isn't any easier to follow than iterative, in most cases.
Case in point, here's a complete example with an iterative version:
#include <stdio.h>
#include <stdlib.h>
struct node {
int id;
struct node *next;
};
struct node *make_node(int id) {
struct node *n = malloc(sizeof(*n));
if (!n) exit(1);
n->id = id;
n->next = NULL;
return n;
}
struct node *reverse_linked_list(struct node *head) {
struct node *prev = NULL;
for (struct node *curr = head; curr;) {
struct node *old_next = curr->next;
curr->next = prev;
prev = curr;
curr = old_next;
}
return prev;
}
void print_linked_list(struct node *head) {
for (; head; head = head->next) {
printf("%d->", head->id);
}
puts("");
}
void free_linked_list(struct node *head) {
while (head) {
struct node *tmp = head;
head = head->next;
free(tmp);
}
}
int main() {
struct node *head = make_node(1);
head->next = make_node(2);
head->next->next = make_node(3);
print_linked_list(head); // => 1->2->3->
head = reverse_linked_list(head);
print_linked_list(head); // => 3->2->1->
free_linked_list(head);
return 0;
}
As another minor point, since the linked list is being mutated I'd probably go for a header like void reverse_linked_list(struct node **head);. Otherwise, it seems too easy to call the non-void function, ignore the return value and wind up with a memory leak or crash when head in the caller scope (which has become a tail pointing to null) is dereferenced.
I'm trying to build a singly-linked list using a struct with 2 data types: char* and int, as well as next to point to other nodes of course.
I have two functions: addToList, and printList as well as the main method to run everything.
What my code is supposed to do is add a node after the head, and check to see if another node with the same data has already been added. If so, it does not add the new node, while incrementing the count data of the already-linked node.
Then, printList() prints the count data and the char* data of each node.
The first issue is that my char comparison doesn't seem to be working, since duplicate nodes are still added. Then, my printList function does not print the char* data correctly. Here's my code (I made sure to comment it so it's easy to follow along):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Struct for node. Has an extra data variable to keep track of which node is next
// in a singly-linked list.
typedef struct node {
char *str;
unsigned int count;
struct node *next;
} node;
// Creates a HEAD node with NULL data.
node *head = NULL;
// Adds a new node and populates the data.
struct node* addToList(struct node* Listptr, char* word){
struct node* new = malloc(sizeof(struct node));
new->str = malloc(sizeof(char) * 34);
strcpy(new->str, word);
new->count = 1;
new->next = Listptr;
// If the head is NULL, sets the new node as the head.
if(head == NULL){
head = new;
return new;
}
// Sets a node iterator "cur" to the first position.
node *cur = head;
// Sets a node iterator to be one place behind cur.
node *prev;
// Loops through the linked list, in order to count the previous words and determine
// if it is necessary to add another node.
// Otherwise, it sets cur to the next node, and prev to the node cur was just at.
while(cur != NULL){
if(cur->str == new->str){
cur->count++;
new = NULL;
return new;
} else{
prev = cur;
cur = cur->next;
}
}
// Checks to see if cur is NULL, if so, sets the previous node.next to the one we're adding.
if(cur == NULL){
prev->next = new;
return new;
}
}
// Prints out the count and word values of each node.
void printList(){
node* cur = head;
while(cur != NULL){
printf("%d %c\n", cur->count, cur->str);
cur = cur->next;
}
}
int main() {
node* Z = NULL;
char *a = "hello";
char *b = "world.";
char *c = "hello";
addToList(Z, a);
addToList(Z, b);
addToList(Z, c);
printList(Z);
return 0;
}
I expect to get:
2 hello
1 world
But in the console, I get:
1 l
1 (weird symbol)
1
Don't use == to compare strings rather use strcmp().
Change if (cur->str == new->str) to this:
if (strcmp(cur->str, new->str) == 0)
Read this to know more on string compare: How do I properly compare strings?
I have the following node structure in C:
#define SIZE 5
typedef struct node
{
int n;
struct node* next;
}node;
node* head = NULL;
void implement(int n); // create linked list
int length(void); // returns length of list
node* reverse(void); // reverses order of list
The list is implemented as follows:
void implement (int n)
{
for (int i = 0; i < n; i++)
{
node* new_node = malloc(sizeof(node));
if(new_node == NULL)
{
printf("out of memory!\n");
return;
}
new_node->n = i;
new_node->next = head;
head = new_node;
}
}
Which when printed to the terminal gives:
4 3 2 1 0
The head pointer is set to node 4.
I have written the following function to reverse the list as follows:
node* reverse(void)
{
node* cur = head;
node* next = NULL;
head = NULL;
while (cur->next != NULL)
{
next = cur->next;
cur->next = head;
head = cur;
cur = next;
}
return cur;
}
However, returning cur only gives:
0
Returning head provides:
1 2 3 4
So it is quite clear that the list is being reversed, but for some reason the last node (0) is not being linked to the remaining nodes. I am probably missing something obvious here, but can anyone give me any pointers? (no pun intended!)
Cheers
while (cur->next != NULL)
This stops when cur is at the last element (next is NULL); i.e. you don't process the last node.
Changing it to
while (cur != NULL)
and returning head should fix it.
So I have a self-made double-linked-list implementation that is being used as a stand in for a queuer. (implemented in C, a language that I am admittedly weak in).
my typedef for the node:
typedef struct Node
{
char *name;
int data;
int recurring;
struct Node *next;
struct Node *prev;
}node;
which says "a node has a name, a datapoint, whether it's recurring or not and pointers to the previous and next nodes"
the insertion function like so
node * insertFromTail(node *tail, int data, int recurring, char *name)
{
node *newNode;
node *oldNext;
node *origTail = tail;
/*assume *pointer points to tail of list*/
/*printf("tail data is %d\n", tail->data);
printf("before loop\n");*/
while(tail->prev != NULL && tail->data > data)
{
/*printf("inside while loop\n");*/
tail = tail -> prev;
}
/*printf("after loop\n");*/
/*if we are looking at a no item list or tail*/
if(tail->next == NULL)
{
/*printf("pointer is tail\n");*/
return insert(tail, data, recurring, name);
}
else /*tail pointer points at item before the point of insertion*/
{
/*printf("default case\n");
printf("pointer data is %d\n", tail->data);*/
oldNext = tail->next;
newNode = (node *)malloc(sizeof(node));
newNode->data = data;
newNode->recurring = recurring;
newNode->name = name;
oldNext -> prev = newNode;
newNode -> next = oldNext;
tail -> next = newNode;
newNode -> prev = tail;
return origTail;
}
}
with the internal insert
node * insert(node *tail, int data, int recurring, char *name)
{
/* Allocate memory for the new node and put data in it.*/
tail->next = (node *)malloc(sizeof(node));
(tail->next)->prev = tail;
tail = tail->next;
tail->data = data;
tail->recurring = recurring;
tail->name = name;
tail->next = NULL;
return tail;
}
which is passed the tail of the list, the data point, the time at which the next item will recur at and the name of the item.
if we start with a node that is empty and has NULL prev and next references (a dummy node), and I add three unique nodes with a function called ADD that calls insertFromTail taking input from stdIn
int main()
{
node *start,*temp,*tail;
start = (node *)malloc(sizeof(node));
temp = start = tail;
temp->next = NULL;
temp->prev = NULL;
if(strcmp(command, "ADD") == 0)
{
scanf("%d",&argTime);
scanf("%s",&argName);
tail = insertFromTail(head, argTime, 0, *argName);
}
}
with input as so:
INPUT:
ADD 10 Gin
ADD 20 Vodka
ADD 30 Rum
PRINT
I would get an output of
OUTPUT:
Rum 10
Rum 20
Rum 30
This is an error, as the desired output would be
OUTPUT:
Gin 10
Vodka 20
Rum 30
I have a feeling it has to do with how the string is passed into the node, but as you can see, I'm stumped. This is the last thing left on the assignment and everything else is working perfectly, so I decided to ask here to see if anyone can nudge me on the right path. Thanks for your help in advance :)
P.S. Sorry for bad everything, I'm sleep deprived :(
Short answer: you'll need to duplicate that name:
tail->name = strdup(name);
Longer answer: at each iteration you're storing the same pointer. You're storing it and then the next time you're writing to it again. So you end up with 3 identical pointers to whatever you input last.
A simple fix is to duplicate the string and store a copy: precisely what strdup does. But if your implementation lacks strdup, you can try:
tail->name = malloc(strlen(name) + 1);
strcpy(tail->name, name);
Don't forget to check for errors
Don't forget to free the memory at some point
I have a program in c which receives messages from different clients and servers. When the messages come in it adds the message to that list. After the message is added to the list I print it on the screen and on the other servers. But i want to delete the node that contains the message after it is printed so when the the print function gets called only prints the new messages. How can I delete the node after print?
Here is my struct:
typedef struct trade_list {
char* trader_msg;
u_int32_t id_of_sender;
int sender_timer;
int local_time;
struct trade_list *next;
}trade_list;
trade_list *head = NULL;
And here is how I print:
void print_trades()
{
trade_list * newnode = head;
trade_list *previous = NULL;
while (newnode) {
previous = newnode;
if ((elapsed - newnode->local_time >= 8))
printf ("%s\n", newnode->trader_msg);
newnode = newnode->next;
if (previous == NULL)
head = newnode->next;
else
{
previous->next = newnode->next;
free(newnode);
}
}
}
Thus gives me a segmentation fault. I tried changing the newnode->next to just newnode in the else part. previous->next = new node; It didn't give me an error but it did not erase the node as it kept printing that node every time the print function was called
At the start of your function:
trade_list *prev = NULL;
At each iteration in your loop, before newnode = newnode->next; add prev = newnode.
Then to delete:
if (prev == NULL) /* replace the head */
head = newnode->next;
else
prev->next = newnode->next;
/* free newnode? */
Fairly simple.
EDIT: You should really have some functions like list_create, list_add, list_remove associated with your data structure, rather than just chucking like the remove code into a print function. That's the first thing I do when I create any sort of data structure.
Another option is to have your linked list like:
typedef struct trade_node {
char* trader_msg;
u_int32_t id_of_sender;
int sender_timer;
int local_time;
struct trade_node *next;
} trade_node;
typedef struct trade_list {
trade_node *head;
/* trade_node *foot; ? */
size_t length;
} trade_list;
EDIT2: As for your edit, change print_trades to something like:
void print_trades()
{
trade_list *prev = NULL, *next;
trade_list *newnode = head;
while (newnode) {
if ((elapsed - newnode->local_time >= 8)) {
printf ("%s\n", newnode->trader_msg);
/* temp variable for newnode->next */
next = newnode->next;
if (prev == NULL) /* replace the head */
head = next;
else
prev->next = next;
/* free newnode->trader_msg? */
free(newnode);
/* don't update prev */
newnode = next;
}
else {
prev = newnode;
newnode = newnode->next;
}
}
}
In your code, previous will never be NULL since you set it to newnode at the start of the loop, and newnode also shouldn't equal newnode->next until after newnode has been processed completely. You're also using newnode after you've freed it.
Instead of having print_trades depend on the global variable head, make it receive this 'head' as an argument and print from there. It's much better design wise.
void print_trades(trade_list* head){ ... }
Then if you are adding the new nodes to the end of the list, you can print it starting from the first new one. If you are not using this information other than to print them, then there is no need to store them in a global list. Just use a local list within the function that receives them, and print that list.
Deleting a list is usually done by calling free(ptr) on each of the pointers. Since you know how to add nodes to the list, you will know how to inverse that appropiately.
Take some time to review Linked List handling and deletion of items from linked lists... once your "print" executes just do a delete...
Basically you want to have some trade_list Pointer objects that point to the node to-be deleted and the previous node. Have the previous node's "next" point to the node-to-be-deleted's next and then free the memory on the deleted node...
Start...
NODE1->NODE2->NODE3
^ ^
| |
*temp1 *temp2
temp1 = NODE1;
temp2 = NODE2; //or temp2 = temp1->next;
Next...
+-------------+
| V
NODE1 NODE2->NODE3
^ ^
| |
*temp1 *temp2
temp1->next = temp2->next;
free(temp2);
After...
NODE1-------->NODE3
//temp1 still = NODE1
//temp2 = null
Have the pointer objects iterate through the list with your while loop as you go so you don't end up out of sync. Also be sure to check for null