# 数据结构与算法-C语言8-单循环列表

#### 示例程序

##### CList.h
``````//CList.h
//结构体定义以及函数声明

#ifndef CLIST_H
#define CLIST_H

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <iostream>

typedef  int ElemType;

typedef struct Node
{
ElemType data;
struct Node *next;
}Node, *PNode;

typedef struct List
{
PNode tail;
int size;
}List,*PList;

bool InitList(PList list);
void Create_t(PList list,ElemType x);             //尾插
void Create_h(PList list,ElemType x);             //头插
void del_back(PList list);                        //尾删
void del_front(PList list);                       //头删
void sortList(PList list);                        //排序
void insert_val(PList list,ElemType x);           //按值插
PNode find(PList list,ElemType x);
void del_val(PList list,ElemType x);
void modify(PList list,ElemType x1,ElemType x2);
void clear(PList list);
void destroy(PList list);
void reserve(PList list);
int length(PList list);
void showList(PList list);
void show_tail(PList list);
ElemType next(PList list,ElemType x);
ElemType prio(PList list,ElemType x);
PNode prev(PList list,PNode p);

#endif

``````
##### CList.cpp
``````//CList.cpp
//函数实现

#include"CList.h"

{
printf("***************************************************************\n");
printf("* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *\n");
printf("* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*\n");
printf("* [8] find        [9] del_val    [10] sortList   [11] modify  *\n");
printf("* [12]clear       [13]destroy    [14] resver     [15]length   *\n");
printf("* [16] next       [17]prio                                    *\n");
printf("***************************************************************\n");
}
bool InitList(PList list)
{

list->size = 0;                           //长度初始化为0
return true;
}

