(C)单链表
老师版
- #include <stdio.h>
- #include <stdlib.h>
- // 定于Node数据类型
- struct Node
- {
- int data; // 数据域
- struct Node *next; // 指针域
- };
- // 创建一个单链表,并把head节点返回;
- struct Node* createLinkList(void);
- struct Node* createLinkList(void)
- {
- struct Node *head = NULL;
- struct Node *tail = NULL;
- struct Node *temp = NULL;
- int data;
- scanf("%d",&data);
- // data 不等于0
- while (data)
- {
- // malloc 函数申请内存
- temp = (struct Node *)malloc(sizeof(struct Node));
- temp->data = data;
- temp->next = NULL;
- if (head == NULL)
- {
- head = temp;
- tail = temp;
- }
- else
- {
- tail->next = temp;
- tail = temp;
- }
- scanf("%d",&data);
- }
- return head;
- }
- // 输出链表元素
- void printLinkList(struct Node *head);
- void printLinkList(struct Node *head)
- {
- struct Node *p = head;
- if (p == NULL)
- {
- return;
- }
- else
- {
- while (p)
- {
- printf("%d-》",p->data);
- // 指针重定向
- p = p->next;
- }
- }
- }
- // 计算链表的长度
- int count(struct Node *head);
- int count(struct Node *head)
- {
- int count = ;
- struct Node *p = NULL;
- p = head;
- while (p)
- {
- count++;
- p = p->next;
- }
- return count;
- }
- int getNode(struct Node *head,int pos);
- int getNode(struct Node *head,int pos)
- {
- if (pos > count(head))
- {
- return ;
- }
- struct Node *p = head;
- for (int i = ; i < pos; i++)
- {
- p = p->next;
- }
- return p->data;
- }
- void insertIntoHead(struct Node **h,int value);
- void insertIntoHead(struct Node **h,int value)
- {
- struct Node *temp = NULL;
- temp = (struct Node *)malloc(sizeof(struct Node));
- temp->data = value;
- temp->next = NULL;
- if (h == NULL)
- {
- *h = temp;
- }
- else
- {
- temp->next = *h;
- *h = temp;
- }
- }
- //2、把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
- int modifyNode(struct Node *head,int pos,int x);
- int modifyNode(struct Node *head,int pos,int x)
- {
- // 如果链表为空,或者pos 超出链表长度
- if (head == NULL || pos > count(head))
- {
- return ;
- }
- struct Node *p = NULL;
- p = head;
- for (int i = ; i < pos; i++)
- {
- p = p->next;
- }
- p->data = x;
- return ;
- }
- void insertIntoTail(struct Node **head,int value);
- void insertIntoTail(struct Node **head,int value)
- {
- struct Node *tmp = NULL;
- tmp = malloc(sizeof(struct Node));
- tmp->data = value;
- tmp->next = NULL;
- if (*head == NULL)
- {
- *head = tmp;
- }
- else
- {
- struct Node *p;
- // 定位最后一个节点
- p = *head;
- while (p->next)
- {
- p = p->next;
- }
- // 将申请的tmp加到链表后面
- p->next = tmp;
- }
- }
- int insertInto(struct Node **head,int pos,int value);
- int insertInto(struct Node **head,int pos,int value)
- {
- if (*head == NULL)
- {
- // 在第一个位置添加一个节点
- insertIntoHead(head, value);
- return ;
- }
- if (pos > count(*head))
- {
- // 在最后一个位置添加一个节点
- insertIntoTail(head, value);
- return ;
- }
- // 申请一个节点
- struct Node *tmp;
- tmp = malloc(sizeof(struct Node));
- tmp->data = value;
- tmp->next = NULL;
- if (tmp==NULL)//tmp 申请内存失败
- {
- return ;
- }
- else
- {
- // 声明一个辅助指针
- struct Node *p;
- p = *head;
- // 定位辅助指针,指向pos位置前一个节点
- for (int i = ; i<pos-; i++)
- {
- p = p->next;
- }
- tmp->next = p->next;
- p->next = tmp;
- return ;
- }
- }
- void insertIntoSortedList(struct Node **head,int value);
- void insertIntoSortedList(struct Node **head,int value)
- {
- // 如果是链表为空,在第一个位置添加
- if (*head == NULL)
- {
- insertIntoHead(head, value);
- return;
- }
- // 记录要添加元素的位置
- int pos = ;
- struct Node *p = NULL;
- p = *head;
- while (p)
- {
- if (p->data < value)
- {
- pos++;
- p = p->next;
- }
- else
- {
- // 跳出循环
- break;
- }
- }
- insertInto(head, pos, value);
- }
- int deleteFirstNode(struct Node **head);
- int deleteFirstNode(struct Node **head)
- {
- if (*head == NULL)
- {
- return ;
- }
- struct Node *p = NULL;
- p = *head;
- *head = p->next;
- int result = p->data;
- // 注意释放内存
- free(p);
- p = NULL;
- return result;
- }
- int deleteTailNode(struct Node **head);
- int deleteTailNode(struct Node **head)
- {
- if (*head == NULL)
- {
- return ;
- }
- struct Node *p,*q;
- p = q = NULL;
- p = *head;
- q = p->next;
- // 通过循环定位让q指向最后一个节点,p指向q前面一个节点
- for (int i = ; i <= count(*head)-; i++)
- {
- p = q;
- q = q->next;
- }
- // 取最后一个节点的数据
- int result = q->data;
- p->next = NULL;
- free(q);
- q = NULL;
- return result;
- }
- int main(int argc, const char * argv[])
- {
- struct Node *head = NULL;
- // 产生链表
- head = createLinkList();
- // 输出链表元素个数
- int c = count(head);
- printf("c = %d\n",c);
- //insertIntoHead(&head, 10);
- //modifyNode(head, 2, 10);
- //insertIntoTail(&head, 100);
- //insertInto(&head, 2, 100);
- //insertIntoSortedList(&head, 6);
- //deleteFirstNode(&head);
- int r = deleteTailNode(&head);
- printf("删除最后一个节点%d\n",r);
- // 输出链表
- printLinkList(head);
- /*
- // 输出第二个节点的数据域;
- c = getNode(head, 2);
- printf("\n");
- printf("c = %d\n",c);
- */
- return ;
- }
不才版
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int data;
- struct Node *next;
- };
- struct Node *creatLinkList(void)
- {
- struct Node *head=NULL;
- struct Node *tail=NULL;
- struct Node *temp=NULL;
- int data;
- scanf("%d",&data);
- while (data)
- {
- temp=malloc(sizeof(struct Node));
- temp->data=data;
- temp->next=NULL;
- if(head==NULL)
- head=tail=temp;
- else
- {
- tail->next=temp;
- tail=temp;
- }
- scanf("%d",&data);
- }
- return head;
- }
- void printLinkList(struct Node *head)
- {
- struct Node *tmp=head;
- if (tmp==NULL) {
- return;
- }
- while (tmp!=NULL) {
- printf("%d->>",tmp->data);
- tmp=tmp->next;
- }
- printf("\n");
- }
- int toNode(struct Node *head,int pos)
- {
- int data;
- for (int i=; i<pos; i++) {
- if ((i!=pos-)&&(head->next==NULL))
- return ;
- data=head->data;
- head=head->next;
- }
- return data;
- //链表长度可以用count(head)算出。
- }
- int changeData(struct Node *head,int pos,int x)
- {
- for (int i=; i<pos; i++) {
- if ((i!=pos-)&&(head->next==NULL))
- return ;
- if (i==pos-) {
- head->data=x;
- }
- head=head->next;
- }
- return ;
- }
- void insertNodeInHead(struct Node **head,int value)
- {
- struct Node *tmp=NULL;
- tmp=(struct Node *)malloc(sizeof(struct Node));
- tmp->data=value;
- tmp->next=*head;
- *head=tmp;
- }
- void insertNodeInEnd(struct Node *head,int value)
- {
- struct Node *tmp=NULL;
- tmp=(struct Node *)malloc(sizeof(struct Node));
- tmp->data=value;
- tmp->next=NULL;
- if (head==NULL) {
- head=tmp;
- }
- while (head->next!=NULL) {
- head=head->next;
- }
- head->next=tmp;
- }
- int insertData(struct Node *head,int pos,int value)
- {
- struct Node *tmp=NULL;
- tmp=(struct Node *)malloc(sizeof(struct Node));
- if (tmp==NULL) {
- return ;
- }
- tmp->data=value;
- if (pos==) {
- return ;
- }
- for (int i=; i<pos; i++) {
- if ((i!=pos-)&&(head->next==NULL)) {
- return ;
- }
- if (i==pos-) {
- tmp->next=head->next;
- head->next=tmp;
- }
- head=head->next;
- }
- return ;
- }
- void insertSortData(struct Node *head,int value)
- {
- struct Node *tmp=(struct Node *)malloc(sizeof(struct Node));
- tmp->data=value;
- while (head->data<tmp->data) {
- if (head->next->data>=tmp->data) {
- tmp->next=head->next;
- head->next=tmp;
- return;
- }
- if (head->next==NULL) {
- tmp->next=head->next;
- head->next=tmp;
- return;
- }
- head=head->next;
- }
- }
- int deleteHead(struct Node **pointhead)
- {
- int data;
- data=(*pointhead)->data;
- struct Node *p=*pointhead;
- *pointhead=(*pointhead)->next;
- free(p);
- p=NULL;
- if (*pointhead==NULL||(*pointhead)->next==NULL) {
- return ;
- }
- return data;
- }
- int deleteEnd(struct Node *head)
- {
- struct Node *tmp=head;
- int data;
- if(head==NULL||head->next==NULL)
- return ;
- while (tmp->next!=NULL) {
- if (tmp->next->next==NULL) {
- data=tmp->next->data;
- free(tmp->next);
- tmp->next=NULL;
- break;
- }
- tmp=tmp->next;
- }
- return data;
- }
- int main(int argc,const char *argv[])
- {
- struct Node *h = creatLinkList();
- printLinkList(h);
- //1、返回单链表中第pos个结点中的元素,若pos超出范围,则返回0
- printf("%d\n",toNode(h,));
- //2、把单链表中第pos个结点的值修改为x的值,若修改成功返回1,否则返回0
- if (changeData(h,,)) {
- printLinkList(h);
- }
- //3、向单链表的表头插入一个元素
- insertNodeInHead(&h, );
- printLinkList(h);
- //4、向单链表的末尾添加一个元素
- insertNodeInEnd(h, );
- printLinkList(h);
- //5、向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
- if(insertData(h, , ))
- printLinkList(h);
- //6、向有序单链表中插入元素x结点,使得插入后仍然有序
- insertSortData(h, );
- printLinkList(h);
- //7、从单链表中删除表头结点,并把该结点的值返回,若删除失败则返回0
- printf("%d\n",deleteHead(&h));
- printLinkList(h);
- //8、从单链表中删除表尾结点并返回它的值,若删除失败则返回0
- printf("%d\n",deleteEnd(h));
- printLinkList(h);
- return ;
- }
(C)单链表的更多相关文章
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- c++单链表基本功能
head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- 分离的思想结合单链表实现级联组件:CascadeView
本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- Android应用开发基础篇(2)-----Notification(状态栏通知)
一.概述 Notification这个部件的功能是在状态栏里显示消息提醒,比如有未读的短信或者是未接的电话,那么状态栏里都会有显示,更或者是从某个应用(比如QQ,酷我音乐等等)里按Home键 ...
- OpenCV---图片生成视频
/** It is a batch processing interface. */ #include "stdafx.h" #include <windows.h> ...
- UVa340 Master-Mind Hints
#include <stdio.h>#include <string.h> #define MIN(a,b) (((a) < (b)) ? (a) : (b)) int ...
- C++的常量折叠(一)
前言 前几天女票问了我一个阿里的面试题,是有关C++语言的const常量的,其实她一提出来我就知道考察的点了:肯定是const常量的内存不是分配在read-only的存储区的,const常量的内存分配 ...
- 设计模式的PHP实现示例(转)
symfony2 很多设计模式思想,下面的资料会有点帮助:http://www.open-open.com/lib/view/open1414996676559.html 阅读目录 Creationa ...
- Protel99 SE快捷键大全
为了方便观看我们的protel99 se视频教程的朋友,我们在这里发布了protel99 se的所有的键盘的快捷分健大全,希望大家在学习我们的视频教程的时候,可以熟悉以下这些快捷键,因为平时我们用pr ...
- Android开发中怎样调用系统Email发送邮件(多种调用方式)
在Android中调用其他程序进行相关处理,几乎都是使用的Intent,所以,Email也不例外,所谓的调用Email,只是说Email可以接收Intent并做这些事情 我们都知道,在Android中 ...
- 使用Win32 API创建不规则形状&带透明色的窗口
前一阵突然想起了9月份电面某公司实习时的二面题,大概就是说怎么用Win32 API实现一个透明的窗口,估计当时我的脑残答案肯定让面试官哭笑不得吧.所以本人决定好好研究下这个问题.经过一下午的摸索,基本 ...
- HDU 1983 Kaitou Kid - The Phantom Thief (2)
神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...
- VS2010/MFC:模态对话框及其弹出过程
模态对话框及其弹出过程 加法计算器对话框程序大家照着做一遍后,相信对基于对话框的程序有些了解了,有个好的开始对于以后的学习大有裨益.趁热打铁,这一节讲讲什么是模态对话框和非模态对话框,以及模态对话框怎 ...