单链表的概念:(只有一个指针结点)
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。(简单讲就是逻辑相邻,物理不相邻)
单链表分类:
- 不带头结点:
- 带头结点:
带头结点:
list.h
#pragma once
//带头节点的单链表,尾节点的next为NULL
//头节点起哨兵位作用,它不使用(数据域不能存储数据)
typedef struct Node
{
int data; //保存数据
struct Node *next; //保存下一节点的地址,struct必须要写
}Node,*List; //List == Node*
//链表初始化
void InitList(List plist);
//头插
bool Insert_head(List plist,int val);
//尾插
bool Insert_tail(List plist,int val);
//查找
Node *Search(List plist,int key);
//删除
bool Delete(List plist,int key);
//获取单链表的长度
int GetLength(List plist);
//判空
bool IsEmpty(List plist);
//清空
void Clear(List plist);
//摧毁
void Destroy(List plist);
//打印
void Show(List plist);
//获取元素
bool GetElem(List plist,int pos,int *rtval);
//逆置
void Reverse(List plist);
//除去重复的数据值
void Unique(List plist);
list.cpp
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"list.h"
//链表初始化
void InitList(List plist)
{
assert(plist != NULL);
if(plist == NULL)
{
return ;
}
plist->next = NULL;
}
头插法:
如图:头结点是*L。。。头结点一般储存单链表的长度的信息。
首节点是(*L)->next。。。是储存元素的值和下一个元素的位置的信息。
现在我想插入一个新的节点p。。。
第一:新节点的指针域指向首节点。
第二:修改头结点的指针域,使其指向新节点p。
//头插 时间复杂度O(1)
bool Insert_head(List plist,int val)
{
assert(plist != NULL);
if(plist == NULL)
{
return false;
}
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);
q->next = p;
p->next = NULL;
return true;
}
//查找
Node *Search(List plist,int key)
{
assert(plist != NULL);
if(plist == NULL)
{
return false;
}
for(Node *p = plist->next;p != NULL;p = p->next)
{
if(p->data == key)
{
return p;
}
}
return NULL;
}
//删除
bool Delete(List plist,int key)
{
Node *p;
for(p = plist;p->next != NULL;p = p->next)
{
if(p->next->data == key)
{
Node *q = p->next;//记录即将要删除的结点
p->next = q->next;
free(q);
return true;
}
}
return false;
}
//获取单链表的长度
int GetLength(List plist)
{
int count = 0;
for(Node *p = plist->next;p != NULL;p = p->next)
{
count++;
}
return count;
}
//判空
bool IsEmpty(List plist)
{
return plist->next == NULL;
}
//清空
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);
}
}
//打印
void Show(List plist)
{
for(Node *p = plist->next;p != NULL;p = p->next)
{
printf("%d ",p->data);
}
printf("\n");
}
//获取元素
bool GetElem(List plist,int pos,int *rtval)
{
if(pos < 0 || pos >= GetLength(plist))
{
return false;
}
int i = 0;
for(Node *p = plist->next;p != NULL;p= p->next)
{
if(i == pos)
{
*rtval = p->data;
return true;
}
i++;
}
return false;
}
//得到结点p的前驱
static Node *GetPri(List plist,Node *p)
{
for(Node *q=plist;q->next !=NULL ;q=q->next)
{
if(q->next == p)
{
return q;
}
}
return NULL;
}
//逆置
void Reverse(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->next = plist->next;
plist->next = p;
p = q;
}
}
//除去重复的数据值
void Unique(List plist)
{
Node *p;
Node *q;
for(p = plist->next;p != NULL;p = p->next)
{
for(q = p;q->next != NULL;q = q->next)
{
if(q->next->data == p->data)
{
Node *s = q->next;
q->next = s->next;
free(s);
}
}
}
}
主函数:
#include<stdio.h>
#include<vld.h>
#include"list.h"
int main()
{
Node head1;
Node head2;
InitList(&head1);
InitList(&head2);
for(int i = 0;i < 15;i++)
{
Insert_head(&head1,i);
Insert_tail(&head2,i);
}
Show(&head1);
Show(&head2);
Reverse(&head1);
Show(&head1);
int val;
GetElem(&head2,3,&val);
printf("%d\n",val);
Destroy(&head1);
Destroy(&head1);
return 0;
}