单链表的实现及其基本操作
Main.cpp文件:
#include"List.h"
void main()
{
List mylist;
InitList(&mylist);
ElemType Item;
int select = 1;
while (select)
{
printf("*************************************************\n");
printf(" [1] push_back [2] push_front *\n");
printf(" [3] show_list [4] pop_back *\n");
printf(" [5] pop_front [6] insert_val *\n");
printf(" [7] find [8] length *\n");
printf(" [9] delete_val [10] sort *\n");
printf(" [11] reserve [12] clear *\n");
printf(" [13*] destory [0] quit_system *\n");
printf("*************************************************\n");
printf("请选择:");
scanf("%d", &select);
if (select == 0)
break;
switch (select)
{
case 1:
printf("请输入要插入的数据(-1结束)");
while (scanf("%d", &Item), Item != -1)
{
push_back(&mylist, Item);
}
break;
case 2:
printf("请输入要插入的数据(-1结束)");
while (scanf("%d", &Item), Item != -1)
{
push_front(&mylist, Item);
}
break;
case 3:
show_list(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
printf("请输入要插入的数值"); //升序的顺序进行插入
scanf("%d", &Item);
insert_val(&mylist, Item);
break;
case 7:
printf("请输入要查找的数据");
scanf("%d", &Item);
PNode p;
p=find(&mylist,Item);
if (p == NULL)
{
printf("查找的数据在链表中不存在\n");
}
break;
case 8:
printf("链表的长度为:%d\n", length(&mylist));
break;
case 9:
printf("请输入要删除的数据:");
scanf("%d", &Item);
delete_val(&mylist, Item);
break;
case 10:
sort(&mylist);
break;
case 11:
reserve(&mylist);
break;
case 12:
clear(&mylist);
break;
/*case 13:
destory(&mylist);
break;*/
default:
printf("输入的命令错误,请重新输入!");
break;
}
}
destory(&mylist);
}
List.h文件:
#ifndef _LIST_H_
#define _LIST_H_
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node* next;
}Node,*PNode;
typedef struct List
{
PNode first;
PNode last;
size_t size;
}List;
void InitList(List *list);
void push_back(List *list, ElemType x);
void push_front(List *list,ElemType x);
void show_list(List *list);
void pop_back(List *list);
void pop_front(List *list);
void insert_val(List *list, ElemType val);
PNode find(List *list, ElemType x);
int length(List *list);
void delete_val(List *list, ElemType x);
void sort(List *list);
void reserve(List *list);
void clear(List *list);
void destory(List *list);
#endif
List.cpp文件:
#include"List.h"
void InitList(List *list)
{
list->first = list->last = (Node *)malloc(sizeof(Node));
assert(list->first);
list->first->next = NULL;
list->size = 0;
}
void push_back(List *list, ElemType x)
{
PNode p = (Node *)malloc(sizeof(Node));
assert(p != NULL);
p->data = x;
p->next = NULL;
list->last->next = p;
list->last = p;
list->size++;
}
void push_front(List *list,ElemType x)
{
PNode p = (PNode)malloc(sizeof(Node));
assert(p != NULL);
p->data = x;
p->next = NULL;
p->next=list->first->next;
list->first->next = p;
if (list->size == 0) //当插入的节点是第一个的时候要更改尾节点的指向
{
list->last = p;
}
list->size++;
}
void show_list(List *list)
{
PNode p = list->first->next; //指向第一个有效节点
while (p != NULL)
{
printf("%d-->", p->data);
p = p->next;
}
printf("null\n");
}
void pop_back(List *list)
{
if (list->size == 0)
return;
PNode p = list->first;
while (p->next != list->last)
{
p = p->next;
}
free(list->last);
list->last = p;
list->last->next = NULL;
list->size--;
}
void pop_front(List *list)
{
if (list->size == 0)
return;
PNode temp=list->first->next;
list->first->next = temp->next;
free(temp);
if (list->size == 1)
{
list->last = list->first;
}
list->size--;
}
void insert_val(List *list, ElemType val)
{
//创建要插入的数据
PNode p = (PNode)malloc(sizeof(Node));
assert(p != NULL);
p->data = val;
p->next = NULL;
//寻找合适的位置 //设数据已经是从小到大排列
PNode temp = list->first;
while (temp->next!=NULL&&temp->next->data <val)
temp = temp->next;
//插入
if (temp->next==NULL) //找到最后一个节点
{
list->last = p;
}
p->next = temp->next;
temp->next = p;
list->size++;
}
PNode find(List *list, ElemType x)
{
PNode temp = list->first->next;
while (temp != NULL&&temp->data != x)
temp = temp->next;
return temp;
}
int length(List *list)
{
return list->size;
}
void delete_val(List *list, ElemType x)
{
if (list->size == 0)
return;
PNode temp =find(list,x);
if (temp == NULL)
{
printf("要删除的数据不存在!\n");
return;
}
if (temp == list->last)
{
pop_back(list);
}
else
{
PNode q = temp->next;
temp->data=q->data;
temp->next = q->next;
free(q);
list->size--;
}
}
void sort(List *list) //真实交换节点
{
if (list->size == 0 || list->size == 1)
return;
PNode s = list->first->next;
PNode q = s->next;
list->last = s;
list->last->next = NULL;
while (q != NULL)
{
s = q;
q = q->next;
//按值插入的一段代码
PNode temp = list->first;
while (temp->next != NULL&&temp->next->data <s->data)
temp = temp->next;
//插入
if (temp->next == NULL) //找到最后一个节点
{
list->last = s;
}
s->next = temp->next;
temp->next = s;
list->size++;
}
}
void reserve(List *list) //分解链表并进行头插
{
if (list->size == 0 || list->size == 1)
return;
PNode s = list->first->next;
PNode q = s->next;
list->last = s;
list->last->next= NULL;
while (q != NULL)
{
s = q;
q = q->next;
s->next = list->first->next;
list->first->next = s;
}
}
void clear(List *list) //释放节点,但保存头结点
{
if (list->size == 0)
return;
PNode p = list->first->next;
while (p != NULL)
{
list->first->next = p->next;
free(p);
p = list->first->next;
}
list->last = list->first;
list->size = 0;
}
void destory(List *list)
{
clear(list);
free(list->first);
list->first = list->last = NULL;
}