Ⅰ 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;
}