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

Linked list questions

How do i keep a linked list sorted? How do i remove if more than 10 items?

Expand|Select|Wrap|Line Numbers
  1. from node import node
  2.  
  3. probe = head
  4. count = 0
  5. while probe != None:
  6.     count += 1
  7.     probe = probe.next
  8. return count
  9.  
  10. def insert(newName, newScore, head):
  11.     probe.next.score = newScore
  12.     if head is None:
  13.         head = newNode
  14.     else:
  15.         probe = head
  16.         while probe.next != None:
  17.                 probe = probe.next
  18.                 probe.next = newNode
  19.     return head
  20.  
  21. def printStructure(newName, newScore):
  22.     probe = head
  23.     while probe != None:
  24.         print "Name: ", probe.name,
  25.         print "Score: ", probe.score
  26.         probe = probe.next
  27.  
  28. def main():
  29.     head  = None
  30.  
  31.     head = insert("She-RA", 1088, head)
  32.     printStructure(head)
  33.  
  34.     #Add ten nodes to the beginning of the linked structure
  35.     head = insert("He-MAN", 32464, head)
  36.     head = insert("Doc-Ock", 143322, head)
  37.     head = insert("Spidey", 6416, head)
  38.     head = insert("Superman", 63438, head)
  39.     head = insert("Arceus", 92515, head)
  40.     head = insert("Batman", 11986, head)
  41.     head = insert("Homer", 26712, head)
  42.     head = insert("F-ZERO", 833849, head)
  43.     insert("Dlew58", 999999, head)
  44.     print "Top Ten"
  45.     printStructure(head)
  46. if __name__ == "__main__":
  47.     main()
  48.  
Dec 20 '10 #1
1 1964
dwblas
626 Expert 512MB
A linked list is not sorted, it is linked in some order. So if you have records with the values 'A', 'C', 'E', 'G', 'H' in the linked list and you wanted to add 'B', the next record number would then be 6, so record number 1 would point to 6 instead of 2, and record 6 would point to the second record to keep them in order.
How do i remove if more than 10 items?
It depends on if you are talking about memory or a file on disk. Generally you mark it as deleted, and simply remove the link to it, so the record that points to it would point to the record that it used to point to. Practically speaking, regenerating the linked list isn't necessary every time a record is deleted but is done after some threshold is reached. To regenerate the linked list, you would copy the records in the original list to a new memory location or file without copying the deleted records.
Dec 21 '10 #2

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

Similar topics

3
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** it's been a while since i have poseted to this newsgroup, but for a long time i was not programming at all. but now that i am out of...
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...
4
by: dssuresh6 | last post by:
Whether browsing forward or backward can be done using a singly linked list. Is there any specific case where a doubly linked list is needed? For people who say that singly linked list allows...
2
by: a | last post by:
keep a list of read and unread items hi guys i m building an rss reader and i want you suggestions for datastructure for keeping read and unread list for each use i m assuming it will be very...
10
by: akadri1010 | last post by:
I am new to Python programming.. Can anyone help me to write a simple Python program that creates a singly linked list and then prints the entries in it?? I know how to do in C?? But Python does...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
16
by: sangram | last post by:
how to delete last node of a Linked list if you only know the address of last node. thanks sangram
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
6
by: CMoose | last post by:
I'm trying to write a program that reads data from a text file and stores it into a doubly linked list. I then want to be able to edit or sort the list. However, I'm having trouble getting started...
10
by: kalar | last post by:
Hello. we have this struct and we must to make a linked list struct node { char name; char phone; struct node *prevName; // previous node alphabetically struct node *prevNumber; //...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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...

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.