单向循环链表
LinkList.h
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建失败\n");
}
printf ("创建成功\n");
int i;
for (i = 0; i < 10; i++)
{
//Insert_Head(ls, i);
Insert_Last(ls, i);
}
Display(ls);
Insert_Pos(ls, 1, 123);
Display(ls);
Insert_Pos(ls, 12, 456);
Display(ls);
Insert_Pos(ls, 3, 100);
Display(ls);
Insert_Pos(ls, 30, 100);
Display(ls);
printf ("------------------\n");
Delete_Pos(ls, 1);
Display(ls);
Delete_Pos(ls, 12);
Display(ls);
Delete_Pos(ls, 2);
Display(ls);
Delete_Pos(ls, 11);
Display(ls);
Delete_Data(ls, 6);
Display(ls);
Reverse(ls);
Display(ls);
Destroy(ls);
return 0;
}
LinkList.c
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List *CreateList()
{
List *ls = (List*)malloc(sizeof(List)/sizeof(char));
if (NULL == ls)
return NULL;
// 创建头结点
ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == ls->head)
{
free(ls);
return NULL;
}
ls->head->next = ls->head; // 空链表
return ls;
}
BOOL Insert_Head(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = ls->head->next;
ls->head->next = node;
return TRUE;
}
BOOL Insert_Last(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = ls->head;
Node *tmp = ls->head; // 头结点
while (tmp->next!=ls->head)
{
tmp = tmp->next;
}
tmp->next = node;
return TRUE;
}
BOOL Insert_Pos(List *ls, int pos, Data data)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == ls->head) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = tmp->next;
tmp->next = node;
return TRUE;
}
BOOL Delete_Pos(List *ls, int pos)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == ls->head || tmp->next == ls->head) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
BOOL Delete_Data(List *ls, Data data)
{
if(NULL == ls)
return ERROR;
Node *tmp = ls->head;
while (tmp->next!=ls->head)
{
if (tmp->next->data == data)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
BOOL Reverse(List *ls)
{
// NULL ==ls || NULL == ls->head 链表不存在
// NULL == ls->head->next 空链表
// NULL == ls->head->next->next 只有一个结点的链表
if (NULL == ls || NULL == ls->head || ls->head == ls->head->next || ls->head == ls->head->next->next)
return ERROR;
Node *pre = ls->head->next; // 第一个结点
Node *cur = pre->next; // 第二个结点
Node *tmp; // 保存当前结点的下一个结点
while (cur!=ls->head)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
ls->head->next->next = ls->head;
ls->head->next = pre;
return TRUE;
}
void Display(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head->next; // 第一个结点
while (tmp!=ls->head)
{
printf ("%-4d", tmp->data);
tmp = tmp->next;
}
printf ("\n");
}
void Destroy(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head;
while (tmp->next!=ls->head)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(ls->head);
free(ls);
}
main.c
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建失败\n");
}
printf ("创建成功\n");
int i;
for (i = 0; i < 10; i++)
{
//Insert_Head(ls, i);
Insert_Last(ls, i);
}
Display(ls);
Insert_Pos(ls, 1, 123);
Display(ls);
Insert_Pos(ls, 12, 456);
Display(ls);
Insert_Pos(ls, 3, 100);
Display(ls);
Insert_Pos(ls, 30, 100);
Display(ls);
printf ("------------------\n");
Delete_Pos(ls, 1);
Display(ls);
Delete_Pos(ls, 12);
Display(ls);
Delete_Pos(ls, 2);
Display(ls);
Delete_Pos(ls, 11);
Display(ls);
Delete_Data(ls, 6);
Display(ls);
Reverse(ls);
Display(ls);
Destroy(ls);
return 0;
}
双向链表
LinkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef enum {TRUE, FALSE, ERROR} BOOL;
typedef int Data;
typedef struct _node
{
Data data; // 数据域
struct _node *next; // 指针域,指向链表的下一个结点
struct _node *pre; // 指针域,指向链表的上一个结点
}Node;
typedef struct _list
{
Node *head; // 头结点
}List;
// 创建链表
List *CreateList();
// 插入:头插
BOOL Insert_Head(List *ls, Data data);
// 插入:尾插
BOOL Insert_Last(List *ls, Data data);
// 插入:在特定位置插入的数据
// pos:第 pos 个结点位置
BOOL Insert_Pos(List *ls, int pos, Data data);
// 删除特定位置的数据
// pos:第 pos 个结点位置
BOOL Delete_Pos(List *ls, int pos);
// 根据值删除数据
BOOL Delete_Data(List *ls, Data data);
// 链表逆序
BOOL Reverse(List *ls);
void Destroy(List *ls);
void Display(List *ls);
#endif // _LINKLIST_H_
LinkList.c
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List *CreateList()
{
List *ls = (List*)malloc(sizeof(List)/sizeof(char));
if (NULL == ls)
return NULL;
// 创建头结点
ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == ls->head)
{
free(ls);
return NULL;
}
ls->head->next = NULL; // 空链表
ls->head->pre = NULL;
return ls;
}
BOOL Insert_Head(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = ls->head->next;
node->pre = ls->head;
ls->head->next = node;
if(node->next!=NULL)
{
node->next->pre=node;
}
return TRUE;
}
BOOL Insert_Last(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = NULL;
Node *tmp = ls->head; // 头结点
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
node->pre = tmp;
node->pre = NULL;
return TRUE;
}
BOOL Insert_Pos(List *ls, int pos, Data data)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == NULL) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = tmp->next;
node->pre = tmp;
tmp->next = node;
if(node->next!=NULL)
{
node->next->pre=node;
}
return TRUE;
}
BOOL Delete_Pos(List *ls, int pos)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == NULL || tmp->next == NULL) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *p = tmp->next;
tmp->next = p->next;
if (p->next != NULL)
{
p->next->pre=tmp;
}
free(p);
return TRUE;
}
BOOL Delete_Data(List *ls, Data data)
{
if(NULL == ls)
return ERROR;
Node *tmp = ls->head;
while (tmp->next)
{
if (tmp->next->data == data)
{
Node *p = tmp->next;
tmp->next = p->next;
if (p->next != NULL)
{
p->next->pre=tmp;
}
free(p);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
BOOL Reverse(List *ls)
{
// NULL ==ls || NULL == ls->head 链表不存在
// NULL == ls->head->next 空链表
// NULL == ls->head->next->next 只有一个结点的链表
if (NULL == ls || NULL == ls->head || NULL == ls->head->next || NULL == ls->head->next->next)
return ERROR;
Node *cur =ls->head->next; // 第一个结点
while (cur->next)
{
Node *tmp = cur->next;
cur->next = cur->pre;
cur->pre = tmp;
cur = tmp;
}
ls->head->next->next = NULL;
ls->head->next=cur;
cur->next=cur->pre;
cur->pre=ls->head;
return TRUE;
}
void Display(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head->next; // 第一个结点
while (tmp)
{
printf ("%-4d", tmp->data);
tmp = tmp->next;
}
printf ("\n");
}
void Destroy(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head;
while (tmp->next)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(ls->head);
free(ls);
}
main.c
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建失败\n");
}
printf ("创建成功\n");
int i;
for (i = 0; i < 10; i++)
{
Insert_Head(ls, i);
//Insert_Last(ls, i);
}
Display(ls);
Insert_Pos(ls, 1, 123);
Display(ls);
Insert_Pos(ls, 12, 456);
Display(ls);
Insert_Pos(ls, 3, 100);
Display(ls);
Insert_Pos(ls, 30, 100);
Display(ls);
printf ("------------------\n");
Delete_Pos(ls, 1);
Display(ls);
Delete_Pos(ls, 12);
Display(ls);
Delete_Pos(ls, 2);
Display(ls);
Delete_Pos(ls, 11);
Display(ls);
Delete_Data(ls, 6);
Display(ls);
Reverse(ls);
Display(ls);
return 0;
}
双向链表
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
typedef enum {TRUE, FALSE, ERROR} BOOL;
typedef int Data;
typedef struct _node
{
Data data; // 数据域
struct _node *next; // 指针域,指向链表的下一个结点
struct _node *pre; // 指针域,指向链表的上一个结点
}Node;
typedef struct _list
{
Node *head; // 头结点
}List;
// 创建链表
List *CreateList();
// 插入:头插
BOOL Insert_Head(List *ls, Data data);
// 插入:尾插
BOOL Insert_Last(List *ls, Data data);
// 插入:在特定位置插入的数据
// pos:第 pos 个结点位置
BOOL Insert_Pos(List *ls, int pos, Data data);
// 删除特定位置的数据
// pos:第 pos 个结点位置
BOOL Delete_Pos(List *ls, int pos);
// 根据值删除数据
BOOL Delete_Data(List *ls, Data data);
// 链表逆序
BOOL Reverse(List *ls);
void Destroy(List *ls);
void Display(List *ls);
#endif // _LINKLIST_H_
LinkList.c
#include "LinkList.h"
#include <stdlib.h>
#include <stdio.h>
List *CreateList()
{
List *ls = (List*)malloc(sizeof(List)/sizeof(char));
if (NULL == ls)
return NULL;
// 创建头结点
ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
if(NULL == ls->head)
{
free(ls);
return NULL;
}
ls->head->next = NULL; // 空链表
ls->head->pre = NULL;
return ls;
}
BOOL Insert_Head(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = ls->head->next;
node->pre = ls->head;
ls->head->next = node;
if(node->next!=NULL)
{
node->next->pre=node;
}
return TRUE;
}
BOOL Insert_Last(List *ls, Data data)
{
if (NULL == ls)
return ERROR;
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = NULL;
Node *tmp = ls->head; // 头结点
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
node->pre = tmp;
node->pre = NULL;
return TRUE;
}
BOOL Insert_Pos(List *ls, int pos, Data data)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == NULL) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if (NULL == node)
return ERROR;
node->data = data;
node->next = tmp->next;
node->pre = tmp;
tmp->next = node;
if(node->next!=NULL)
{
node->next->pre=node;
}
return TRUE;
}
BOOL Delete_Pos(List *ls, int pos)
{
if(NULL == ls|| pos<1)
return ERROR;
Node *tmp = ls->head;
int i;
for (i = 0; i<pos-1; i++)
{
tmp = tmp->next;
if (tmp == NULL || tmp->next == NULL) // 越界
{
printf ("长度越界: %d\n", pos);
return ERROR;
}
}
Node *p = tmp->next;
tmp->next = p->next;
if (p->next != NULL)
{
p->next->pre=tmp;
}
free(p);
return TRUE;
}
BOOL Delete_Data(List *ls, Data data)
{
if(NULL == ls)
return ERROR;
Node *tmp = ls->head;
while (tmp->next)
{
if (tmp->next->data == data)
{
Node *p = tmp->next;
tmp->next = p->next;
if (p->next != NULL)
{
p->next->pre=tmp;
}
free(p);
return TRUE;
}
tmp = tmp->next;
}
return FALSE;
}
BOOL Reverse(List *ls)
{
// NULL ==ls || NULL == ls->head 链表不存在
// NULL == ls->head->next 空链表
// NULL == ls->head->next->next 只有一个结点的链表
if (NULL == ls || NULL == ls->head || NULL == ls->head->next || NULL == ls->head->next->next)
return ERROR;
Node *cur =ls->head->next; // 第一个结点
while (cur->next)
{
Node *tmp = cur->next;
cur->next = cur->pre;
cur->pre = tmp;
cur = tmp;
}
ls->head->next->next = NULL;
ls->head->next=cur;
cur->next=cur->pre;
cur->pre=ls->head;
return TRUE;
}
void Display(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head->next; // 第一个结点
while (tmp)
{
printf ("%-4d", tmp->data);
tmp = tmp->next;
}
printf ("\n");
}
void Destroy(List *ls)
{
if (NULL == ls)
return;
Node *tmp = ls->head;
while (tmp->next)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p);
}
free(ls->head);
free(ls);
}
main.c
#include <stdio.h>
#include "LinkList.h"
int main()
{
List *ls = CreateList();
if (NULL == ls)
{
printf ("创建失败\n");
}
printf ("创建成功\n");
int i;
for (i = 0; i < 10; i++)
{
Insert_Head(ls, i);
//Insert_Last(ls, i);
}
Display(ls);
Insert_Pos(ls, 1, 123);
Display(ls);
Insert_Pos(ls, 12, 456);
Display(ls);
Insert_Pos(ls, 3, 100);
Display(ls);
Insert_Pos(ls, 30, 100);
Display(ls);
printf ("------------------\n");
Delete_Pos(ls, 1);
Display(ls);
Delete_Pos(ls, 12);
Display(ls);
Delete_Pos(ls, 2);
Display(ls);
Delete_Pos(ls, 11);
Display(ls);
Delete_Data(ls, 6);
Display(ls);
Reverse(ls);
Display(ls);
return 0;
}