c linked list print backwards - c

I am trying to make a doubly linked list that loops around, so the last link is connected to the first one.
However, I cannot figure out what I am doing wrong with the backlink, as I can print my list forwards but not backwards.
Any tip/help would be much appreciated.
This is my structure definition:
struct NODE {
union {
int nodeCounter;
void *dataitem;
} item;
struct NODE *link;
struct NODE *backlink;
};
//function to create a list
struct NODE *InitList() {
struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
temp->item.nodeCounter = 0;
temp->link = NULL;
temp->backlink = NULL;
return temp;
}
This is my insert function:
void Add2List(struct NODE *start, struct NODE *NewNode) {
struct NODE *current = start;
while (current->link != NULL && current->link != start) {
current = current->link;
}
current->link = NewNode;
NewNode->link = start;
NewNode->backlink = current;
start->backlink = NewNode;
start->item.nodeCounter++;
}
and this is my print backwards function:
void PrintBackwards(struct NODE *start) {
struct NODE * current = start;
while(current->backlink != start) {
DisplayNode((struct inventory*)current->item.dataitem);
current = current->backlink; //go one node back
}
}

The rest of your functions look reasonable but there are least two mistakes in your PrintBackwards function.
If you had intended to print it starting at the end, you should be starting at start->backlink, not at start.
You should not be checking for NULL in the while loop because your list is circular, so there should not be NULL.
The code below fixes those two mistakes, but I am not sure if there are others.
void PrintBackwards(struct NODE *start)
{
if(start == NULL || start->backlink == NULL)
return;
struct NODE * current = start->backlink;
while(current->backlink != start)
{
DisplayNode((struct inventory*)current->item.dataitem);
current = current->backlink; //go one node back
}
}

Maybe....
while(current->backlink != start)
{
DisplayNode((struct inventory*)current->item.dataitem); //dangerous
current = current->backlink; //go one node back
}

Related

Removing unique elements in a doubly linked list in C

