Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Deleting the first item in linked list
#1
Hello, this is what I have so far:

class Linked_list:
    def __init__(self, data, next=None):
        self.data, self.next = data, next

def print_list(list):
    while list is not None:
        print(list.data, end=' ---> ')
        list = list.next
    print(None)

def make(sequence):
    list = None
    for value in reversed(sequence):
        list = Linked_list(value, list)
    return list

def remove_first(list):
    ...
I simply don´t know how to delete the first item of my linked list. I should use just structure I wrote above. Can you give me a help?

Thanks.
Quote
#2
Using list as a variable name is a bad idea. list is a built-in, and by using it as a variable name, you block the use of the built-in, which could cause errors in other code.

Let's say your linked list is called llist. llist is actually the first item in you list, and it links to the next item in the list. The second item in the list is llist.next. Can you see then how to make the first item in your list be the second item in your list, thus deleting the previous first item?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
I could change a refference to the second item? But not sure how to do this using a separate function remove_first.
Quote
#4
If remove_first is not supposed to have a return value, you could copy the values of the second item in the list into the first item of the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
Something like this?

llist.data = llist.next.data
Quote
#6
That's half of it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#7
llist.next = llist.next.next works at least in my case. Is it correct?
Quote
#8
Yeah, that's the other half.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How can I increment a List item with in a "for in" msteffes 4 242 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  'int' object does not support item assignment shane1236 5 156 Aug-13-2019, 01:53 PM
Last Post: buran
  Checking for an item in a list (if then statement) Sailnir 1 79 Jul-18-2019, 05:36 PM
Last Post: ndc85430
  Error: int object does not support item assignment ankita_nthu 2 171 Jul-07-2019, 02:14 PM
Last Post: ankita_nthu
  Store a product/item in a inventory program viktoria_linn 1 138 Jul-02-2019, 09:26 PM
Last Post: DeaD_EyE
  Deleting data in sqlite3 JJ39 3 202 Jun-23-2019, 04:39 PM
Last Post: noisefloor
  Sort sets by item values Sergey 4 278 Apr-19-2019, 10:50 AM
Last Post: Sergey
  looking for a multi item container Skaperen 2 211 Apr-15-2019, 04:06 AM
Last Post: Skaperen
  looking fo an expression that yields a dictionary without an item Skaperen 5 346 Apr-09-2019, 02:05 AM
Last Post: Skaperen
  How do I use this? TypeError: 'NoneType' object does not support item assignment ejected 9 1,778 Mar-26-2019, 05:06 AM
Last Post: ejected

Forum Jump:


Users browsing this thread: 1 Guest(s)