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. Perl技巧

    项目里面一直用的是Perl,Perl里有各种小技巧就分享在这吧. push(@a, $b) 把b元素压入a数组中, 还可以有 push(@a, [@b]); 那a就成了二维数组了 scalar(@a) ...

  2. Linux下swoole的安装配置

    前几天搭建swoole环境,在安装php的swoole扩展时不知道什么原因,提示成功,但是使用的时候不能加载,最后决定重新安装php试试,顺便记录了php的安装过程 wget http://cn2.p ...

  3. 如何判断pc或者移动端

    <script type="text/javascript"> var userAgentInfo = navigator.userAgent; var Agents ...

  4. #include <vector>用法之我见

    vector是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,(何为动态拓展,即是说如果你知道你要存的数据的个数,你定义的存储数据的数组大小也就决定了,但是若你事先不知道 ...

  5. 炫酷的Linux终端命令大全-1

    1. 命令行日常快捷键. CTRL + U            ------------------------------- 剪切光标前的内容 CTRL + K             ----- ...

  6. Jquery和Javascript 实际项目中写法基础-ajax和json (3)

    一.什么是JSON数据? 一种轻量级的数据交换格式.实际中知道如何使用即可. 软件开发我认为就是一个会用,然后知其原理的过程. 例子如下: <!DOCTYPE html> <html ...

  7. Python为什么要self

    Python要self的理由 Python的类的方法和普通的函数有一个很明显的区别,在类的方法必须有个额外的第一个参数 (self ),但在调用这个方法的时候不必为这个参数赋值 (显胜于隐 的引发). ...

  8. Swift中文基础教程----下标

    类,结构和枚举类型都可以通过定义下标来访问一组或者一个序列中的成员元素.通过下标索引就可以方便地检索和设置相应的值,而不需要其他的额外操作.比如你可以通过someArray[index]来访问数组中的 ...

  9. day27_反射

    1.反射-概述(掌握) 反射就是在程序运行过程中,通过.class文件动态的获取类的信息(属性,构造,方法),并调用 注意:JAVA不是动态语言,因为动态语言强调在程序运行过程中不仅能获取并调用类里面 ...

  10. JSON入门

    一.简介     1.描述         1)JavaScript 对象表示法(JavaScript Object Notation) 2)存储和交换文本信息的语法.类似 XML 3)比 XML 更 ...