I need a little help removing unique characters in a doubly linked list in C. So here's the logic I tried implementing: I counted the occurrence of each character in the doubly linked list. If it's occurrence is 1 time, then it is unique element and needs to be deleted. I'll be repeating the process for all elements. But my code in remove_unique_dll() function isn't working properly, please help me fix it. Here's my code-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *next;
struct node *prev;
};
struct node *head, *tail = NULL; //Represent the head and tail of the doubly linked list
int len;
void addNode(char data)
{
struct node *newNode = (struct node*) malloc(sizeof(struct node)); //Create new node
newNode->data = data;
if (head == NULL)
{ //If dll is empty
head = tail = newNode; //Both head and tail will point to newNode
head->prev = NULL; //head's previous will point to NULL
tail->next = NULL; //tail's next will point to NULL, as it is the last node of the list
}
else
{
tail->next = newNode; //newNode will be added after tail such that tail's next points to newNode
newNode->prev = tail; //newNode's previous will point to tail
tail = newNode; //newNode will become new tail
tail->next = NULL; //As it is last node, tail's next will point to NULL
}
}
void remove_unique_dll()
{
struct node *current = head;
struct node *next;
struct node *prev;
int cnt;
while (current != NULL)
{
next = current->next;
cnt = 1;
//printf("!%c ",next->data);
while (next != NULL)
{
if (next->data == current->data)
{
cnt += 1;
next = next->next;
}
else
next = next->next;
//printf("#%c %d %c\n",next->data,cnt,current->data);
}
if (cnt == 1)
{
prev = current->prev;
//printf("#%c %d",prev->data,cnt);
if (prev == NULL)
{
head = next;
}
else
{
prev->next = next;
}
if (next == NULL)
{
tail = prev;
}
else
{
next->prev = prev;
}
}
current = current->next;
//printf("#%c ",current->data);
}
head = current;
}
void display()
{
struct node *current = head; //head the global one
while (current != NULL)
{
printf("%c<->", current->data); //Prints each node by incrementing pointer.
current = current->next;
}
printf("NULL\n");
}
int main()
{
char s[100];
int i;
printf("Enter string: ");
scanf("%s", s);
len = strlen(s);
for (i = 0; i < len; i++)
{
addNode(s[i]);
}
printf("Doubly linked list: \n");
display();
remove_unique_dll();
printf("Doubly linked list after removing unique elements: \n");
display();
return 0;
}
The output is like this-
If you uncomment the printf() statements inside remove_unique_dll() you'll notice that no code below inner while loop is being executed after inner while loop ends. What's the issue here and what's the solution?
Sample input- aacb
Expected output- a<->a<->NULL
Some issues:
You shouldn't assign head = current at the end, because by then current is NULL
The next you use in the deletion part is not the successor of current, so this will make wrong links
As you progress through the list, every value is going to be regarded as unique at some point: when it is the last occurrence, you'll not find a duplicate anymore, as your logic only looks ahead, not backwards.
When you remove a node, you should free its memory.
Not a big issue, but there is no reason to really count the number of duplicates. Once you find the first duplicate, there is no reason to look for another.
You should really isolate the different steps of the algorithm in separate functions, so you can debug and test each of those features separately and also better understand your code.
Also, to check for duplicates, you might want to use the following fact: if the first occurrence of a value in a list is the same node as the last occurrence of that value, then you know it is unique. As your list is doubly linked, you can use a backwards traversal to find the last occurrence (and a forward traversal to find the first occurrence).
Here is some suggested code:
struct node* findFirstNode(char data) {
struct node *current = head;
while (current != NULL && current->data != data) {
current = current->next;
}
return current;
}
struct node* findLastNode(char data) {
struct node *current = tail;
while (current != NULL && current->data != data) {
current = current->prev;
}
return current;
}
void removeNode(struct node *current) {
if (current->prev == NULL) {
head = current->next;
} else {
current->prev->next = current->next;
}
if (current->next == NULL) {
tail = current->prev;
} else {
current->next->prev = current->prev;
}
free(current);
}
void remove_unique_dll() {
struct node *current = head;
struct node *next;
while (current != NULL)
{
next = current->next;
if (findFirstNode(current->data) == findLastNode(current->data)) {
removeNode(current);
}
current = next;
}
}
You have at least three errors.
After counting the number of occurrences of an item, you use next in several places. However, next has been used to iterate through the list. It was moved to the end and is now a null pointer. You can either reset it with next = current->next; or you can change the places that use next to current->next.
At the end of remove_unique_dll, you have head=current;. There is no reason to update head at this point. Whenever the first node was removed from the list, earlier code in remove_unique_dll updated head. So it is already updated. Delete the line head=current;.
That will leave code that deletes all but one occurrence of each item. However, based on your sample output, you want to leave multiple occurrences of items for which there are multiple occurrences. For that, you need to rethink your logic in remove_unique_dll about deciding which nodes to delete. When it sees the first a, it scans the remainder of the list and sees the second, so it does not delete the first a. When it sees the second a, it scans the remainder of the list and does not see a duplicate, so it deletes the second a. You need to change that.
Let's consider your code step by step.
It seems you think that in this declaration
struct node *head, *tail = NULL; //Represent the head and tail of the doubly linked list
the both pointers head and tail are explicitly initialized by NULL. Actually only the pointer tail is explicitly initialized by NULL. The pointer head is initialized implicitly as a null pointer only due to placing the declaration in file scope. It to place such a declaration in a block scope then the pointer head will be uninitialized.
Instead you should write
struct node *head = NULL, *tail = NULL; //Represent the head and tail of the doubly linked list
Also it is a very bad approach when the functions depend on these global variables. In this case you will be unable to have more than one list in a program.
Also the declaration of the variable len that is used only in main as a global variable
int len;
also a bad idea. And moreover this declaration is redundant.
You need to define one more structure that will contain pointers head and tail as data members as for example
struct list
{
struct node *head;
struct node *tail;
};
The function addNode can invoke undefined behavior when a new node can not be allocated
void addNode(char data)
{
struct node *newNode = (struct node*) malloc(sizeof(struct node)); //Create new node
//...
You should check whether a node is allocated successfully and only in this case change its data members. And you should report the caller whether a node is created or not.
So the function should return an integer that will report an success or failure.
In the function remove_unique_dll after this while loop
while (next != NULL)
{
if (next->data == current->data)
{
cnt += 1;
next = next->next;
}
else
next = next->next;
//printf("#%c %d %c\n",next->data,cnt,current->data);
}
if cnt is equal to 1
if (cnt == 1)
//..
then the pointer next is equal to NULL. And using the pointer next after that like
if (prev == NULL)
{
head = next;
}
else
{
prev->next = next;
}
is wrong.
Also you need to check whether there is a preceding node with the same value as the value of the current node. Otherwise you can remove a node that is not a unique because after it there are no nodes with the same value.
And this statement
head = current;
does not make sense because after the outer while loop
while (current != NULL)
the pointer current is equal to NULL.
Pay attention that the function will be more useful for users if it will return the number of removed unique elements.
Here is a demonstration program that shows how the list and the function remove_unique_dll can be defined.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char data;
struct node *next;
struct node *prev;
};
struct list
{
struct node *head;
struct node *tail;
};
int addNode( struct list *list, char data )
{
struct node *node = malloc( sizeof( *node ) );
int success = node != NULL;
if (success)
{
node->data = data;
node->next = NULL;
node->prev = list->tail;
if (list->head == NULL)
{
list->head = node;
}
else
{
list->tail->next = node;
}
list->tail = node;
}
return success;
}
size_t remove_unique_dll( struct list *list )
{
size_t removed = 0;
for ( struct node *current = list->head; current != NULL; )
{
struct node *prev = current->prev;
while (prev != NULL && prev->data != current->data)
{
prev = prev->prev;
}
if (prev == NULL)
{
// there is no preceding node with the same value
// so the current node is possibly unique
struct node *next = current->next;
while (next != NULL && next->data != current->data)
{
next = next->next;
}
if (next == NULL)
{
// the current node is indeed unique
struct node *to_delete = current;
if (current->prev != NULL)
{
current->prev->next = current->next;
}
else
{
list->head = current->next;
}
if (current->next != NULL)
{
current->next->prev = current->prev;
}
else
{
list->tail = current->prev;
}
current = current->next;
free( to_delete );
++removed;
}
else
{
current = current->next;
}
}
else
{
current = current->next;
}
}
return removed;
}
void display( const struct list *list )
{
for (const node *current = list->head; current != NULL; current = current->next)
{
printf( "%c<->", current->data );
}
puts( "null" );
}
int main()
{
struct list list = { .head = NULL, .tail = NULL };
const char *s = "aabc";
for (const char *p = s; *p != '\0'; ++p)
{
addNode( &list, *p );
}
printf( "Doubly linked list:\n" );
display( &list );
size_t removed = remove_unique_dll( &list );
printf( "There are removed %zu unique value(s) in the list.\n", removed );
printf( "Doubly linked list after removing unique elements:\n" );
display( &list );
}
The program output is
Doubly linked list:
a<->a<->b<->c<->null
There are removed 2 unique value(s) in the list.
Doubly linked list after removing unique elements:
a<->a<->null
You will need at least to write one more function that will free all the allocated memory when the list will not be required any more.

