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??
10 36198
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 -
list = []
-
list.append([1,0])
-
current_element = list[0]
-
-
# build a linked list 1,2,3,4,5
-
-
for i in range(5):
-
list.append([i+2,0])
-
current_element[1] = list[i+1]
-
current_element = list[i+1]
-
-
# traverse the list
-
# and print it to screen.
-
-
current_element = list[0]
-
while(current_element[1] != 0):
-
print current_element[0]
-
current_element = current_element[1]
-
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??
It isn't necessary to implement a linked list, since Python already has a build-in list data structure. -
myList = []
-
myList.append("this")
-
myList.append("is")
-
myList.append("the")
-
myList.append("list")
-
for item in myList:
-
print item
-
well...but you wouldn't have a linked list :)
Heya, kudos.
Please use CODE tags when posting source code:
[CODE=python]
Python code goes here.
[/CODE]
Heya, kudos.
Please use CODE tags when posting source code:
Surely, you are not admonishing one of the Python Forum's experts.
You must mean mwillis.?
maybe something like this.... -
class Node:
-
def __init__(self,value):
-
self.data = value
-
self.next = 0
-
-
class List:
-
def __init__(self):
-
self.firstNode = Node(0)
-
-
def __ShowNodeData(self,aNode):
-
if aNode.next != 0:
-
print aNode.data
-
self.__ShowNodeData(aNode.next)
-
-
def Dump(self):
-
self.__ShowNodeData(self.firstNode)
-
-
def InsertAfter(self,aNode,aNewNode):
-
aNewNode.next = aNode.next
-
aNode.next = aNewNode
-
-
def InsertBeginning(self,aNewNode):
-
aNewNode.next = self.firstNode
-
self.firstNode = aNewNode
-
-
nodeA = Node("A")
-
nodeB = Node("B")
-
nodeC = Node("C")
-
nodeD = Node("D")
-
-
aList = List()
-
-
aList.InsertBeginning(nodeB)
-
aList.InsertAfter(nodeB,nodeD)
-
aList.InsertAfter(nodeD,nodeC)
-
aList.InsertAfter(nodeC,nodeA)
-
-
aList.Dump()
-
-
B
D
C
A
@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
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
@MojaveKid
Heh.. sounds ok to me... :) Very many thanks - it's a very nice bit of code!
Bye for now -
- latte
@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?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:
|
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;
};
...
|
by: Xarky |
last post by:
Hi,
I am writing a linked list in the following way.
struct list
{
struct list *next;
char *mybuff;
};
|
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...
|
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
|
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 *...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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: 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: 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...
|
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...
|
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...
| |