473,407 Members | 2,629 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,407 software developers and data experts.

How to implement linked list in Python??

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 not support pointers, so how to get around with this issue??
Aug 15 '06 #1
10 36198
kudos
127 Expert 100+
Hi
actually, python variables do 'points' to things, so in a sense python does support pointers. Here is an example program, that fills a list with numbers 1 to 5. Each
element in the list looks the following way: (value, pointer_to_next_element). Then it prints the content of the list
Expand|Select|Wrap|Line Numbers
  1. list = []
  2. list.append([1,0])
  3. current_element = list[0]
  4.  
  5. # build a linked list 1,2,3,4,5
  6.  
  7. for i in range(5):
  8.  list.append([i+2,0])
  9.  current_element[1] = list[i+1]
  10.  current_element = list[i+1]
  11.  
  12. # traverse the list
  13. # and print it to screen.
  14.  
  15. current_element = list[0]
  16. while(current_element[1] != 0):
  17.  print current_element[0]
  18.  current_element = current_element[1]
  19.  
Hope that made sense
-k


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 not support pointers, so how to get around with this issue??
Aug 16 '06 #2
It isn't necessary to implement a linked list, since Python already has a build-in list data structure.

Expand|Select|Wrap|Line Numbers
  1. myList = []
  2. myList.append("this")
  3. myList.append("is")
  4. myList.append("the")
  5. myList.append("list")
  6. for item in myList:
  7.     print item
  8.  
Sep 8 '06 #3
kudos
127 Expert 100+
well...but you wouldn't have a linked list :)
Sep 12 '06 #4
pbmods
5,821 Expert 4TB
Heya, kudos.

Please use CODE tags when posting source code:

[CODE=python]
Python code goes here.
[/CODE]
Oct 7 '07 #5
bartonc
6,596 Expert 4TB
Heya, kudos.

Please use CODE tags when posting source code:

Expand|Select|Wrap|Line Numbers
  1. Python code goes here.
  2.  
Surely, you are not admonishing one of the Python Forum's experts.
You must mean mwillis.?
Oct 8 '07 #6
maybe something like this....

Expand|Select|Wrap|Line Numbers
  1. class Node:
  2.     def __init__(self,value):
  3.         self.data = value
  4.         self.next = 0
  5.  
  6. class List:
  7.     def __init__(self):
  8.         self.firstNode = Node(0)
  9.  
  10.     def __ShowNodeData(self,aNode):
  11.         if aNode.next != 0:
  12.            print aNode.data
  13.            self.__ShowNodeData(aNode.next)
  14.  
  15.     def Dump(self):
  16.         self.__ShowNodeData(self.firstNode)
  17.  
  18.     def InsertAfter(self,aNode,aNewNode):
  19.         aNewNode.next = aNode.next
  20.         aNode.next = aNewNode
  21.  
  22.     def InsertBeginning(self,aNewNode):
  23.         aNewNode.next = self.firstNode
  24.         self.firstNode = aNewNode    
  25.  
  26. nodeA = Node("A")
  27. nodeB = Node("B")
  28. nodeC = Node("C")
  29. nodeD = Node("D")
  30.  
  31. aList = List()
  32.  
  33. aList.InsertBeginning(nodeB)
  34. aList.InsertAfter(nodeB,nodeD)
  35. aList.InsertAfter(nodeD,nodeC)
  36. aList.InsertAfter(nodeC,nodeA)
  37.  
  38. aList.Dump()
  39.  
  40.  
B
D
C
A
Oct 11 '07 #7
latte
4
@MojaveKid
Hi MojaveKid - this is good! Thanks for doing it!
I'm doing a public domain app at the moment which needs some code for a linked list, so I was wondering - may I use this in my app? ( You'll be credited as the author of it, no worries there... :) ).
Thanks again - bye for now -
- latte
May 21 '09 #8
ahh...sure you can use it...
however, you must freely teach every python newbie who might also ask about implementing a linked list in Python...or no deal..ehehehehehe
Aug 10 '09 #9
latte
4
@MojaveKid
Heh.. sounds ok to me... :) Very many thanks - it's a very nice bit of code!
Bye for now -
- latte
Aug 10 '09 #10
@kudos
What is the purpose of the i+ in the [i+2]? I ran the code and removed the i+ why does it continuously print 2?
Feb 7 '12 #11

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

Similar topics

10
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...
11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: hakimks | last post by:
You are provided with a sample C programs: calc.c, which implements a reverse polish notation calculator. Study it carefully. This program uses a stack (of course!) but the stack implementation is...
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
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
5
by: adam.kleinbaum | last post by:
Hi there, I'm a novice C programmer working with a series of large (30,000 x 30,000) sparse matrices on a Linux system using the GCC compiler. To represent and store these matrices, I'd like to...
1
by: theeverdead | last post by:
Ok I have a file in it is a record of a persons first and last name. Format is like: Trevor Johnson Kevin Smith Allan Harris I need to read that file into program and then turn it into a linked...
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: 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
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...
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,...
0
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...
0
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...
0
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...

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.