Deleting node from linked list issues C

I'm simply trying to delete a node from the linked list and seem to be having trouble. I was wondering if someone could please have a look at what could be wrong? Thanks!
struct ets {
struct node *equip_head;
struct node *member_head;
struct node *loan_head;
const char *equip_fname;
const char *member_fname;
const char *loan_fname;
};
struct node {
void *data; /* Accepts all data, yay */
struct node *next;
};
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *tmpNode = current;
struct member_data *member_data = NULL;
while (current != NULL) {
member_data = current->data;
if (strcmp(member_data->MemberID, MemberID) == 0) {
tmpNode = current;
current = current->next;
free(tmpNode->data);
free(tmpNode);
return TRUE;
}
current = current->next;
}
return FALSE;
}
You are not removing the node from list. You can do this to remove a node from a list:
BOOLEAN deleteMember(struct ets *ets, char MemberID[]) {
struct node *current = ets->member_head;
struct node *prev=NULL;
struct member_data *member_data = NULL;
while(current != NULL) {
member_data = current->data;
if(strcmp(member_data->MemberID, MemberID) == 0) {
if(prev==NULL) // removing 1st node
ets->member_head=current->next;
else
prev->next=current->next; // removing current node from list
free(current->data);
free(current);
return TRUE;
}
prev = current;
current = current->next;
}
return FALSE;
}
As you have a single linked list you delete algorythm is broken. Here is what your are currently doing :
locate member to delete. Fine. You have node-1 -> node -> node+1
you delete the member. Why not. But you list would become node-1 -> unallocated node and no way to find next nodes
You should instead test if next node has the correct id to have something like : prev_node(current) -> node_to_delete -> next_node`
Then you can do :
tmpNode = current->next;
current->next = tmpNode->next; /* ok for the chaining */
free(tmpNode->data); /* deletion will be ok */
free(tmpNode);
With of course a special management for first and last nodes ...
Edit : Ali already gave the answer. I leave this one as a comment on why OP's algo was broken

inserting in singly linked list in ascending order

Say I have a singly linked list of elements in ascending order that looks like:
A->B->D->E
I want to insert C in between B and D.
I know how to point C to D, but I don't know how to point B to C since the linked list does not keep track of the prev node.
While you are scanning down the list of nodes you have to keep two pointers: one points to the current node that you are interested in, and the other points to the previous node.
A possible implementation follows:
struct node {
int value;
struct node *next;
};
void sorted_insert(struct node **head, struct node *element) {
// Is the linked list empty?
if (*head == NULL) {
element->next = NULL;
*head = element;
return;
}
// Should we insert at the head of the linked list
if (element->value < (*head)->value) {
element->next = *head;
*head = element;
return;
}
// Otherwise, find the last element that is smaller than this node
struct node *needle = *head;
while (true) {
if (needle->next == NULL)
break;
if (element->value < needle->next->value)
break;
needle = needle->next;
}
// Insert the element
element->next = needle->next;
needle->next = element;
return;
}
You don't need to point B to C, or to maintain a pointer to the previous element. One method is:
Step to node D
malloc() a new node
Copy the data and next member from node D to your new node
Copy the data for node C into the existing node D (which now becomes node C)
Point the next member of the old node D to the new node.
For instance, excluding the possibility of inserting at the head of the list:
void insert(struct node * head, const int data, const size_t before)
{
assert(before > 0);
struct node * node = head;
while ( before-- && node ) {
node = node->next;
}
if ( !node ) {
fprintf(stderr, "index out of range\n");
exit(EXIT_FAILURE);
}
struct node * new_node = malloc(sizeof *new_node);
if ( !new_node ) {
perror("couldn't allocate memory for node");
exit(EXIT_FAILURE);
}
new_node->data = node->data;
new_node->next = node->next;
node->next = new_node;
node->data = data;
}
Why not keep track of the 'previous' node as you iterate through the list? Please forgive any syntactic shortcomings, as I haven't compiled this, but this should give you the idea.
struct node {
char name[ 10 ];
struct node *next;
};
struct list {
struct node *head;
};
void insert_C(struct list *list) {
struct node *new_node = malloc(sizeof(struct node));
if( new_node == NULL ) {
/* Error handling */
}
strcpy(new_node->name, "C");
struct node *pnode;
struct node *prev_node = NULL;
for( pnode = list->head; pnode->next != null; pnode = pnode->next ) {
if( !strcmp(pnode->name, "D") ) {
if( prev_node == NULL ) {
/* The 'C' node is going to be the new head. */
new_node->next = list->head;
list->head = new_node;
}
else {
prev_node->next = new_node;
new_node->next = pnode;
}
break;
}
/* Remember this node for the next loop iteration! */
prev_node = pnode;
}
}

c doubly linked list print backwards

I am trying to make my linked list program into a doubly linked list, however, am encountering a problem when I am trying to print my list backwards.
At the moment when I try to print backwards, it just runs a never ending loop, and I can't quite figure out where the error is.
If anyone can point out what I'm doing wrong it'd be very helpful.
EDIT: I believe the problem is in the displaybackwards method, but I do not know how to change it, as removing it would cause the program to crash.
These are the parts of my current code that I think the problem may be in:
struct NODE
{
union
{
int nodeCounter;
void *dataitem;
}item;
struct NODE *link;
struct NODE *backlink;
};
struct NODE *InitList()
{
struct NODE *temp = (struct NODE*)malloc(sizeof NODE);
temp->item.nodeCounter = 0;
temp->link = NULL;
temp->backlink = NULL;
return temp;
}
void Add2List(struct NODE *start, struct NODE *NewNode)
{
struct NODE *current = start;
while (current->link != NULL)
{
current->backlink = current; //problem should be this line
current = current->link;
}
current->link = NewNode;
NewNode->link = NULL;
NewNode->backlink = current;
start->item.nodeCounter++;
}
void DisplayList(struct NODE *start)
{
struct NODE *current = start->link;
while (current != NULL)
{
DisplayNode((struct inventory *)current->item.dataitem);
current = current->link;
}
}
void DisplayBackwards(struct NODE *start)
{
struct NODE *current = start->link;
while(current != NULL && current->link != NULL) //goes until current == last node
{
current = current->link;
current->backlink = current;
}
//when current == last node
while(current != start)// && current->backlink != NULL)
{
DisplayNode((struct inventory*)current->item.dataitem);
current->link = current;
current = current->backlink;
}
}
void DisplayBackwards(struct NODE *start)
{
struct NODE *current = start; //current points to first node
if(current==NULL) //if empty list, return
return;
//now we are sure that atleast one node exists
while(current->link != NULL) //goes until current == last node
{
current = current->link; //keep on going forward till end of list
}
//start from last node and keep going back till you cross the first node
while(current != NULL)
{
DisplayNode((struct inventory*)current->item.dataitem);
current = current->backlink; //go one node back
}
}

Elegant implementation of circular singly-linked list in C?

Going through classic data structures and have stopped on linked lists.Just implemented a circular singly-linked list, but I'm under overwhelming impression that this list could be expressed in a more elegant manner, remove_node function in particular.
Keeping in mind efficiency and code readability, could anybody present a more concise and efficient solution for singly-linked circular list?
#include <stdio.h>
#include <stdlib.h>
struct node{
struct node* next;
int value;
};
struct list{
struct node* head;
};
struct node* init_node(int value){
struct node* pnode;
if (!(pnode = (struct node*)malloc(sizeof(struct node)))){
return NULL;
}
else{
pnode->value = value;
}
return pnode;
}
struct list* init_list(){
struct list* plist;
if (!(plist = (struct list*)malloc(sizeof(struct list)))){
return NULL;
}
plist->head = NULL;
return plist;
}
void remove_node(struct list*a plist, int value){
struct node* current, *temp;
current = plist->head;
if (!(current)) return;
if ( current->value == value ){
if (current==current->next){
plist->head = NULL;
free(current);
}
else {
temp = current;
do {
current = current->next;
} while (current->next != plist->head);
current->next = plist->head->next;
plist->head = current->next;
free(temp);
}
}
else {
do {
if (current->next->value == value){
temp = current->next;
current->next = current->next->next;
free(temp);
}
current = current->next;
} while (current != plist->head);
}
}
void print_node(struct node* pnode){
printf("%d %p %p\n", pnode->value, pnode, pnode->next);
}
void print_list(struct list* plist){
struct node * current = plist->head;
if (!(current)) return;
if (current == plist->head->next){
print_node(current);
}
else{
do {
print_node(current);
current = current->next;
} while (current != plist->head);
}
}
void add_node(struct node* pnode,struct list* plist){
struct node* current;
struct node* temp;
if (plist->head == NULL){
plist->head = pnode;
plist->head->next = pnode;
}
else {
current = plist->head;
if (current == plist->head->next){
plist->head->next = pnode;
pnode->next = plist->head;
}
else {
while(current->next!=plist->head)
current = current->next;
current->next = pnode;
pnode->next = plist->head;
}
}
}
Take a look at the circular linked list in the Linux kernel source: http://lxr.linux.no/linux+v2.6.36/include/linux/list.h
Its beauty derives from the fact that you don't have a special struct for your data to fit in the list, you only have to include the struct list_head * in the struct you want to have as a list. The macros for accessing items in the list will handle the offset calculation to get from the struct list_head pointer to your data.
A more verbose explanation of the linked list used in the kernel can be found at kernelnewbies.org/FAQ/LinkedLists (Sorry, I dont have enough karma to post two hyperlinks).
Edit: Well, the list is a double-linked list and not a single-linked one like you have, but you could adopt the concept and create your own single-linked list.
List processing (particularly of circular lists) gets way easier when you treat the list head like an element of the list (a so-called "sentinel"). A lot of special cases just disappear. You can use a dummy node for the sentinel, but if the next pointer is first in the struct, you don't need to do even that. The other big trick is to keep a pointer to the next pointer of the previous node (so you can modify it later) whenever you modify the list. Putting it all together, you get this:
struct node* get_sentinel(struct list* plist)
{
// use &plist->head itself as sentinel!
// (works because struct node starts with the next pointer)
return (struct node*) &plist->head;
}
struct list* init_list(){
struct list* plist;
if (!(plist = (struct list*)malloc(sizeof(struct list)))){
return NULL;
}
plist->head = get_sentinel(plist);
return plist;
}
void add_node_at_front(struct node* pnode,struct list* plist){
pnode->next = plist->head;
plist->head = pnode;
}
void add_node_at_back(struct node* pnode,struct list* plist){
struct node *current, *sentinel = get_sentinel(plist);
// search for last element
current = plist->head;
while (current->next != sentinel)
current = current->next;
// insert node
pnode->next = sentinel;
current->next = pnode;
}
void remove_node(struct list* plist, int value){
struct node **prevnext, *sentinel = get_sentinel(plist);
prevnext = &plist->head; // ptr to next pointer of previous node
while (*prevnext != sentinel) {
struct node *current = *prevnext;
if (current->value == value) {
*prevnext = current->next; // remove current from list
free(current); // and free it
break; // we're done!
}
prevnext = &current->next;
}
}
void print_list(struct list* plist){
struct node *current, *sentinel = get_sentinel(plist);
for (current = plist->head; current != sentinel; current = current->next)
print_node(current);
}
A few comments:
I think the remove function doesn't correctly adjust the circular list pointers when you delete the head node and the list is larger than 3 elements. Since the list is circular you have to point the last node in the list to the new head.
You might be able to shorten the remove function slightly by creating a "find_node" function. Since the list is circular, however, there will still be the edge case of deleting the head node which will be more complex than in a non-circular list.
Code "beauty" is in the eye of the beholder. As code goes yours is easy to read and understand which beats a lot of code in the wild.
I use the following to create a dynamic circular singly linked list. All it requires is the size.
Node* createCircularLList(int size)
{
Node *it; // to iterate through the LList
Node *head;
// Create the head /1st Node of the list
head = it = (Node*)malloc(sizeof(Node));
head->id = 1;
// Create the remaining Nodes (from 2 to size)
int i;
for (i = 2; i <= size; ++i) {
it->next = (Node*)malloc(sizeof(Node)); // create next Node
it = it->next; // point to it
it->id = i; // assign its value / id
if (i == 2)
head->next = it; // head Node points to the 2nd Node
}
// close the llist by having the last Node point to the head Node
it->next = head;
return head; // return pointer to start of the list
}
And i define Node ADT like so:
typedef struct Node {
int id;
struct Node *next;
} Node;

Resources