Singly Linked List

Singly linked list storage structure:
typedef struct Node
{
ElemType data;
struct Node *next;
}Node;

typedef struct Node *LinkList;

LinkedList without head node:

LinkedList with head node:

Operations:

/*check the size of link list.*/
int ListLength(LinkList *L)
{
i=0;
LinkList p;
p=L->next;
while(!p)
{
p=p->next;
++i;
}
return i;
}

/*return the value of number i data element in list L to e.*/
Status GetElem(LinkList L,int i,ElemType *e)
{
int j;
LinkList p;
/*let p point to the first node of L.*/
p=L->next;
/*j works as a counter.*/
j=1;
/*p is a null and j is not equal to i.*/
while(p&&j<i)
{
/*let p point to the next node.*/
p=p->next;
++j;
}
/*the ith element does not exist.*/
if(!p||j>i)
{
return ERROR;
}
/*get the number i element.*/
*e=p->data;
return OK;
}

List Insert

Status ListInsert(LinkList *L,int i,ElemType e)
{
int j;
LinkList p,s;
p=*L;
j=1;
/*search for number i node.*/
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i)
{
/*number i node does not exist.*/
return ERROR;
}
/*make a new node.*/
s=(LinkList)malloc(sizeof(Node));
s->data=e;
/*key steps:*/
s->next=p->next;
p->next=s;
return OK;

}

List Delete

Status ListDelete(LinkList *L,int i,ElemType *e)
{
int j;
LinkList p,q;
p=*L;
j=1;
/*search for number i node.*/
while(p->next&&j<i)
{
p=p->next;
++j;
}
if(!(p->next)||j>i)
{
/*number i node does not exist.*/
return ERROR;
}
/*key steps*/
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;

}

/*create a list using head inserting method.*/
void CreateListHead(LinkList *L,int n)
{
LinkList p;
int i;
*L=(LinkList)malloc(sizeof(Node));
/*create a linked list with head node.*/
(*L)->next=NULL;
for(i=0;i<n;i++)
{
/*generate a new node.*/
p=(LinkList)malloc(sizeof(Node));
/*store the data of number i node as 100+i.*/
p->data=100+i;
p->next=(*L)->next;
/*insert the node to head.*/
(*L)->next=p;
}
}

/*create a list using tail inserting method.*/
void CreateListTail(LinkList *L,int n)
{
LinkList p,r;
int i;
/*create a linked list with head node.*/
*L=(LinkList)malloc(sizeof(Node));
r=*L;
for(i=0;i<n;i++)
{
/*generate a new node.*/
p=(LinkList)malloc(sizeof(Node));
p->data=100+i;
r->next=p;
r=p;
}
/*marks the end of list.*/
r->next=NULL;
}

/*clear the list to empty.*/
Status ClearList(LinkList *L)
{
LinkList p,q;
/*let p point to first node.*/
p=(*L)->next;
/*while it's not list end.*/
while(p)
{
q=p->next;
free(p);
p=q;
}
/*set the list head pointer to null.*/
(*L)->next=NULL;
return OK;
}

Singly Linked List的更多相关文章

  1. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  2. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  3. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  4. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  5. 单链表反转(Singly Linked Lists in Java)

    单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法   package dsa.linkedlist; public class Node<E> ...

  6. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  7. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...

  8. Singly linked list algorithm implemented by Java

    Jeff Lee blog:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...

  9. Detect loop in a singly linked list

    去Twitter面试的被问到这个问题,当时只想到了用HashMap的办法,这种办法时间复杂度O(n),空间复杂度是O(n), 更好的办法是用 FastRunner / SlowRunner appro ...

随机推荐

  1. 【iCore2 模块相关资料】iM_LAN 100M 以太网模块UDP例程

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  2. mount: /dev/sdb1 already mounted or /mnt/hdb busy 导致NameNode无法启动

    最近,公司由于断电导致的服务器关机,等到来电了,重启集群发现Namenode无法启动,查看原因是由于无法加在faimage文件,在查看一下Namenode的磁盘挂在情况(df -h命令查看),发现磁盘 ...

  3. Alpha版使用说明书

     游戏规则:             玩家是黑色的小煤球哦!             通过方向键或者ASDW,来控制小球移动(上.下.左.右).             累计时间,直到碰到了红色的小球 ...

  4. IIS7+windows 64位配置注意事项

    问题和解决办法 1  如果网站为Asp:再asp中注意启用父路径 2  操作必须使用一个可更新的查询:给用户iis_iusrs 一个完全控制的权限 3  Windows(64位IIS)未在本地计算机上 ...

  5. iOS的触摸事件的用法以及和手势识别器的区别

    1.首先来介绍下触摸事件和手势识别器的利与弊 触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义v ...

  6. 结构及其使用 struct (C#)

    首先结构是值类型. 结构是使用 struct 关键字定义的,结构如下: struct 结构名{} 结构概述 结构具有以下特点: 结构是值类型,而类是引用类型. (结构不能包含显式的无参数构造函数) 与 ...

  7. mybatis配置文件查询参数的传递

    通常来说,参数传递可以使用#与$进行编写,但是使用#的效率更高,使用$方式,查看日志更方便些,尤其是当执行的sql语句非常麻烦的时候. 1) 接口 形式 以下方式 [传递参数是一个实体] public ...

  8. Cosh.1

    不管  先查壳 是VC++的 拿到OD  看看    经过跟踪 发现一个挺神奇的地方 再随便单步单步看看,发现CreateFileA返回的值总是-1 0040137A      83F8 00     ...

  9. ASP.NET项目部署到Linux服务器出现服务器错误

    在Linux系统中安装了Mono和Apache作为Web服务器,使用Visual Studio开发的ASP.NET Web应用或者API应用,在部署到Linux服务器后出现服务器错误,其中一个原因是由 ...

  10. winform客户端利用webClient实现与Web服务端的数据传输

    由于项目需要,最近研究了下WebClient的数据传输.关于WebClient介绍网上有很多详细介绍,大概就是利用WebClient可以实现对Internet资源的访问.无外乎客户端发送请求,服务端处 ...