python中有没有类似c语言链表详解的链表

& & & & 示意图:& & & & python双向链表实现代码:& & 复制代码代码如下:& & #!/usr/bin/python& & # -*- coding: utf-8 -*-& & class Node(object):& & def __in……
声明:该文章系网友上传分享,此内容仅代表网友个人经验或观点,不代表本网站立场和观点;若未进行原创声明,则表明该文章系转载自互联网;若该文章内容涉嫌侵权,请及时向
论文写作技巧
上一篇:下一篇:
相关经验教程python实现单链表 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 18时,
用Python模拟一下单链表,比较简单,初学者可以参考参考,大神可以给我点改进意见
代码片段(1)
1.&[代码][Python]代码&&&&
#coding:utf-8
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class NodeList(object):
def __init__(self, node):
self.head = node
self.head.next = None
self.end = self.head
def add_node(self, node):
self.end.next = node
self.end = self.end.next
def length(self):
node = self.head
while node.next is not None:
count += 1
node = node.next
return count
# delete node and return it's value
def delete_node(self, index):
if index+1 & self.length():
raise IndexError('index out of bounds')
node = self.head
while True:
if i==index-1:
node = node.next
tmp_node = node.next
node.next = node.next.next
return tmp_node.data
def show(self):
node = self.head
node_str = ''
while node is not None:
if node.next is not None:
node_str += str(node.data) + '-&'
node_str += str(node.data)
node = node.next
print node_str
# Modify the original position value and return the old value
def change(self, index, data):
if index+1 & self.length():
raise IndexError('index out of bounds')
node = self.head
while True:
if i == index:
node = node.next
tmp_data = node.data
node.data = data
return tmp_data
# To find the location of index value
def find(self, index):
if index+1 & self.length():
raise IndexError('index out of bounds')
node = self.head
while True:
if i == index:
node = node.next
return node.data
#test case
n1 = Node(0)
n2 = Node(1)
n3 = Node(2)
n4 = Node(3)
n5 = Node(4)
node_list = NodeList(n1)
node_list.add_node(n2)
node_list.add_node(n3)
node_list.add_node(n4)
node_list.add_node(n5)
#node = node_list.delete_node(3)
#print node
#d = node_list.change(0,88)
data = node_list.find(5)
print data
node_list.show()
开源中国-程序员在线工具:
相关的代码(492)
100回/34578阅
8回/32045阅
34回/24408阅
59回/24032阅
26回/19825阅
2回/18328阅
1回/16629阅
25回/16229阅
8回/15748阅
40回/15113阅
有列表要啥单链表
2楼:阳光_如此耀眼 发表于
引用来自“nocodo”的评论有列表要啥单链表没看我写的吗?是模拟一下,不是为了要用它干啥,那都有C++了,为啥还要出其他语言啊,搞笑
开源从代码分享开始
阳光_如此耀眼的其它代码匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。一般线性链表类的C++实现
一般线性链表类的C++实现
  以下的C++类LinkList实现了线性链表的一般操作。可以直接在其他的程序中直接建立它的对象,其中线性表中的数据在此为整型,具体应用的时候可以适当的修改,并可以在此基础上继续封装特定的功能。
  头文件:LinkList.h
  typedef strUCt LNode {&&&&&& struct LNode *&&& }LNode, *pLinkL
  class LinkList {&&& private:&&& pLinkList m_pL&&& int m_listL&&& public:&&& LinkList();&&& ~LinkList();&&& bool InitList ();&&& bool DestroyList ();&&& bool ClearList();&&& bool IsEmpty ();&&& int GetLength ();&&& bool GetNode(int position, LNode** node);&&& int LocateElem(int elem);&&& bool SetNodeData(int position, int newData);&&& bool GetNodeData(int position, int &data);&&& bool InsertNode(int beforeWhich, int data);&&& bool DeleteNode(int position);&&& };
  Cpp文件:LinkList.cpp
  #include &iostream.h&&&& #include "LinkList.h"
  LinkList::LinkList() {&&& m_pList = NULL;&&& m_listLength = 0;
  InitList();&&& }
  LinkList::~LinkList() {&&& if (!DestroyList()) {&&&&& DestroyList();&&& }&&& }
  //初始化,分配一个头节点。&&& bool LinkList::InitList() {&&& if (!(m_pList = new LNode)) {&&&&&&&& }&&& m_pList-&next = NULL;
  //销毁链表。&&& bool LinkList::DestroyList() {&&& if (!ClearList()) {&&&&&&&& }
  delete m_pL
  //判断链表是否为空。若为空,返回true,否则返回false。&&& bool LinkList::IsEmpty() {&&& if (m_pList-&next == NULL) {&&&&&&&& }&&&&&& }
  //返回链表的中当前节点数。&&& int LinkList::GetLength() {&&& return m_listL&&& }
  //将链表清空,释放当前所有节点。&&& bool LinkList::ClearList() {&&& if (m_pList == NULL) {&&&&&&&& }
  LNode *pTemp = NULL;&&& while (m_pList-&next != NULL) {&&&&& pTemp = m_pList-&&&&&& m_pList-&next = pTemp-&&&&&& delete pT&&& }&&& m_listLength = 0;
  //将position指定的节点内的数据设置为newData。&&& //第一个有效节点的position为1。&&& bool LinkList::SetNodeData(int position, int newData) {&&& LNode *pTemp = NULL;
  if (!(GetNode(position, &pTemp))) {&&&&&&&& }
  pTemp-&data = newD
  //得到指定位置节点的数据。&&& //节点索引从1到listLength。&&& bool LinkList::GetNodeData(int position, int &data) {&&& LNode *pTemp = NULL;
  if (!(GetNode(position, &pTemp))) {&&&&&&&& }
  data = pTemp-&
  //在链表中插入一个节点。&&& //插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。&&& //beforeWhich的取值在1到ListLength+1之间。&&& bool LinkList::InsertNode(int beforeWhich, int data) {&&& LNode *pTemp = NULL;
  if (beforeWhich & 1 beforeWhich & (m_listLength + 1)) {&&&&&&&& }
  if (!(GetNode(beforeWhich - 1, &pTemp))) {&&&&&&&& }
  LNode *newNode = new LN&&& newNode-&data =&&& newNode-&next = pTemp-&&&& pTemp-&next = newN
  m_listLength++;
  //删除一个指定的节点。&&& //节点位置由position指定。&&& //positon的值从1到listLength。&&& //若链表为空或指定的节点不存在则返回false。&&& bool LinkList::DeleteNode(int position) {&&& if (position & 1 position & m_listLength) {&&&&&&&& }
  LNode *pTemp = NULL;&&& if (!(GetNode(position - 1, &pTemp))) {&&&&&&&& }
  LNode *pDel = NULL;&&& pDel = pTemp-&&&& pTemp-&next = pDel-&&&& delete pD
  m_listLength--;
  //得到指定位置节点的指针。&&& bool LinkList::GetNode(int position, LNode **node) {&&& LNode *pTemp = NULL;&&& int curPos = -1;
  pTemp = m_pL&&& while (pTemp != NULL) {&&&&& curPos++;&&&&& if (curPos == position)&&&&&&&&&&& pTemp = pTemp-&&&& }
  if (curPos != position) {&&&&&&&& }
  *node = pT
  //定位与指定数据相等的数据节点。&&& //如果在当前链表中已经存在该数据则返回该数据节点的索引号。&&& //若不存在这样的节点则返回0。&&& //节点索引从0开始到listLength。&&& int LinkList::LocateElem(int elem) {&&& LNode *pTemp = NULL;&&& int curIndex = 1;
  pTemp = m_pList-&&&& while ((pTemp != NULL) && (pTemp-&data != elem)) {&&&&& pTemp = pTemp-&&&&&& curIndex++;&&& }
  if (pTemp == NULL) {&&&&& return 0;&&& }
  return curI&&& }
  /*&&& int main(){&&& LinkL
  l.InsertNode(1, 10);&&& l.InsertNode(2, 20);&&& l.InsertNode(3, 30);&&& l.InsertNode(4, 40);&&& cout && l.GetLength() &&
  int dataTemp = 0;&&& for (int i = 1; i &= l.GetLength(); i++) {&&&&& l.GetNodeData(i, dataTemp);&&&&& cout && dataTemp &&&&& }
  if (l.SetNodeData(3, 50)) {&&&&& cout &&"DONE\n";&&& } else {&&&&& cout && "Failed\n";&&& }
  for (i = 1; i &= l.GetLength(); i++) {&&&&& l.GetNodeData(i, dataTemp);&&&&& cout && dataTemp &&&&& }
  if (l.DeleteNode(4)) {&&&&& cout &&"DONE\n";&&& } else {&&&&& cout && "Failed\n";&&& }
  for (i = 1; i &= l.GetLength(); i++) {&&&&& l.GetNodeData(i, dataTemp);&&&&& cout && dataTemp &&&&& }
  cout && l.LocateElem(50) &&&&& return 0;&&& }&&& */
H3C认证Java认证Oracle认证
基础英语软考英语项目管理英语职场英语
.NETPowerBuilderWeb开发游戏开发Perl
二级模拟试题一级模拟试题一级考试经验四级考试资料
软件测试软件外包系统分析与建模敏捷开发
法律法规历年试题软考英语网络管理员系统架构设计师信息系统监理师
高级通信工程师考试大纲设备环境综合能力
路由技术网络存储无线网络网络设备
CPMP考试prince2认证项目范围管理项目配置管理项目管理案例项目经理项目干系人管理
职称考试题目
招生信息考研政治
网络安全安全设置工具使用手机安全
生物识别传感器物联网传输层物联网前沿技术物联网案例分析
Java核心技术J2ME教程
Linux系统管理Linux编程Linux安全AIX教程
Windows系统管理Windows教程Windows网络管理Windows故障
数据库开发Sybase数据库Informix数据库
&&&&&&&&&&&&&&&
希赛网 版权所有 & &&

我要回帖

更多关于 c语言链表详解 的文章

 

随机推荐