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. js实现当前导航菜单高亮显示

    为了增加用户体验度,增加网页的易用性和美观度,往往需要把当前导航菜单以特殊方式显示,通常是高亮显示或有不同于其它菜单的背景,有两种方法可以实现,第一种是用纯css来实现,二是用js辅助css来实现,两 ...

  2. Python学习笔记-抽象

    懒惰即美德.代码量少. hasattr判断函数是否可用. 创建函数. def hello(name) return 'hello,'+name+'!' 文档化函数: 加注释(#开头) 文档字符串.函数 ...

  3. js②

    操作符 ECMA-262描述了一组用于操作数据值的操作符,包括算术操作符(如加号和减号).位操作符.关系操作符和相等操作符. 一元操作符 递增和递减操作符(++ --) 一元加和减操作符 对非数值应用 ...

  4. Python的平凡之路(19)

    一.Django请求生命周期   对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端                                          ...

  5. 第五篇——C++实现四则运算

    写一个能自动生成小学四则运算题目的命令行 “软件”, 分别满足下面的各种需求.下面这些需求都可以用命令行参数的形式来指定: a) 除了整数以外,还要支持真分数的四则运算. (例如: 1/6 + 1/8 ...

  6. eclipse为方法添加注释的快捷键是什么

    /** * 登录验证 * @param 传入的vo类 * @return * @throws Exception */这种注释快捷键?   先敲“/”在敲两个**,然后回车

  7. [SHELL]判断一个命令是否存在

    首先要说明的是,不要使用which来进行判断,理由如下: 1.which非SHELL的内置命令,用起来比内置命令的开销大,并且非内置命令会依赖平台的实现,不同平台的实现可能不同. # type typ ...

  8. 【JavaScript】之【Object】

    见代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  9. 安装PLSQL,登录报“无法解析指定的连接标识符的错误”

    安装PLSQL,本地不需要安装oracle服务器,但要安装oracle客户端. 一.安装客户端需要配置服务命名,tnsnames.oRA文件和监听(因为我开始没有配置监听,所以一直报无法解析制定的连接 ...

  10. 深入理解JavaScript系列:试着谈谈闭包

    闭包可能是JavaScript里最被人神乎其神的一个概念,世间万物皆凡夫俗子,你觉着他神奇是因为你根本没有了解,所有的事物当你了解透彻后就不会有这种不明觉厉的错觉了.哈哈哈,上来又是一顿哲学普及. 下 ...