数据结构与算法 - 线性表
第1关 实现一个顺序存储的线性表
/*************************************************************
date: April 2017
copyright: Zhu En
DO NOT distribute this code without my permission.
**************************************************************/
// 顺序表操作实现文件
//////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "Seqlist.h"
SeqList* SL_Create(int maxlen)
// 创建一个顺序表。
// 与SqLst_Free()配对。
{
SeqList* slist=(SeqList*)malloc(sizeof(SeqList));
slist->data = (T*)malloc(sizeof(T)*maxlen);
slist->max=maxlen;
slist->len=0;
return slist;
}
void SL_Free(SeqList* slist)
// 释放/删除 顺序表。
// 与SqLst_Create()配对。
{
free(slist->data);
free(slist);
}
void SL_MakeEmpty(SeqList* slist)
// 置为空表。
{
slist->len=0;
}
int SL_Length(SeqList* slist)
// 获取长度。
{
return slist->len;
}
bool SL_IsEmpty(SeqList* slist)
// 判断顺序表是否空。
{
return 0==slist->len;
}
bool SL_IsFull(SeqList* slist)
// 判断顺序表是否满。
{
return slist->len==slist->max;
}
T SL_GetAt(SeqList* slist, int i)
// 获取顺序表slist的第i号结点数据。
// 返回第i号结点的值。
{
if(i<0||i>=slist->len) {
printf("SL_GetAt(): location error when reading elements of the slist!\n");
SL_Free(slist);
exit(0);
}
else
return slist->data[i];
}
void SL_SetAt(SeqList* slist, int i, T x)
// 设置第i号结点的值(对第i号结点的数据进行写)。
{
if(i<0||i>=slist->len) {
printf("SL_SetAt(): location error when setting elements of the slist!\n");
SL_Free(slist);
exit(0);
}
else
slist->data[i]=x;
}
bool SL_InsAt(SeqList* slist, int i, T x)
// 在顺序表的位置i插入结点x, 插入d[i]之前。
// i 的有效范围[0,plist->len]。
{
// 请在下面的Begin-End之间补充代码,插入结点。
/********** Begin *********/
if (i<0 || i>slist->len || slist->len==slist->max) {
printf("SL_InsAt(): location error, or slist full.\n");
return false;
}
for (int j=slist->len; j>=i+1; j--) {
slist->data[j]=slist->data[j-1];
}
slist->data[i]=x;
slist->len++;
return true;
/********** End **********/
}
T SL_DelAt(SeqList* slist, int i)
// 删除顺序表plist的第i号结点。
// i的有效范围应在[0,plist->len)内,否则会产生异常或错误。
// 返回被删除的数据元素的值。
{
// 在下面的Begin-End之间补充代码,删除第i号结点。
/********** Begin *********/
if (i<0 || i>=slist->len) {
printf("SL_DelAt(): location error!\n");
SL_Free(slist);
exit(0);
}
T res=slist->data[i];
for (int j=i; j<slist->len-1; j++) {
slist->data[j] = slist->data[j+1];
}
slist->len--;
return res;
/********** End **********/
}
int SL_FindValue(SeqList* slist, T x)
// 在顺序表表中查找第一个值为x的结点,返回结点的编号。
// 返回值大于等于0时表示找到值为x的结点的编号,-1表示没有找到。
{
int i=0;
while(i<slist->len && slist->data[i]!=x) i++;
if (i<slist->len) return i;
else return -1;
}
int SL_DelValue(SeqList* slist, T x)
// 删除第一个值为x的结点。
// 存在值为x的结点则返回结点编号, 未找到返回-1。
{
// 在下面的Begin-End之间补充代码,删除第一个值为 x 的结点。
/********** Begin *********/
int i=SL_FindValue(slist, x);
if (i>=0) SL_DelAt(slist, i);
return i;
/********** End **********/
}
void SL_Print(SeqList* slist)
// 打印整个顺序表。
{
if (slist->len==0) {
printf("The slist is empty.\n");
return;
}
//printf("The slist contains: ");
for (int i=0; i<slist->len; i++) {
printf("%d ", slist->data[i]);
}
printf("\n");
}
第2关 实现一个链接存储的线性表
/*************************************************************
date: April 2017
copyright: Zhu En
DO NOT distribute this code without my permission.
**************************************************************/
// 单链表实现文件
#include <stdio.h>
#include <stdlib.h>
#include "LinkList.h"
// 1)
LinkList* LL_Create()
// 创建一个链接存储的线性表,初始为空表,返回llist指针。
{
LinkList* llist=(LinkList*)malloc(sizeof(LinkList));
llist->front=NULL;
llist->rear=NULL;
llist->pre=NULL;
llist->curr=NULL;
llist->position=0;
llist->len=0;
return llist;
}
// 2)
void LL_Free(LinkList* llist)
// 释放链表的结点,然后释放llist所指向的结构。
{
LinkNode* node=llist->front;
LinkNode* nextnode;
while(node){
nextnode=node->next;
free(node);
node=nextnode;
}
free(llist);
}
// 3)
void LL_MakeEmpty(LinkList* llist)
// 将当前线性表变为一个空表,因此需要释放所有结点。
{
LinkNode* node=llist->front;
LinkNode* nextnode;
while(node){
nextnode=node->next;
free(node);
node=nextnode;
}
llist->front=NULL;
llist->rear=NULL;
llist->pre=NULL;
llist->curr=NULL;
llist->position=0;
llist->len=0;
}
// 4)
int LL_Length(LinkList* llist)
// 返回线性表的当前长度。
{
return llist->len;
}
// 5)
bool LL_IsEmpty(LinkList* llist)
// 若当前线性表是空表,则返回true,否则返回TRUE。
{
return llist->len==0;
}
// 6)
bool LL_SetPosition(LinkList* llist, int i)
// 设置线性表的当前位置为i号位置。
// 设置成功,则返回true,否则返回false(线性表为空,或i不在有效的返回)。
// 假设线性表当前长度为len,那么i的有效范围为[0,len]。
{
int k;
/* 若链表为空,则返回*/
if (llist->len==0) return false;
/*若位置越界*/
if( i < 0 || i > llist->len)
{ printf("LL_SetPosition(): position error");
return false;
}
/* 寻找对应结点*/
llist->curr = llist->front;
llist->pre = NULL;
llist->position = 0;
for ( k = 0; k < i; k++) {
llist->position++;
llist->pre = llist->curr;
llist->curr = (llist->curr)->next;
}
/* 返回当前结点位置*/
return true;
}
// 7)
int LL_GetPosition(LinkList* llist)
// 获取线性表的当前位置结点的编号。
{
return llist->position;
}
// 8)
bool LL_NextPosition(LinkList* llist)
// 设置线性表的当前位置的下一个位置为当前位置。
// 设置成功,则返回true,否则返回false(线性表为空,或当前位置为表尾)。
{
if (llist->position >= 0 && llist->position < llist->len)
/* 若当前结点存在,则将其后继结点设置为当前结点*/
{
llist->position++;
llist->pre = llist->curr;
llist->curr = llist->curr->next;
return true;
}
else
return false;
}
// 9)
T LL_GetAt(LinkList* llist)
// 返回线性表的当前位置的数据元素的值。
{
if(llist->curr==NULL)
{
printf("LL_GetAt(): Empty list, or End of the List.\n");
LL_Free(llist);
exit(1);
}
return llist->curr->data;
}
// 10)
void LL_SetAt(LinkList* llist, T x)
// 将线性表的当前位置的数据元素的值修改为x。
{
if(llist->curr==NULL)
{
printf("LL_SetAt(): Empty list, or End of the List.\n");
LL_Free(llist);
exit(1);
}
llist->curr->data=x;
}
// 11)
bool LL_InsAt(LinkList* llist, T x)
// 在线性表的当前位置之前插入数据元素x。当前位置指针指向新数据元素结点。
// 若插入失败,返回false,否则返回true。
{
LinkNode *newNode=(LinkNode*)malloc(sizeof(LinkNode));
if (newNode==NULL) return false;
newNode->data=x;
if (llist->len==0){
/* 在空表中插入*/
newNode->next=NULL;
llist->front = llist->rear = newNode;
}
//当前位置为表头。
else if (llist->pre==NULL)
{
/* 在表头结点处插入*/
newNode->next = llist->front;
llist->front = newNode;
}
else {
/* 在链表的中间位置或表尾后的位置插入*/
newNode->next = llist->curr;
llist->pre->next=newNode;
}
//插入在表尾后。
if (llist->pre==llist->rear)
llist->rear=newNode;
/* 增加链表的大小*/
llist->len++;
/* 新插入的结点为当前结点*/
llist->curr = newNode;
return true;
}
// 12)
bool LL_InsAfter(LinkList* llist, T x)
// 在线性表的当前位置之后插入数据元素x。空表允许插入。当前位置指针将指向新结点。
// 若插入失败,返回false,否则返回true。
{
// 请在Begin-End之间补充代码,实现结点插入。
/********** Begin *********/
LinkNode *newNode=(LinkNode*)malloc(sizeof(LinkNode));
if (newNode==NULL) return false;
newNode->data=x;
if (llist->len==0) {
/* 在空表中插入*/
newNode->next=NULL;
llist->front = llist->rear = newNode;
}
else if (llist->curr == llist->rear || llist->curr == NULL) {
/* 在尾结点后插入*/
newNode->next = NULL;
llist->rear->next=newNode;
llist->pre=llist->rear;
llist->rear=newNode;
llist->position=llist->len;
}
else{
/* 在中间位置插入*/
newNode->next = llist->curr->next;
llist->curr->next=newNode;
llist->pre=llist->curr;
llist->position ++;
}
/* 增加链表的大小*/
llist->len ++;
/* 新插入的结点为当前结点*/
llist->curr = newNode;
return true;
/********** End **********/
}
// 13)
bool LL_DelAt(LinkList* llist)
// 删除线性表的当前位置的数据元素结点。
// 若删除失败(为空表,或当前位置为尾结点之后),则返回false,否则返回true。
{
LinkNode *oldNode;
/* 若表为空或已到表尾之后,则给出错误提示并返回*/
if (llist->curr==NULL)
{
printf("LL_DelAt(): delete a node that does not exist.\n");
return false;
}
oldNode=llist->curr;
/* 删除的是表头结点*/
if (llist->pre==NULL)
{
llist->front = oldNode->next;
}
/* 删除的是表中或表尾结点*/
else if(llist->curr!=NULL){
llist->pre->next = oldNode->next;
}
if (oldNode == llist->rear) {
/* 删除的是表尾结点,则修改表尾指针和当前结点位置值*/
llist->rear = llist->pre;
}
/* 后继结点作为新的当前结点*/
llist->curr = oldNode->next;
/* 释放原当前结点*/
free(oldNode);
/* 链表大小减*/
llist->len --;
return true;
}
// 14)
bool LL_DelAfter(LinkList* llist)
// 删除线性表的当前位置的后面那个数据元素。
// 若删除失败(为空表,或当前位置时表尾),则返回false,否则返回true。
{
LinkNode *oldNode;
/* 若表为空或已到表尾,则给出错误提示并返回*/
if (llist->curr==NULL || llist->curr== llist->rear)
{
printf("LL_DelAfter(): delete a node that does not exist.\n");
return false;
}
/* 保存被删除结点的指针并从链表中删除该结点*/
oldNode = llist->curr->next;
llist->curr->next=oldNode->next;
if (oldNode == llist->rear)
/* 删除的是表尾结点*/
llist->rear = llist->curr;
/* 释放被删除结点*/
free(oldNode);
/* 链表大小减*/
llist->len --;
return true;
}
// 15)
int LL_FindValue(LinkList* llist, T x)
// 找到线性表中第一个值为x的数据元素的编号。
// 返回值-1表示没有找到,返回值>=0表示编号。
{
LinkNode* p=llist->front;
int idx=0;
while(p!=NULL && p->data!=x) {
idx++;
p = p->next;
}
if (idx>=llist->len) return -1;
else return idx;
}
// 16)
int LL_DelValue(LinkList* llist, T x)
// 删除第一个值为x的数据元素,返回该数据元素的编号。如果不存在值为x的数据元素,则返回-1。
{
int idx=LL_FindValue(llist, x);
if (idx<0) return -1;
LL_SetPosition(llist, idx);
LL_DelAt(llist);
return idx;
}
// 17)
void LL_Print(LinkList* llist)
// 打印整个线性表。
{
LinkNode* node=llist->front;
while (node) {
printf("%d ", node->data);
node=node->next;
}
printf("\n");
}
单链表实验
第1关 倒置链表
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
link l; // 定义指针型变量
void Print(link l) // 算法Print,依次访问每个元素结点
{
link P; // 定义指针型变量
P = l->next;//Blank 1
while (P!=NULL)
{
printf(P == l->next ? "%d" : " %d", P->data);//Blank 2
P = P->next;
}
puts("");
}
void Reverse(link &L) // 算法Reverse,实现链表元素结点的倒置
{
link h, u, tmp; // 定义所要用到的指针变量
h = NULL; u = L->next;
while (u != NULL)//Blank 3
{
tmp = u->next;
u->next = h;
h = u;
u = tmp;// Blank 4
}
L->next = h; // Blank 5
}
int linklength(link l) // 算法length
{
link p;
int i = 0;
p = l->next; // Blank 6
while (p!=NULL)
{
i++;
p = p->next; // Blank 7
}
return i;//Blak 8
}
void solve() // 主程序部分
{
create_hsllist(l); // 调用函数库中的函数构建链表l
disp_check_hsllist("Created Sllist",l); // 显示并检查链表l
printf("Length=%d\n",linklength(l)); // 调用算法求链表l的长度
Print(l); // 调用算法Print 对l运算
Reverse(l); // 调用算法Reverse倒置l表的头结点之后的部分
disp_check_hsllist("Reversed Sllist",l); // 显示在调用算法Reverse倒置后的结果链表l
}
}
第2关 求链表内节点的指针
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
link solve(link & L, int i)
{
link p=L;
for(int j=0;j<i;++j){
if(p==NULL)break;
p=p->next;
}
return p;
}
}
第3关 在链表中插入节点
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
void solve(link & L, int i, int x)
{
if(i<=0)return;
link p=L;
for(int j=1;j<i;++j){
p=p->next;
if(p==NULL)break;
}
if(p==NULL||p->next==NULL)return;
link tmp=(link)malloc(sizeof(node));
tmp->data=x;
tmp->next=p->next;
p->next=tmp;
}
}
第4关 在链表中删除节点
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
void solve(link & L, int i)
{
if (i <= 0) return ;
link p = L;
for (int j = 1; j < i; ++j) {
p = p->next;
if (p == NULL) break;
}
if (p == NULL || p->next == NULL) return ;
link tmp = p->next;
p->next = tmp->next;
free(tmp);
}
}
第5关 在有序链表中插入节点
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
void solve(link & L, int x)
{
link p;
for (p = L; ; p = p->next) {
if (p->next == NULL || p->next->data > x) break;
}
link tmp = (link)malloc(sizeof(node));
tmp->data = x;
tmp->next = p->next;
p->next = tmp;
}
}
第6关 链表的奇偶节点分离
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
link solve(link & L)
{
link odd = (link)malloc(sizeof(node));
odd->next = NULL;
link odd_now = odd;
for (link p = L->next; p != NULL && p->next != NULL; p = p->next) {
odd_now->next = p->next;
odd_now = p->next;
p->next = p->next->next;
odd_now->next = NULL;
}
link tail = L;
while (tail->next != NULL) tail = tail->next;
tail->next = odd->next;
return L;
}
}
第7关 求有序链表的交集
#include "linklist.h" // 引用库函数文件
namespace exa { //请在命名空间内编写代码,否则后果自负
link solve(link & L1, link & L2)
{
link L = (link)malloc(sizeof(node));
L->next = NULL;
link now = L, now1 = L1->next, now2 = L2->next;
while (now1 != NULL && now2 != NULL) {
if (now1->data == now2->data) {
link tmp = (link)malloc(sizeof(node));
tmp->data = now1->data; tmp->next = NULL;
now->next = tmp; now = tmp;
now1 = now1->next; now2 = now2->next;
}
else if (now1->data > now2->data) {
now2 = now2->next;
}
else now1 = now1->next;
}
return L;
}
}
数据结构-队列的应用
第一关 循环队列
//
// queue_.cpp
// Queue
//
// Created by ljpc on 2018/5/29.
// Copyright © 2018年 ljpc. All rights reserved.
//
#include "queue_.h"
void creatQueue(Queue* que, int maxSize)
// 创建一个循环队列指针que,队列最大长度为maxSize
{
que->maxSize = maxSize;
que->data = (int*)malloc(maxSize * sizeof(int));
que->front = que->rear = 0;
}
void destroyQueue(Queue* que)
// 释放队列内存空间
{
free(que->data);
}
bool isFull(Queue* que)
// 判断队列que是否为满
// 若满返回 true 并在一行打印 The queue is Full 末尾换行!!!
// 否则返回 false
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if((que->rear+1) % que->maxSize == que->front)
{
printf("The queue is Full\n");
return true;
}else{
return false;
}
/********** End **********/
}
bool isEmpty(Queue* que)
// 判断队列que是否为空
// 若空返回 true 并在一行打印 The queue is Empty 末尾换行!!!
// 否则返回 false
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if(que->front == que->rear)
{
printf("The queue is Empty\n");
return true;
}else{
return false;
}
/********** End **********/
}
int enQueue(Queue* que, int item)
// 实现入队操作:将元素item加入队列que尾部
// 若队列没满,编写加入操作,返回 1
// 若队列满了,不做任何操作,返回 -1
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if(isFull(que)==true){
return -1;
}else{
que->data[que->rear] = item;
que->rear = (que->rear + 1) % que->maxSize;
return 1;
}
/********** End **********/
}
int deQueue(Queue* que)
// 实现出队操作:移除队列que首部元素,并返回元素值
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if(isEmpty(que)==true){
return -1;
}else{
int item = que->data[que->front];
que->front = (que->front + 1) % que->maxSize;
return item;
}
/********** End **********/
}
void printQueue(Queue* que)
// 打印队列
{
while (isEmpty(que)==false) {
int item = deQueue(que);
printf("%d ", item);
}
}
第2关 链队列
//
// queue_.cpp
// LinkQueue
//
// Created by ljpc on 2018/5/30.
// Copyright © 2018年 ljpc. All rights reserved.
//
#include "queue_.h"
void creatLinkQueue(LinkQueue* que)
// 创建一个循环队列指针que
{
que->front = (Node*)malloc(sizeof(Node));
que->rear = que->front;
que->rear->next = NULL;
}
bool isEmpty(LinkQueue* que)
// 判断队列que是否为空
// 若空返回 true 并在一行打印 The queue is Empty 末尾换行!!!
// 否则返回 false
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if( que->front == que->rear){
printf("The queue is Empty\n");
return true;
}
else
return false;
/********** End **********/
}
void enQueue(LinkQueue* que, int item)
// 实现入队操作:将元素item加入队列que尾部
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
Node *node = (Node*)malloc(sizeof(Node));
node->data = item;
node->next = NULL;
que->rear->next = node;
que->rear = node;
/********** End **********/
}
int deQueue(LinkQueue* que)
// 实现出队操作:移除队列que首部元素,并返回元素值
{
// 请在这里补充代码,完成本关任务
/********** Begin *********/
if( isEmpty(que) == true)
{
return -1;
}
Node *node = que->front->next;
int item = node->data;
que->front->next = node->next;
delete node;
if( que->front->next == NULL ){
que->rear = que->front;
}
return item;
/********** End **********/
}
void printQueue(LinkQueue* que)
// 打印队列
{
while (isEmpty(que)==false) {
int item = deQueue(que);
printf("%d ", item);
}
}