473,766 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C - problem with insert in linked list

4 New Member
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 1640
gpraghuram
1,275 Recognized Expert Top Contributor
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
WarmDismalAgony
4 New Member
yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=ta il 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 Recognized Expert Top Contributor
yeah thats exactly what my code is except for tail->next=newnode and newnode>prev=ta il 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
WarmDismalAgony
4 New Member
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
WarmDismalAgony
4 New Member
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
5496
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 following: struct Node { int integer_value;
3
1532
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 in advance. #include <cstdio> #include <cstdlib> #include <ctime> struct LinkList {
2
1392
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 quite a bit in my program. By the way, I am not a student hoping someone will do my work for me (the "cout"s are going to be taken out when I finalize the class... there just for debugging purposes now). This code is part of a computer program I...
57
4302
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
9476
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 prod400db.test.meldbf.InventoryHistory However, this insert statement fails: insert into prod400db.TEST.MELDBF.InventoryHistory
3
1515
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 node{
13
2099
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 properly formatted. I'll post my code below. Thanks in advance. This is the code for the linked list. It tests using int's and string's. #ifndef LIST_H #define LIST_H
6
20002
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 *)malloc(sizeof(Node)); while(*node && (*node)->value < v) node = &(*node)->next;
10
10505
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 between then what is the best solution.
4
4767
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: #include <iostream> using namespace std; class NumberList {
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10008
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.