单链表的基本操作
来源:互联网 发布:91邀请码怎么弄 知乎 编辑:程序博客网 时间:2024/06/13 23:10
今天我们来看看单链表的基本操作:初始化,插入(头插、尾插),查找,删除,判空,求长,摧毁,逆置。
.cpp:
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "list.h"void InitList(List plist){assert(plist != NULL);plist->next = NULL;//plist->data不用操作}//头插bool Insert_Head(List plist,int val){Node *p = (Node *)malloc(sizeof(Node));p->data = val;p->next = plist->next;plist->next = p;return true;}//尾插bool Insert_Tail(List plist,int val){Node *p = (Node *)malloc(sizeof(Node));p->data = val;Node *q;for(q=plist;q->next!=NULL;q=q->next) ;//将p插入在q的后面p->next = q->next;//p->next = NULL;q->next = p;return true;}Node *Search(List plist,int key){for(Node *p=plist->next;p!=NULL;p=p->next){if(p->data == key){return p;}}return NULL;}//查找key的前驱static Node *SearchPri(List plist,int key){for(Node *p=plist;p->next!=NULL;p=p->next){if(p->next->data == key){return p;}}return NULL;}bool Delete(List plist,int key){Node *p = SearchPri(plist,key);if(p == NULL){return false;}Node *q = p->next;p->next = q->next;//将q从链表中剔除//p->next = p->next->next;free(q);return true;}bool IsEmpty(List plist){return plist->next == NULL;}int GetLength(List plist){int count = 0;for(Node *p=plist->next;p!=NULL;p=p->next){count++;}return count;}void Show(List plist){for(Node *p=plist->next;p!=NULL;p=p->next){printf("%d ",p->data);}printf("\n");}void Clear(List plist){Destroy(plist);}void Destroy(List plist){Node *p;while(plist->next != NULL)//第一种方法总删除第一个数据节点{p = plist->next;plist->next = p->next;free(p);}/*Node *p = plist->next;//第二种方法//Node *q = p->next;//bugNode *q;while(p != NULL){q = p->next;free(p);p = q;}plist->next = NULL;*/}void Revers(List plist)//逆置{//第一种方法头插的思想if(plist==NULL || plist->next==NULL || plist->next->next==NULL){return ;}Node *p = plist->next;Node *q;plist->next = NULL;//while(p != NULL){q = p->next;//将p头插进链表p->next = plist->next;plist->next = p;p = q;}//第二种方法#if 0/*if(plist->next->next==NULL || plist==NULL || plist->next==NULL )*///errorif(plist==NULL || plist->next==NULL || plist->next->next==NULL){return ;}Node*p = plist->next;Node *q = p->next;Node *s;p->next = NULL;while(q != NULL){s = q->next;q->next = p;p = q;q = s;}plist->next = p;#endif}
.h:
#pragma once//带头节点的单链表typedef struct Node{int data;//数据域struct Node *next;//指向下一个节点}Node,*List;//List == Node*void InitList(List plist);//Node *plist//头插bool Insert_Head(List plist,int val);//尾插bool Insert_Tail(List plist,int val);Node *Search(List plist,int key);//List Node *Search_before(List plist,int key);bool Delete(List plist,int key);bool IsEmpty(List plist);int GetLength(List plist);void Show(List plist);void Clear(List plist);void Destroy(List plist);void Revers(List plist);
阅读全文
0 0
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作!
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- Maven仓库jar包冲突解决篇
- 排序删除
- mysql分页原理和高效率的mysql分页查询语句
- java并发实战第五章
- java作业4
- 单链表的基本操作
- 求一个n阶方阵对角线元素之和
- AngularJS购物车代码
- ListView 使用方法详解
- css纵向标尺
- 用小说的形式讲解Spring(1) —— 为什么需要依赖注入
- 数据结构基础算法-循环链表
- 多尺度小波分解与重构,matlab,对系数进行处理后,构造C,重构信号
- C++打印今天年月日,时间和星期几。