Ⅰ python可以编写自己的链表类吗
你如果一定要模拟C建立树形结构的方式,可以不必特意制定left和right的类型,仅在需要的时候给其赋上LinkedList的类型就行了。
如:
>>> class LinkedList():
def __init__(self,default_value=""):
self.left=0 #比如0表示left节点为空
self.right=0
self.value=default_value
return
>>> root = LinkedList()
>>> left = LinkedList()
>>> right = LinkedList()
>>> root.left = left
>>> root.right = right
>>> root.left
<__main__.LinkedList object at 0x00F964B0>
你自己写的那个class有不少错误,比如value,left,right这样写,属于类值,类似C++中的静态值,是所有LinkedList类和类实例共享的,你的__init__缺少self。
Ⅱ python中如何创建类似这样的列表,如[[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2][1,2,3]……[3,3,3]]
用自带的proct给你做个生成器:
from itertools import proct
for e in proct(range(1, 4), repeat=3): print(e)
Ⅲ Java,Python中没有指针,怎么实现链表,图等数据结构
用编程实现图的存储一般有常见的有两种方式,第一种是邻接链表、第二种就是邻接矩阵。
Ⅳ python中的链表(linked list)
凤鸾宝帐景非常,尽是泥金巧样妆。
曲曲远山飞翠色,翩翩舞袖映霞裳。
梨花带雨争娇艳,芍药笼烟骋媚妆。
但得妖娆能举动,取回长乐侍君王。
// 这题需要使用辗转相除法,又名欧几里德算法(Euclideanalgorithm)
#include <stdio.h>
int main (void)
{
int m, n, p, tmp;
printf ("Please type in two number:\n");
scanf ("%i %i", &m, &n); //输入两个数,分别放入m, n
p=m*n; //先把两数的积算出来,后面m和n的值会有变
while (n!=0){
tmp=m%n;
m=n;
n=tmp; //这段是求最大公约数的算法
}
printf ("The GCD is %i\n", m); //上面的算法n=0时m这时的值就是最大公约数
printf ("The LCM is %i\n", p/m);//两数的积除以最大公约数就是最小公倍数了
return 0;
}
Ⅳ python怎样创建具有一定长度和初始值的列表
1、首先,我们需要打开Python的shell工具,在shell当中新建一个对象member,对member进行赋值。
Ⅵ python有链表吗
python中的链表(linked list)是一组数据项的集合,其中每个数据项都是一个节点的一部分,每个节点还包含指向下一个节点的链接。链表有两种类型:单链表和双链表。
链表的数据结构
在链表中删除操作可以通过修改指针来实现,
插入则是调整,插入点的前后两个指针的指向关系,
在python中每个变量都是指针,例如:
用内置数据结构(list,dict,tuple等)的嵌套/组合,它们隐式地包含了指向/嵌套关系,如graph[u][v]={w0,w1..}类的成员变量、嵌套类可能包含了指向/嵌套关系;
引用表示指向关系,只不过引用不能像指针一样运算,比如p+1指向下一个元素,所以可能限制颇多。因此,要实现链表的操作,不能和c一样直接对指针进行操作。
python学习网,大量的免费python视频教程,欢迎在线学习!
Ⅶ 如何使用python建立链表
classListNode:
def__init__(self,x):
self.val=x
self.next=None
l1=newListNode(0)
l1.next=newListNode(1)
Ⅷ python编程中实现linkedlist(链表)报错是因为什么,怎么解决
楼主你好!
看你的代码存在很多问题,一个个来说明
1)首先你代码的报错源于你想用list来展开你的SLinkedList类,在python中,除非内置的可迭代对象外,其他都需要实现__iter__()函数,才能用list来进行展开。注意:判断一个对象是否可迭代,请使用isinstance(obj, Iterable)来判断obj是不是可以迭代,Iterable需要从collections中导入
2)插入的方法存在严重问题,按楼主的方法插入的话,因为头节点始终在变,所以当你需要遍历链表的时候就会找不到头节点;
3)pop的方法实现也有问题,因为是单向链,所以无法从末节点开始删除,只能删除头节点
4)top方法的意图未知
其他:
下面列举了一下我修改后的方案,做了一些锦上添花的操作,每个基本操作都会返回链表对象,这样就可以使用链式操作来写代码;迭代函数使用yield来实现,避免展开时占用不必要的内存。
另:我的展开时直接取链表中各个节点的元素,加了一些关键注释在代码中;
#-*-coding:utf-8-*-
classNode:
def__init__(self):
'''
elm:节点元素
nxt:下个节点指针
'''
self.elm,self.nxt=None,None
classSLinkedList:
def__init__(self):
'''
head:链表头
end_point:链表尾
'''
self.head=None
self.end_point=None
defpush(self,x):
p=Node()
p.elm=x
ifself.headisNone:
self.head=p
self.end_point=p
returnself
self.end_point.nxt=p
self.end_point=p
returnself
defpop(self):
'''因为实现的是一个单链表,所以只能从头开始删除节点'''
ifself.head.nxtisNone:
return
self.head=self.head.nxt
returnself
def__iter__(self):
temp_node=self.head
whiletemp_nodeisnotNone:
yieldtemp_node.elm
temp_node=temp_node.nxt
if__name__=='__main__':
'''增加1,2,5三个元素,并删除一个头节点'''
mylinklist=SLinkedList().push(1).push(2).push(5).pop()
print(list(mylinklist))
其实python这个语言使用链表有些画蛇添足,但是如果拿来当作需求练手也无妨。
望采纳,谢谢!
Ⅸ 请问Python3中创建列表有哪些方法
Python中的列表内建了许多方法。在下文中,使用“L”代表一个列表,使用“x”代表方法的参数,以便说明列表的使用方法。
1 append()方法
列表的append()方法用于将一个项添加到列表的末尾,L.append(x)等价于L[len(L):] = [x]。
例如,使用append()方法分别将'cow'和'elephant'添加到animals列表的末尾:
>>>animals=['cat','dog','fish','dog']
>>>animals.append('cow')#等价于animals[4:]=['cow']
>>>animals
['cat','dog','fish','dog','cow']
>>>animals.append('elephant')#等价于animals[5:]=['elephant']
>>>animals
['cat','dog','fish','dog','cow','elephant']
2 ()方法
列表的()方法用于将一个项插入指定索引的前一个位置。L.(0, x)是将x插入列表的最前面,L.(len(L)), x)等价于L.append(x)。
例如,使用()方法分别将'cow'和'elephant'插入animals列表:
>>>animals=['cat','dog','fish','dog']
>>>animals.(0,'cow')
>>>animals
['cow','cat','dog','fish','dog']
>>>animals.(3,'elephant')
>>>animals
['cow','cat','dog','elephant','fish','dog']
3 extend()方法
列表的extend()方法用于将可迭代对象的所有项追加到列表中。L.extend(iterable)等价于L[len(L):] = iterable。extend()和append()方法的区别是,extend()方法会将可迭代对象“展开”。
例如,分别使用append()方法和extend()方法在animals列表后面追加一个包含'cow'和'elephant'的列表:
>>>animals=['cat','dog','fish','dog']
>>>animals.append(['cow','elephant'])#此处append()参数是一个列表
>>>animals
['cat','dog','fish','dog',['cow','elephant']]
>>>animals=['cat','dog','fish','dog']
>>>animals.extend(['cow','elephant'])#此处extend()参数也是一个列表
>>>animals
['cat','dog','fish','dog','cow','elephant']
4 remove()方法
列表的remove()方法用于移除列表中指定值的项。L.remove(x)移除列表中第一个值为x的项。如果没有值为x的项,那么会抛出ValueError异常。
例如,使用remove()方法移除animals列表中值为'dog'的项:
>>>animals=['cat','dog','fish','dog']
>>>animals.remove('dog')
>>>animals
['cat','fish','dog']
>>>animals.remove('dog')
>>>animals
['cat','fish']
>>>animals.remove('dog')
Traceback(mostrecentcalllast):
File"",line1,in
ValueError:list.remove(x):xnotinlist
5 pop()方法
列表的pop()方法用于移除列表中指定位置的项,并返回它。如果没有指定位置,那么L.pop()移除并返回列表的最后一项。
例如,使用pop()方法移除animals列表中指定位置的项:
>>>animals=['cat','dog','fish','dog']
>>>animals.pop()
'dog'
>>>animals
['cat','dog','fish']
>>>animals.pop(2)
'fish'
>>>animals
['cat','dog']
在调用前面的列表方法后,并没有打印任何值,而pop()方法打印了“弹出”的值。包括append()、()、pop()在内的方法都是“原地操作”。原地操作(又称为就地操作)的方法只是修改了列表本身,并不返回修改后的列表。
在类型转换时使用的int()函数,str()函数都有返回值:
>>>number=123
>>>mystring=str(number)#将返回值赋给变量mystring
>>>mystring
'123'
但是在使用“原地操作”时,大部分则不会有返回值,包括pop()方法也只是返回了被“弹出”的值,并没有返回修改后的列表:
>>>animals=['cat','dog','fish','dog']
>>>new_animals=animals.append('cow')
>>>print(new_animals)
None
关于深度学习的基础问题可以看下这个网页的视频教程,网页链接,希望我的回答能帮到你。
Ⅹ 如何创建单链表
这我原来写的,有单链表的建立、插入、删除、查找等,希望对你有帮助
typedef struct node{
int data;
struct node *next;
}node;
node *create()
{
node *head,*p,*q;
int i=0;
int x;
head=(node *)malloc(sizeof(node));
while(1)
{
printf("please input the node:");
scanf("%d",&x);
if(x==0) {break;}
p=(node *)malloc(sizeof(node));
p->data=x;
if(++i==1)
{
head->next=p;
}
else
{
q->next=p;
}
q=p;
}
q->next=NULL;
return head;
}
void print(node *head)
{
node *p;
int index=0;
if(head->next==NULL)
{
printf("Link is empty!\n");
exit(0);
}
p=head->next;
while(p!=NULL)
{
printf("the %d node is %d\n",++index,p->data);
p=p->next;
}
}
int length(node *head)
{
int len=0;
node *p;
p=head->next;
while(p)
{
len++;
p=p->next;
}
return len;
}
node *search(node *head,int pos)
{
node *p;
int len=length(head);
p=head->next;
if(pos<0)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos>len)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos==0)
{
return head;
}
if(p==NULL)
{
printf("the link is empty!\n");
return NULL;
}
while (--pos)
{
p=p->next;
}
return p;
}
node *delete(node *head,int pos)
{
node *p,*q;
int len=length(head);
p=head->next;
if(pos<0)
{
printf("incorrect position!\n");
return NULL;
}
else if(pos>len)
{
printf("incorrect position!\n");
return NULL;
}
if(p==NULL)
{
printf("link empty!\n");
return NULL;
}
p=search(head,pos-1);
if(p!=NULL&&p->next!=NULL)
{
q=p->next;
p->next=q->next;
free(q);
}
return head;
}
node *insert(node *head,int pos,int x)
{
node *p,*q=NULL;
q=(node *)malloc(sizeof(node));
q->data=x;
if(pos==0)
{
head->next=q;
return head;
}
p=search(head,pos);
if(p!=NULL)
{
q->next=p->next;
p->next=q;
}
return head;
}