473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C - problem with insert in linked list

hey, can anyone figure out why this insert function is setting all nodes in the list to have the same item?
Expand|Select|Wrap|Line Numbers
  1. void insert_list (list_ref list, list_item item) {
  2.     listnode_ref new = malloc (sizeof (struct listnode));
  3.     assert (is_list (list));
  4.     assert (new != NULL);
  5.     new->tag = listnode_tag;
  6.     new->item = item;
  7.     if (list->head == NULL) {
  8.        list->head = new;
  9.        list->last = new;
  10.        list->curr = new;
  11.     }else{
  12.        new->prev = list->last;
  13.        list->last->next = new;
  14.        list->last = new;
  15.        list->curr = new;
  16.     };
  17. }
  18.  
i cant figure it out for the life of me. other than that problem it seems to be working fine - it creates a new node and sticks it on the list, and it appears that the links work, too. any help would be GREATLY appreciated.. i've been trying to figure this out for like 2 hours -_- i think i must be missing something obvious, but i have no idea what. i know for sure the problem has to be in this function, though (after a LOT of debug prints to pinpoint it).
Mar 2 '08 #1
5 1613
gpraghuram
1,275 Expert 1GB
I dont know how you have defined your doubly linked list.
But the logic would be

If head == NULL
head = tail = newnode;
else
tail->next=newnode;
newnode->prev = tail;
tail=newnode



Raghuram
Mar 3 '08 #2
yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=tail are switched (which shouldnt make a difference right?)

so.. i dont see how it can be this function thats the problem.. yet i stuck a print statement for the item right before the insert function is called, and a debug function that shows all the items right after the insert function is called...

the print statement shows different items being called in and the debug shows all the items being set to whatever the most recent one was (and a new node is added every time).

any ideas?
Mar 3 '08 #3
gpraghuram
1,275 Expert 1GB
yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=tail are switched (which shouldnt make a difference right?)

so.. i dont see how it can be this function thats the problem.. yet i stuck a print statement for the item right before the insert function is called, and a debug function that shows all the items right after the insert function is called...

the print statement shows different items being called in and the debug shows all the items being set to whatever the most recent one was (and a new node is added every time).

any ideas?
I can understand other lines except this
What does this line do?
list->last->next = new;

I think issue is with this line

Raghuram
Mar 3 '08 #4
I can understand other lines except this
What does this line do?
list->last->next = new;

I think issue is with this line

Raghuram

thats the equivalent of your tail->next=newnode
Mar 3 '08 #5
ok the problem was not creating a new space in memory for each item. i'm an idiot. problem solved.
Mar 3 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Fabio | last post by:
Hi everyone, Is there anybody who can suggest me a link where I can find information about 'Persistent linked list' ? I need to implement a linked list where every node is a structure like the...
3
by: sugaray | last post by:
hi, I wrote a simple program which merge two single linked lists into one for practice, but it always freezes after the creation of the first list, hope someone might help me out with this. thanx...
2
by: Skywise | last post by:
I am fairly new to linked lists. I am trying to write a class using linked lists. It seems to work fine, but I need to know if I have any resource leaks in it because I plan on using this class...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
3
by: idshiy | last post by:
hi there i have a linked list content another linked list. which is something like: struct nodebranch{ unsigned int offset; unsigned char data; struct nodebranch *next; }; struct...
13
by: B. Williams | last post by:
I have written some code to accept input and place this input at the beginning or end of a list, but I need assistance including a class so that it will allow input of a phone number that is...
6
by: askmatlab | last post by:
Hello all: I would like to insert a number into a linked list in ascending order. Is the following function correct? void insert(Node **node, int v) { Node *tmp = (Node...
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.