#include <stdio.h>
#include <stdlib.h> typedef struct linklist
{
unsigned int count;
struct linklist *next;
}linklist; linklist* CreatLinklist(int len)
{
linklist *head = NULL, *pre = NULL;
head = (linklist*)malloc(sizeof(linklist));//头指针。
pre = (linklist*)malloc(sizeof(linklist)); //pre-previous, 前一个;相对于head来说是后一个。
printf("Please input the data:\n");
scanf("%u", &pre->count);
head->next = pre; //head的后一个是pre。
pre->next = NULL; //pre的next是NULL。 for (int i = ; i < len; i++)
{
linklist* tmp = (linklist*)malloc(sizeof(linklist));
scanf("%u", &tmp->count);
pre->next = tmp; //新加的节点放在pre后。
tmp->next = NULL; //新加节点的next是NULL。
pre = tmp; //新加的节点变成pre,等待下一个tmp。
}
return head;
} int Outputlinklist(linklist *head)
{
printf("#### Output linklist.\n");
if (head != NULL)
{
while (head->next != NULL)
{
printf("%u\n", head->next->count);
head = head->next;
}
}
} int Lenlinklist(linklist *head)
{
int len = ;
if (NULL == head->next)
return len;
len++;
linklist *p = NULL;
p = head->next;
while(p->next != NULL)
{
len++;
p = p->next;
}
return len;
} int InsertLinklist(linklist *head, int index)
{
int len = ;
len = Lenlinklist(head);
printf("the length of linklist:%d, the insert index:%d\n", len, index);
if (index < || index > len)
return -;
int cur_index = ;
linklist *p = NULL;
p = head->next;
while(cur_index < index)
{
p = p->next;
cur_index++;
}
linklist *node = (linklist*)malloc(sizeof(linklist));
printf("Please input the new node data:\n");
scanf("%u", &node->count);
node->next = p->next;
p->next = node;
return ;
} int DeleteNode(linklist *head, int index)
{
int len = ;
len = Lenlinklist(head); if((index < ) || (index > len))
return -; int cur_index = ;
linklist *p = NULL;
linklist *q = NULL;
p = head->next;
while(cur_index < index)
{
p = p->next;
cur_index++;
}
q = p->next;
p->next = q->next;
free(q);
q = NULL;
} linklist *Reverselinklist(linklist *head)
{
linklist *p=NULL, *pre=NULL, *pnext=NULL; p = head->next;
while(p!=NULL)
{
pnext = p->next;
if(pnext == NULL)
head->next = p;
p->next = pre;
pre = p;
p = pnext;
}
return head;
} int main(void)
{
linklist *mylist = NULL; printf("#### Creat linklist.\n");
mylist = CreatLinklist();
Outputlinklist(mylist); printf("#### Insert new node.\n");
InsertLinklist(mylist, );
Outputlinklist(mylist); printf("#### Delete exist node.\n");
DeleteNode(mylist, );
Outputlinklist(mylist); printf("#### Reverse Linklist.\n");
Reverselinklist(mylist);
Outputlinklist(mylist); printf("Handle complete!");
}

1.线性表的链式存储和顺序存储的比较。

常见的数组就是线性表的顺序存储,各个结点之间被分配连续的物理内存,因此在查找上直接可以使用下标,速度很快,但是对于插入和删除操作需要在操作点之后执行移动操作,较为麻烦,并且数组在之前需要分配内存的大小,也就是需要我们知道结点的数量,这样就有一定的限制。
链表就是线性表的链式存储,各个结点之间在物理内存中是随机分配的,在查找时需要逐个遍历,但是在插入和删除操作时仅仅是局部性的指针切换,并且链表只有创建结点和删除结点时才会对内存进行操作,事先不需要知道结点数量。

2.单向链表属于线性表链式存储中最简单的一类,常见的操作包括:创建、删除、查找、插入、反序等。

July_One_Week—linked list的更多相关文章

  1. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  2. [LeetCode] Plus One Linked List 链表加一运算

    Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...

  3. [LeetCode] Odd Even Linked List 奇偶链表

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  4. [LeetCode] Delete Node in a Linked List 删除链表的节点

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  7. [LeetCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  8. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  9. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

随机推荐

  1. 关于table动态添加数据 单元格合并 数组合并

    var newArr = [ {"BranchID":1,"BranchName":"城二","BranchFullName&qu ...

  2. Qt中的角度转弧度

    在Qt中,qAsin(),qAtan2()等三角函数的返回值是弧度而不是角度,因此要将弧度转化为角度. 弧度=角度*Pi/180 以qAtan()函数为例 qreal qAtan(qreal v) R ...

  3. python Django 创建应用

    如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”,如下图 创建视图 在 ...

  4. nodejs中function*、yield和Promise的示例

    var co = require("co"); var fs = require("fs"); function cusReadFile(fileName) { ...

  5. 2017-2018-2 20165303 实验二《Java面向对象程序设计》实验报告

    实验一 实验要求 参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 提交最后三个JUnit测试用例(正常情况, ...

  6. c++-pimer-plus-6th-chapter03

    Chapter Review Having more than one integer type lets you choose the type that is best suited to a p ...

  7. LeetCode--441--排列硬币

    问题描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币. 给定一个数字 n,找出可形成完整阶梯行的总行数. n 是一个非负整数,并且在32位有符号整型的范围 ...

  8. (Gorails) activeStore模块,把一堆属性放在一个hash对象内。gem 'activerecord-typedstore'增强了store模块,更好用了

    https://api.rubyonrails.org/classes/ActiveRecord/Store.html https://gorails.com/episodes/preferences ...

  9. 4.1.5 Georigia and Bob

    Problem description: Georgia and Bob 在玩一个游戏. 如图所示,排成直线的格子上放有n个棋子.棋子 i 在左数第 Pi 个格子上.Georgia 和 Bob 轮流选 ...

  10. 性能测试工具 Web Service 性能测试工具比较

    [转自]https://testerhome.com/topics/3003 背景 希望选择一款Web Service性能测试工具,能真实模拟大量用户访问网站时的请求,从而获取服务器当前的请求处理能力 ...