void Create_t(PList list,ElemType x)     //尾插法
{
PNode s = (PNode)malloc(sizeof(Node));
assert(s != NULL);
s->data = x;                         //填充数据域
list->tail->next = s;                //tail指向最后一个节点，把新建立的节点链接到链表的最后
list->tail = s;                      //改变尾指针的指向
list->size++;
}
void showList(PList list)            //链表显示函数
{
if(1>list->size)
{
printf("--链表为空\n");
return ;
}
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Create_h(PList list,ElemType x)  //头插法
{
PNode s = (PNode)malloc(sizeof(Node));
assert(s != NULL);
s->data = x;                             //填充数据域
if(list->size == 0)                      //如果是第一次头插，需改变尾指针和尾节点的next指向，之后都不改变
{
list->tail = s;
}
list->size++;        //插入一个，长度加1
}

void del_back(PList list)    //尾删
{
if(0==list->size)
return;
{
p = p->next;
}
free(list->tail);                       //释放最后一个节点
list->tail = p;                         //尾指针指向原来的倒数第二个节点（现在倒数第一）
printf("--尾节点已删除\n");
list->size--;
}

void del_front(PList list)   //头删
{
if(0==list->size)
return;
else
{
if(1==list->size)
{                                    //只有一个节点，若删去，需改变尾指针
}
else
{
}
free(p);                             //释放第一个节点
}
printf("--头节点已删除\n");
list->size--;
}

void show_tail(PList list)     //为测试尾指针是否正确改变，需显示最后一个节点
{
printf("--链表的尾节点是：》%d \n",list->tail->data);
}

void sortList(PList list)      // 对无序链表进行从小到大排序
{
if(2>list->size)
return ;
PNode q = p->next;
for(int i = 0;i<list->size-1;i++,p = list->head->next,q = p->next)      //n个数比较n-1趟
{
for(int j = 0;j<list->size-1-i;j++,p=q,q=q->next)   //第i趟比较n-i次
{
if(p->data > q->data)        //如果前面的数大于后面，则交换
{
p->data = p->data + q->data;
q->data = p->data - q->data;
p->data = p->data - q->data;
}
}
}
}

void insert_val(PList list,ElemType x)   //链表有序的前提下，给一个值插入
{
{
if(x<p->data)
break;
q = p;
p = p->next;
}
PNode s = (PNode)malloc(sizeof(Node)); //初始化新节点
s->data = x;
q->next = s;              //把新节点插入到链表中（在p,q之间插入）
s->next = p;
{
list->tail = s;
}
list->size++;
}

PNode find(PList list,ElemType x)       //返回要查找元素的前面一个的地址
{
while(list->tail != p && list->head != p->next && x != p->next->data)
{
p = p->next;                   //循环结束，p指向x的前面一个元素
}
{
printf("--没找到！\n");
return NULL;
}
return p;
}

void del_val(PList list,ElemType x)     //删除指定的值x
{
if(0 == list->size)
return ;
PNode p = find(list,x);
PNode q = NULL;
if(NULL != p)
{
q = p->next;               //q指向要删除的节点
if(q == list->tail)        //若删除最后一个节点，需改变尾指针
{
list->tail = p;
}
else
{
p->next = q->next;
}
free(q);                  //释放要删除的节点
list->size--;
printf("--%d已删除!\n",x);
}
return ;
}

void modify(PList list,ElemType x1,ElemType x2)    //把原有的x1修改成x2
{
PNode p = find(list,x1);
if(NULL != p)
p->next->data = x2;
else
return ;
}

void clear(PList list)       //删除链表的所有节点，但不删除头结点
{
PNode q = p;
{
p = p->next;         //p依次后移，跟屁虫q依次释放节点
free(q);
q = p;
}
list->size = 0;
printf("--链表已被清空！\n");
}

void destroy(PList list)   //摧毁链表，包括所有节点和头结点
{
clear(list);
list->tail = NULL;
printf("--链表已被摧毁！\n");
}

PNode prev(PList list,PNode p)     //返回p指向的前面一个节点
{
{
while(q != list->head && q->next != p)  //依次往后移，知道尾指针的前面一个节点
q=q->next;
if(q->next == p)
return q;
}
return NULL;
}
void reserve(PList list)   //逆置链表
{
PNode s = (PNode)malloc(sizeof(Node));       //建立一个节点
s->next = list->tail;
PNode p = list->tail;
{
list->tail = prev(list,list->tail);
p->next = list->tail;
p=p->next;
}
p->next = s;              //p指向第一个节点，即新链表的最后一个节点，尾指针的next指向头结点s，链表结束
}

int length(PList list)     //求链表的长度
{
return list->size;
}

ElemType next(PList list,ElemType x)      //返回x的后继
{
PNode p = find(list,x);
if(NULL == p)
return -1;
if(p->next == list->tail)           //因为是单循环链表，尾节点的下一个元素是第一个节点
{
}
p=p->next;
return p->next->data;
}
ElemType prio(PList list,ElemType x)         //返回x的前驱
{
PNode p = find(list,x);
if(NULL != p)
{
if(p == list->head || p == list->tail)
{
return list->tail->data;
}
return p->data;
}
return -1;
}

``````
##### main.cpp
``````//main.cpp
//测试函数
#include "CList.h"

int main()
{
List mylist;
InitList(&mylist);
ElemType item = 0;
int pos = 0;
int chose = 1;
PNode p = NULL;
while(chose)
{
printf("给出想要操作的序号：\n");
scanf("%d",&chose);
switch(chose)
{
case 0:
destroy(&mylist);
chose = 0;
break;
case 1:
printf("输入要尾插的数据[-1结束]：\n");
while(scanf("%d",&item),item!=-1)
{
Create_t(&mylist,item);
}
break;
case 2:
printf("输入要头插的数据：\n");
while(scanf("%d",&item),item!=-1)
{
Create_h(&mylist,item);
}
break;
case 3:
showList(&mylist);
break;
case 4:
del_back(&mylist);
break;
case 5:
del_front(&mylist);
break;
case 6:
printf("给出要插入的数：\n");
scanf("%d",&item);
insert_val(&mylist,item);
break;
case 7:
show_tail(&mylist);
break;
case 8:
printf("输入要查找的数：\n");
scanf("%d",&item);
p = find(&mylist,item);
if(NULL!=p)
printf("%d\n",p->next->data);
break;
case 9:
printf("输入要删除的数：\n");
scanf("%d",&item);
del_val(&mylist,item);
break;
case 10:
sortList(&mylist);
break;
case 11:
printf("输入要修改的数和修改后的数\n");
scanf("%d %d",&item,&pos);
modify(&mylist,item,pos);
break;
case 12:
clear(&mylist);
break;
case 13:
destroy(&mylist);
break;
case 14:
reserve(&mylist);
break;
case 15:
printf("链表长度为：%d\n",length(&mylist));
break;
case 16:
printf("输入想要找哪个一数的后继：\n");
scanf("%d",&item);
printf("%d 的后继是：%d\n",item,next(&mylist,item));
break;
case 17:
printf("输入想要找哪个一数的前驱：\n");
scanf("%d",&item);
printf("%d 的前驱是：%d\n",item,prio(&mylist,item));
break;
default:
printf("重新输入\n");
break;
}
}
return 0;
}

``````

``````***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

1

5 6  9 2 8 2 6 -1
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

3
5 6 9 2 8 2 6
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

8

2
2
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

7
--链表的尾节点是：》6
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

10
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

3
2 2 5 6 6 8 9
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

14
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

3
9 8 6 6 5 2 2
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

16

8
8 的后继是：6
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

12
--链表已被清空！
***************************************************************
* [0] quit_system [1] Create_t   [2] Create_h    [3] showList *
* [4] del_back    [5] del_front  [6] insert_val  [7] show_tail*
* [8] find        [9] del_val    [10] sortList   [11] modify  *
* [12]clear       [13]destroy    [14] resver     [15]length   *
* [16] next       [17]prio                                    *
***************************************************************

3
--链表为空
``````