473,385 Members | 1,740 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.

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 1438
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.com> 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
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,...
7
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)...
5
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...
7
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...
6
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: ...
4
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,...
6
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...
3
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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...
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,...

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.