473,770 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list insertion

i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy

Aug 24 '05 #1
6 1456
What you've posted looks right, more or less.

To get any sort of meaningful feedback, you really need to post a full
version of your code, including the print statements, what's being
printed, and why you think this shows the code is not working. What
you've shown is far too little for anybody else to work with.

Regards,
Jordan
Randy Bush:
i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy


Aug 24 '05 #2
Randy Bush enlightened us with:
hold = self.next
self.next = DaClass(value)
self.next.next = hold
shouldn't that last line be this?
self.next.prev = hold
but i suspect (from print statement insertions) that the result is
not as i expect.


What did you expect, and what did you ovserve?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Aug 24 '05 #3
On Tue, 23 Aug 2005 20:58:11 -0700, Randy Bush wrote:
i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

mahalo,
randy


The example above looks like it would work, as long as other
stuff you _don't_ show is fine. Specifically, how do you
handle the case when the list is empty?

Better to show a more complete example with output and how that
is not what you expect.

Ross
Aug 24 '05 #4
>> hold = self.next
self.next = DaClass(value)
self.next.next = hold shouldn't that last line be this?
self.next.prev = hold


single threaded list
What did you expect, and what did you ovserve?


i will try to distill a case

randy

Aug 24 '05 #5
On Tue, 23 Aug 2005 20:58:11 -0700, Randy Bush <ra***@psg.co m> wrote:
i am trying to insert into a singly linked list

hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

I think you're fine. Here's some possible context for your code:
class DaClass(object) : ... def __init__(self, value):
... self.value = value
... self.next = None
... def values(self):
... while True:
... yield self.value
... if self.next is None: break
... self = self.next
... def insert(self, value):
... hold = self.next
... self.next = DaClass(value)
... self.next.next = hold
... return self
... def __repr__(self):
... return '<DaClass %s>'%(' -> '.join(map(repr , self.values())) )
...
... sll = DaClass(1)
sll <DaClass 1> sll.insert(2) <DaClass 1 -> 2> sll <DaClass 1 -> 2> sll.insert('a') <DaClass 1 -> 'a' -> 2> sll.next <DaClass 'a' -> 2> sll.next.insert ('b') <DaClass 'a' -> 'b' -> 2> sll

<DaClass 1 -> 'a' -> 'b' -> 2>

Regards,
Bengt Richter
Aug 28 '05 #6
>> hold = self.next
self.next = DaClass(value)
self.next.next = hold

but i suspect (from print statement insertions) that the result
is not as i expect. as the concept and code should be very
common, as i am too old for pride, i thought i would ask.

I think you're fine.


indeed. the bug was elsewhere. i got confused and tried to
look-ahead too far when i could have just recursed. i threw
away the too-clever code and replaced it with one line. i
love it when that happens.

randy

Aug 28 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2551
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
7
1881
by: OMouse | last post by:
Hi, I just switched to using STL for my linked lists and obviously I need a way to insert. I have all the necessary includes (list & algorithm) and the other functions that I've used (erase & find) work. But when I try to insert the iterator that was found, gcc 3.3.4 tells me that I'm missing a few inputs. Anyway, here's the snippet of code. main.cpp ----------------- #include "usernode.h" #include <list>
5
6061
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
7
2613
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's children - when a child is fork()ed its pid is added to the linked list. I then have a SIGCHLD handler which is supposed to remove the pid from the list when a child exits. The problem I'm having is that very very occasionally and seemingly...
6
16134
by: Julia | last post by:
I am trying to sort a linked list using insertion sort. I have seen a lot of ways to get around this problem but no time-efficient and space-efficient solution. This is what I have so far: struct node { int x; struct node *next; };
4
5620
by: WongMingYin | last post by:
Something I don't really understand. In http://maxnoy.com/interviews.html, Insert and Delete for linked list are defined as: int Insert(node **head, int data) and int Delete(node** head, int deleteMe)
6
4017
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create a new object of class T, I would like to check if this object already exists in the list: meaning one having same integers. This can be done in linear time in a list, and probably faster if I use STL Set instead of list. I am wondering however if...
3
3063
by: Suyash Upadhyay | last post by:
Hello All, I am a beginner of C Programming, I am working on linked list now-a-days, i have successfully created a linked list and displayed, but when i tried to insert an element in mid of linked list, it doesnt take place. I think I have written every thing write. Please help me, #include "stdio.h" struct node { int value; struct node* link;
4
1482
by: eight02645999 | last post by:
hi i have a list (after reading from a file), say data = I wanted to insert a word after every 'a', and before every 'd'. so i use enumerate this list: for num,item in enumerate(data): if "a" in item: data.insert(num+1,"aword") if "d" in item:
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
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
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7420
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
6682
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
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
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.