数据结构链表基本操作,链表实现

扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
数据结构课程设计---用链表实现通讯录
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口页面导航:
→ 正文内容 java单链表
java实现数据结构单链表示例(java单链表)
这篇文章主要介绍了java数据结构实现单链表示例,需要的朋友可以参考下
代码如下:/**&* 单向链表&*&*/public class NodeList&E& {&private static class Node&E& { // 节点类&&E // 节点上的数据&&Node&E& // 指向下一个节点
&&Node(E e) {&&&this.data =&&&this.next =&&}&}
&private Node&E& // 链表的头节点&private Node&E& // 链表的尾节点&private Node&E& other =&private int length = 0; // 节点数量
&/**& * 无参构造方法& */&public NodeList() {&&// 默认节点为空&&this.head = new Node&E&(null);&}
&/**& * 初始化时创建一个节点& * & * @param data& *&&&&&&&&&&& 数据& */&public NodeList(E data) {&&this.head = new Node&E&(data);&&this.last =&&length++;&}
&/**& * 添加一个节点(尾插法)& * & * @param data& *&&&&&&&&&&& 数据& */&public void add(E data) {&&if (isEmpty()) {&&&head = new Node&E&(data);&&&last =&&&length++;&&} else {&&&Node&E& newNode = new Node&E&(data);&&&last.next = newN&&&last = newN&&}&}&/**& * 获得索引处的数据(索引输入错误抛出越界异常)& * @param index 索引& * @return 索引处数据& */&public E get(int index){&&if(index&0 || index&length){&&&throw new IndexOutOfBoundsException("索引越界:"+index);&&}&&other =&&for(int i=0;i&i++){&&&other = other.&&}&&return other.&}&/**& * 新值替换旧值& * @return 成功为true,未找到为false& */&public boolean set(E oldValue,E newValue){&&other =&&while(other!=null){&&&if(other.data.equals(oldValue)){&&&&other.data = newV&&&&&&&}&&&other = other.&&}&&&}&/**& * 在指定元素后插入一个元素& * & * @param data& *&&&&&&&&&&& 指定的元素& * @param insertData& *&&&&&&&&&&& 需要插入的元素& * @return false为未找到元素,true为插入成功& */&public boolean add(E data, E insertData) {&&other =&&while (other != null) {&&&if (other.data.equals(data)) {&&&&Node&E& newNode = new Node&E&(insertData);&&&&Node&E& temp = other.&&&&newNode.next =&&&&other.next = newN&&&&length++;&&&&&&&}&&&other = other.&&}&&&}
&/**& * 链表中是否包含此元素& * @return 包含为true,不包含为false& */&public boolean contains(E data){&&other =&&while(other!=null){&&&if(other.data.equals(data)){&&&&&&&}&&&other = other.&&}&&&}&/**& * 移除指定的元素& * @param data 需要移除的元素& * @return 不存在为false,成功为true& */&public boolean remove(E data){&&other =&&Node&E& temp =&&//临时变量,用于保存前一个节点&&while(other!=null){&&&if(other.data.equals(data)){&&&&temp.next = other.&&&&length--;&&&&&&&}&&&temp =&&&other = other.&&}&&&}&/**& * 判断链表是否为空& * & * @return 空为true,非空为false& */&public boolean isEmpty() {&&return length == 0;&}
&/**& * 清空链表& */&public void clear() {&&this.head =&&this.length = 0;&}
&/**& * 输出所有节点& */&public void printLink() {&&if(isEmpty()){&&&System.out.println("空链表");&&}else{&&&other =&&&while (other != null) {&&&&System.out.print(other.data);&&&&other = other.&&&}&&&System.out.println();&&}&}}
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910数据结构:单链表的实现:查找给定值的前驱后继问题_c吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:71,901贴子:
数据结构:单链表的实现:查找给定值的前驱后继问题收藏
问题: 核心已转储(可能是越界了,它是怎么发生的?而且我要查找的单前字符在链表中已经存在了啊。。)代码如下:///////////////////////////////////////LinkList.h////////////////////////////////////////////////#define ERROR -1#define OVERFLOW -2#define TRUE 1#define FALSE 0typedef char /*typename1*/ Etypedef char/*Statue*/ SStatus visit(Elemtype *);typedef struct LNode{
struct LNode *}LNode, *LinkL///////////////////////////////////////问题函数/////////////////////////////////////////////////////////Status PriorElem_l(LinkList L,Elemtype *pcur_e,Elemtype *ppre_e)
LNode *p, *q;
exit(ERROR);
if(!(p=L-&next)&&!(q=p-&next))
return FALSE;
if(q-&data==*pcur_e)
*ppre_e=p-&
return TRUE;
return FALSE;
*ppre_e=p-&
return TRUE;
}///////////////////////////////main()//////////////////////////////////////////////////////////////////Locate().Prior().Next()//链表中存有26个字母(小写)
temp='h';
i=LocateElem_l(L,&temp,eq);//运行正常
PriorElem_l(L,&temp,&temp2);
printf(&the charater h 's prior is %c,\n&,temp2);
NextElem_l(L,&temp,&temp2);
printf(& and next is %c.\n&,temp2);
printf(&Locate error!&);
TIP:链表使用了头结点指向第一个数据节点,数据节点的引索从0开始!!
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或一步一步学数据结构之n--n(图--邻接链表法实现) - 推酷
一步一步学数据结构之n--n(图--邻接链表法实现)
&&&&&& 在上篇文章已经介绍过图的内容,而且还介绍了用邻接矩阵实现的图,而在本篇文章,就说下用邻接链表实现的图。
&&&&&& 常做的操作可以在上篇文章中查找,因为两中方法实现的图操作都是一模一样的。
&代码总分为三个文件:
(LGraph.h&,&LGraph.c&,&Main.c&&)。
整体结构图为:
&&&&&&&简单说下邻接链表法:
&&&&&&&&&&
&&&&& 如果对邻接链表法还不懂的话,建议拿直笔把图画下和代码结合着看,代码里面也有注释,希望能帮到你,如果还不懂的话,可以在文章下面评论留言!
&&&&& OK!&上代码:
&&&&&&LGraph.h&,
#ifndef _LGRAPH_H_
#define _LGRAPH_H_
typedef void LG
typedef void LV
typedef void (LGraph_Printf)(LVertex*);
/* 创建并返回有n个顶点的图 */
LGraph* LGraph_Create(LVertex** v, int n);
/* 销毁graph所指向的图 */
void LGraph_Destory(LGraph* graph);
/* 将graph所指图的边集合清空 */
void LGraph_Clear(LGraph* graph);
/* 在graph所指图中的v1和v2之间加上边,且边的权为w */
int LGraph_AddEdge(LGraph* graph, int v1, int v2, int w);
/* 将graph所指图中v1和v2之间的边删除,返回权值 */
int LGraph_RemoveEdge(LGraph* graph, int v1, int v2);
/* 将graph所指图中v1和v2之间的边的权值返回 */
int LGraph_GetEdge(LGraph* graph, int v1, int v2);
/* 将graph所指图中v顶点的度数 */
int LGraph_TD(LGraph* graph, int v);
/* 将graph所指图中的顶点数返回 */
int LGraph_VertexCount(LGraph* graph);
/* 将graph所指图中的边数返回 */
int LGraph_EdgeCount(LGraph* graph);
/* 将graph所指图输出 */
void LGraph_Display(LGraph* graph, LGraph_Printf* pFunc);
&&&&& LGraph.c&,&
#include &stdio.h&
#include &malloc.h&
#include &LGraph.h&
#include &LinkList.h&
typedef struct _tag_LGraph
{//头结点定义
//顶点个数
LVertex**//顶点相关的数据
LinkList**//链表数组
typedef struct _tag_ListNode
{//边描述结点
//边指向的顶点标号
LGraph* LGraph_Create(LVertex** v, int n)
TLGraph* ret = NULL;
int ok = 0;
if((NULL != v) && (0 & n))
{//申请头结点内存
ret = (TLGraph*)malloc(sizeof(TLGraph));
if(NULL != ret)
ret-&count =
//通过二级指针动态申请一维指针数组
ret-&v = (LVertex**)calloc(n, sizeof(LVertex*));
ret-&la = (LinkList**)calloc(n, sizeof(LinkList*));
ok = (NULL != ret-&v) && (NULL != ret-&la);
int i = 0;
for(i=0; i&n; i++)
{//把指针数组和数据空间连接起来
ret-&v[i] = v[i];
for(i=0; (i&n)&& i++)
{//为指针数组每个指针创建链表
ok = ok && (NULL != (ret-&la[i] = LinkList_Create()));
if(NULL != ret-&la)
int i = 0;
for(i=0; i&n; i++)
LinkList_Destroy(ret-&la[i]);
free(ret-&v);
free(ret-&la);
free(ret);
ret = NULL;
void LGraph_Destroy(LGraph* graph)
TLGraph* tGraph = (TLGraph*)
//先清理,再销毁
LGraph_Clear(graph);
if(NULL != tGraph)
int i = 0;
for(i=0; i&tGraph-& i++)
{//销毁链表数组中的链表
LinkList_Destroy(tGraph-&la[i]);
free(tGraph-&v);
free(tGraph-&la);
free(tGraph);
void LGraph_Clear(LGraph* graph)
TLGraph* tGraph = (TLGraph*)
if(NULL != tGraph)
int i = 0;
for(i=0; i&tGraph-& i++)
{//遍历链表数组
while(0 & LinkList_Length(tGraph-&la[i]))
{//把链表数组中每一个链表的每一个结点所占的内存都释放掉
free(LinkList_Delete(tGraph-&la[i], 0));
int LGraph_AddEdge(LGraph* graph, int v1, int v2, int w)
{//(v1---&v2
TLGraph* tGraph = (TLGraph*)
TListNode* node = NULL;
//安全性检测
int ret = (NULL != tGraph);
ret = ret && (0 &= v1) && (v1 & tGraph-&count);
ret = ret && (0 &= v2) && (v2 & tGraph-&count);
ret = ret && (0 &=
w) && (NULL != (node = (TListNode*)malloc(sizeof(TListNode))));
node-&v = v2;//赋入顶点
node-&w =//赋入权值
//插入到v1链表中,因为v1链表中的结点都是v1的出度
LinkList_Insert(tGraph-&la[v1], (LinkListNode*)node, 0);
int LGraph_RemoveEdge(LGraph* graph, int v1, int v2)
{//(v1---&v2 删除v1到v2的边)
TLGraph* tGraph = (TLGraph*)
int ret = 0;
//安全性检测
int condition = (NULL != tGraph);
condition = condition && (0 &= v1) && (v1 & tGraph-&count);
condition = condition && (0 &= v2) && (v2 & tGraph-&count);
if(condition)
TListNode* node = NULL;
int i = 0;
for(i=0; i&tGraph-& i++)
{//要删除v1到v2的边,就要从v1链表中寻找
node = (TListNode*)LinkList_Get(tGraph-&la[v1], i);
if(node-&v == v2)
{//匹配到的话 就删除
ret = node-&w;
LinkList_Delete(tGraph-&la[v1], i);
free(node);
int LGraph_GetEdge(LGraph* graph, int v1, int v2)
{//(v1---&v2
获取v1到v2的边的权值)
TLGraph* tGraph = (TLGraph*)
int ret = 0;
//安全性检测
int condition = (NULL != tGraph);
condition = condition && (0 &= v1) && (v1 & tGraph-&count);
condition = condition && (0 &= v2) && (v2 & tGraph-&count);
if(condition)
int i = 0;
for(i=0; i&tGraph-& i++)
{//从v1链表中寻找
TListNode* node = (TListNode*)LinkList_Get(tGraph-&la[v1], i);
if(node-&v == v2)
{//匹配好以后把边的权值返回
ret = node-&w;
int LGraph_TD(LGraph* graph, int v)
{//获取v顶点的出度和入度和
TLGraph* tGraph = (TLGraph*)
int ret = 0;
int condition = (NULL != tGraph);
condition = condition && (0 &= v) && (v & tGraph-&count);
if(condition)
int i = 0;
int j = 0;
for(i=0; i&tGraph-& i++)
{//遍历链表数组中每一个链表
for(j=0; j&LinkList_Length(tGraph-&la[i]); j++)
{// 遍历每一个链表中的每一个结点
TListNode* node = (TListNode*)LinkList_Get(tGraph-&la[i], j);
if(node-&v == v)
{//匹配即进行自加
//最后再加上v的出度,也就是v链表的长度
ret += LinkList_Length(tGraph-&la[v]);
int LGraph_VertexCount(LGraph* graph)
{//获取顶点个数
TLGraph* tGraph = (TLGraph*)
int ret = 0;
if(NULL != tGraph)
ret = tGraph-&
int LGraph_EdgeCount(LGraph* graph)
{//获取边的个数
TLGraph* tGraph = (TLGraph*)
int ret = 0;
if(NULL != tGraph)
int i = 0;
for(i=0; i&tGraph-& i++)
{//遍历链表数组中的每一个链表,获取长度,计算总和
ret += LinkList_Length(tGraph-&la[i]);
void LGraph_Display(LGraph* graph, LGraph_Printf* pFunc)
TLGraph* tGraph = (TLGraph*)
if((NULL!=tGraph) && (NULL!=pFunc))
int i = 0;
int j = 0;
for(i=0; i&tGraph-& i++)
{//输出顶点信息
printf(&%d: &, i);
pFunc(tGraph-&v[i]);
printf(&\n&);
printf(&\n&);
//输出边信息 包括 &xx顶点---&xx顶点 权值:xx&
for(i=0; i&tGraph-& i++)
for(j=0; j&LinkList_Length(tGraph-&la[i]); j++)
TListNode* node
= (TListNode*)LinkList_Get(tGraph-&la[i], j);
printf(&&&);
pFunc(tGraph-&v[i]);
printf(& , &);
pFunc(tGraph-&v[node-&v]);
printf(& : %d&\n&, node-&w);
printf(&\n&);
&&&&& Main.c&&
#include &stdio.h&
#include &LGraph.h&
void print_data(LVertex* v)
if(NULL != v)
printf(&%s&, (char*)v);
int main(void)
LVertex* v[] = {&A&, &B&, &C&, &D&, &E&, &F&};
LGraph* graph = LGraph_Create(v, 6);
LGraph_AddEdge(graph, 0, 1, 1);
LGraph_AddEdge(graph, 0, 2, 1);
LGraph_AddEdge(graph, 0, 3, 1);
LGraph_AddEdge(graph, 1, 5, 1);
LGraph_AddEdge(graph, 1, 4, 1);
LGraph_AddEdge(graph, 2, 1, 1);
LGraph_AddEdge(graph, 3, 4, 1);
LGraph_AddEdge(graph, 4, 2, 1);
LGraph_Display(graph, print_data);
printf(&2 TD
: %d\n&, LGraph_TD(graph, 2));
printf(&Vertex: %d\n&, LGraph_VertexCount(graph));
printf(&Edge
: %d\n&, LGraph_EdgeCount(graph));
LGraph_Destroy(graph);
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示}

我要回帖

更多关于 数据结构单链表的实现 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信