July_One_Week—linked list
#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的更多相关文章
- [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 ...
- [LeetCode] Plus One Linked List 链表加一运算
Given a non-negative number represented as a singly linked list of digits, plus one to the number. T ...
- [LeetCode] Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- [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 ...
- [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 ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [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 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
随机推荐
- Codeforces 833 C - Ever-Hungry Krakozyabra
思路: 首先,inedible tails 的个数最多为C(18+9,9)个(用隔板法),所以我们暴力出所有的 inedible tails,然后检查一下在[L, R]这段区间是否存在这个inedib ...
- 使用两个栈来完成一个队列,需要是实现的功能有add,poll.peek
2017-06-23 19:15:16 队列时先进先出型,而栈是先进后出型,这就需要建立一个联系.我想到的一个简单的表示方式是: 这样就需要两个栈,栈1是用来实现add操作,即直接push进去就行:栈 ...
- Notepad++安装json插件
安装 : 1.下载插件压缩包并解压出dll:NPPJSONViewer.dll(64位) 下载地址:https://pan.baidu.com/s/1JeBzrovb-GHRo14vO-AnJA 提 ...
- HeadFirst Ruby 第十四章总结 Web apps: Serving HTML
前言 这一章节主要讲了如何利用 Ruby 中的 Sinatra 这个 gem 来创建一个 Web app 的具体流程,其中的要点包括了: Sinatra, a third party library ...
- Entity Framework 学习
Entity Framework 学习初级篇1--EF基本概况 Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry. ...
- Windows Phone 8 开发必备资源
一.MVVM框架推荐 1. MVVM-Light 这个框架是我最常用的MVVM框架之一,它比Prism更轻量级,但对于一般的小应用,功能足够. 官方网站:http://mvvmlight.codepl ...
- oracle 查看被锁表 及解除锁定
查看 哪些表 被锁了 SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$ ...
- Confluence 6 教程:空间高手
这个教程将会带你如何在 Confluence 中创建和自定义空间,同时也包括如何删除空间,如果需要的话.通过这个教程,你将成为使用空间的高手. 你需要具有创建空间(Create space)和创建个人 ...
- Centos 6.5 搭建阿里云yum源
Linux系统下yum源配置(Centos 6) 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo ...
- python-爬虫框架scrapy
一 介绍 Scrapy一个开源和协作的框架,其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可 ...