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. C++多线程环境下的构造函数

    多线程的环境里,我们总不可避免要使用锁.于是一个常见的场景就是: class ObjectWithLock { private: std::mutex mtx_; SomeResType shared ...

  2. selenium问题记录

    错误一: 错误信息:Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output: 原 ...

  3. Python的平凡之路(14)

    一.什么是HTML HTML(Hyper Text Mark-up Language ) 即超文本标记语言,是 WWW 的描述语言,由 Tim Berners-lee提出.设计 HTML 语言的目的是 ...

  4. ASP.NET 中HTML和Form辅助方法

    Form辅助方法 Form最重要的属性就是action和method,action指明form中的数据被提交到哪里,method指明用什么方法,默认为GET,下面是一个简单的例子: <form ...

  5. 利用NABCD模型进行竞争性需求分析

    微博的NABCD模型 N-Need:毫无疑问,当今的中国普通民众是有这点需求的,在上个世纪中国民众的休闲娱乐方式更多的停留在以电视传媒为主的娱乐方式,而进入21世纪以来中国民众的娱乐中心向互联网转移, ...

  6. utf8转gbk,libcurl中文乱码处理

    这两个转码在网页客户端处理用很常见,所使用的平台为VS2010,字符集采用多字节字符集 utf8转gbk string UTF8ToGBK(const std::string& strUTF8 ...

  7. Using dijit/Destroyable to build safe Components

    In today's long-lived JavaScript apps it is essential to not introduce memory leaks within your cust ...

  8. 基于.NET的CAD二次开发学习笔记一:CAD开发入门

    1.AutoCAD .NET API由不同的DLL文件组成,它们提供用于访问图形文件或AutoCAD应用程序的包含丰富的类.结构.方法和事件.每一个DLL文件都定义不同的使用基于功能的库组织组件的命名 ...

  9. Jquery仿彩票更换数字动画效果

    <script type="text/javascript" src="jquery-1.11.3.min.js"></script> ...

  10. SQL注入的分类

    基于从服务器接收到的响应    基于错误的SQL注入    联合查询的类型    堆查询注射    SQL盲注        基于布尔SQL盲注        基于时间的SQL盲注        基于 ...