#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. C++.【转】C++数值类型与string的相互转换

    1.C++数值类型与string的相互转换 - JohnGu - 博客园.html(https://www.cnblogs.com/johngu/p/7878029.html) 2. 1.数值类型转换 ...

  2. [osg][opengl]透视投影的参数Perspective

    gluPerspective这个函数指定了观察的视景体(frustum为锥台的意思,通常译为视景体)在世界坐标系中的具体大小,一般而言,其中的参数aspect应该与窗口的宽高比大小相同.比如说,asp ...

  3. [osg]OSG相机添加动画路径

    查看osg坐标系,camare默认姿态:http://www.cnblogs.com/lyggqm/p/8073688.html 首先搞清楚osg的坐标系以及osg::camare的默认姿态 下代码面 ...

  4. Ajax同步

    转载自:https://blog.csdn.net/xiegongmiao/article/details/78217386 AJAX中根据async的值不同分为同步(async = false)和异 ...

  5. CSS 控制鼠标在元素停留的样式

    以下资料来自网络,收藏学习总结用: 有时候需要改变鼠标样式,DIV 可以改成手型等,A也可以改成光标形式 巧合要用到鼠标样式效果,就顺便整理了下十五种CSS鼠标样式,小例子供大家使用啊.CSS鼠标样式 ...

  6. 03-python-装饰器

    装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权 ...

  7. 神兽保佑代码无bug O(∩_∩)O

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. Linux下的常用指令汇总

    内容提纲: 1.ubuntu安装 2.linux目录结构 3.apt.dpkg 4.date.cal.tzselect 5.修改密码.忘记密码 6.注销/重启/关机 7.cd pwd 8.-h --h ...

  9. Python全栈开发-Day4-Python基础4

    本节内容 匿名函数 装饰器 列表生成式.迭代器&生成器 内置函数 Json & pickle 数据序列化 1. 匿名函数 匿名函数就是不需要显式的指定函数 1 2 3 4 5 6 7 ...

  10. xml ----> 几个常用dtd头文件模板

    环境: idea ce 2018.1 "File --> settings... --> Editor --> file and code templates" ...