# 《算法导论》 — Chapter 10 基本数据结构

## 序

GitHub 第十章 程序实现代码

## 栈

#ifndef _CSTACK_H
#define _CSTACK_H

#include <iostream>

class CStack{

public:
CStack();
~CStack();

int getTop()
{
return data[top];
}

bool empty()
{
}

bool full()
{
}
void push(int x);

void pop();

void display();

private:
int *data;
int top;
const int maxSize = 100;
};
#endif

#include "CStack.h"
#include <iostream>

CStack::CStack()
{
top = -1;
data = new int[maxSize];
}

CStack::~CStack()
{
delete data;
}

void CStack::push(int x)
{
if (full())
{
std::cout << "栈已满！" << std::endl;
return;
}
else
top = top + 1;
data[top] = x;
}

void CStack::pop()
{
if (empty())
{
std::cout << "栈空！" << std::endl;
return;
}
else
std::cout << data[top--] << std::endl;
}

void CStack::display()
{
if (empty())
{
std::cout << "栈空！" << std::endl;
return;
}

for (int i = 0; i <= top; i++)
std::cout << data[i] << "\t";
std::cout << std::endl;
}

#include "CStack.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
CStack cs;

//显示栈中元素
cout << "当前栈中元素有：" << endl;
cs.display();

//添加元素入栈
for (int i = 0; i < 10; i++)
cs.push(i*i);

//显示栈中元素
cout << "当前栈中元素有：" << endl;
cs.display();

//显示栈顶元素
cout << "栈顶元素为：" << endl;
cout << cs.getTop() << endl;

//栈顶元素出栈
cout << "栈顶元素出栈："<< endl;
cs.pop();

//重新打印栈元素
cout << "当前栈中元素有：" << endl;
cs.display();

system("pause");
return 0;

}

## 队列

#ifndef _CQUEUE_H_
#define _CQUEUE_H_

#include <iostream>
class CQueue{
public:

CQueue();
~CQueue();

bool empty()
{
}

bool full()
{
return ((head == tail + 1) || (head == 0 && tail == maxSize-1));
}

void enQueue(int x);
void deQueue();
void display();

private:
int *data;
int tail;
//队列最大容量，最多存储maxSize-1个元素
const int maxSize = 10;
};
#endif

#include "CQueue.h"
#include <iostream>

using namespace std;

CQueue::CQueue()
{
tail = 0;
data = new int[maxSize];
}

CQueue::~CQueue()
{
delete data;
}

void CQueue::enQueue(int x)
{
if (full())
{
cout << "队列已满" << endl;
return;
}

data[tail] = x;
if (tail == maxSize - 1)
tail = 0;
else tail += 1;
}

void CQueue::deQueue()
{
if (empty())
{
cout << "队列空！" << endl;
return;
}

if (head == maxSize - 1)
else
}

void CQueue::display()
{
if (empty())
{
cout << "队列空！" << endl;
return;
}

{
for (int i = head; i < maxSize-1; i++)
cout << data[i] << "\t";
for (int j = 0; j < tail; j++)
cout << data[j] << "\t";
}
else{
for (int i = head; i < tail; i++)
cout << data[i] << "\t";
}
cout << endl;
}

#include "CQueue.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
CQueue cq;

cout << "此时队的状态是：" << endl;
cq.display();

for (int i = 0; i < 9; i++)
cq.enQueue(i + 1);

//显示队列中所有元素
cout << "显示队中所有元素" << endl;
cq.display();

//再次入队一个元素
cout << "元素11入队！";
cq.enQueue(11);

//队头依次出队
cout << "队头元素：" << endl;
cq.deQueue();
cout << "队头元素：" << endl;
cq.deQueue();
cout << "队头元素：" << endl;
cq.deQueue();

//此时入队新元素
cout << "元素11入队！"<<endl;
cq.enQueue(11);
cout << "元素12入队！"<<endl;
cq.enQueue(12);

//再次显示队列中所有元素
cout << "显示队中所有元素" << endl;
cq.display();

system("pause");
return 0;
}

## 双向链表

#ifndef _CLIST_H_
#define _CLIST_H_

#include <iostream>

//双向链表结构体
typedef struct Node{
Node *prev;
Node *next;
int key;

Node(int x) :prev(NULL), next(NULL), key(x){}

};

class CList{
public:
CList();
~CList();

void Insert(int x);
Node *Search(int value);
void Delete(int x);
void Display();

private:
};
#endif


#include "CList.h"
#include <iostream>
#include <cstdlib>

using namespace std;

CList::CList()
{
}

CList::~CList()
{
}

void CList::Insert(int x)
{
//新建一个结点，其值为x
Node *node = new Node(x);

{
}
}

Node * CList::Search(int value)
{
while (node != NULL && node->key != value)
{
node = node->next;
}
//如果找到相应结点
if (node)
return node;
else
return NULL;
}

//删除值为x的结点
void CList::Delete(int x)
{
{
std::cout << "链表为空！" << endl;
return;
}
Node *node = Search(x);

if (node)
{
if (node->prev != NULL)
node->prev->next = node->next;
else

if (node->next != NULL)
node->next->prev = node->prev;

delete node;

return;
}
else{
std::cout << "链表中无值为"<<x<<"的元素！" << endl;
return;
}
}

void CList::Display()
{
{
std::cout << "链表为空!" << std::endl;
return;
}
while (node)
{
cout << node->key << "\t";
node = node->next;
}
cout << endl;
}

#include "CList.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
CList list;

//打印链表
list.Display();

//向链表插入元素
for (int i = 0; i < 10; i++)
list.Insert(i*i);

cout << "打印链表：" << endl;
list.Display();

//查找元素为9的结点
Node *node = list.Search(9);
cout << "查找元素为9的结点" << node->key << endl;

//删除元素为10的结点
list.Delete(10);

cout << "打印链表：" << endl;
list.Display();

//删除元素为36的结点
list.Delete(36);
cout << "打印链表：" << endl;
list.Display();

system("pause");
return 0;
}

## 带哨兵的循环双向链表

#ifndef _SLIST_H_
#define _SLIST_H_

#include <iostream>

/**
*  带哨兵的环形双向链表结构体
*/

typedef struct SNode{
SNode *prev;
SNode *next;
int key;

SNode(int x) :prev(NULL), next(NULL), key(x){}
};

class SList{

public:
SList();
~SList();

void Insert(int x);
SNode *Search(int value);
void Delete(int x);
void Display();
private:
SNode *nil;
};
#endif

#include "SList.h"
#include <iostream>

using namespace std;

SList::SList()
{
nil = new SNode(0);
nil->prev = nil;
nil->next = nil;
}

SList::~SList()
{
delete nil;
}

void SList::Insert(int x)
{
//新建一个结点，其值为x
SNode *node = new SNode(x);
node->next = nil->next;

nil->next->prev = node;
nil->next = node;
node->prev = nil;
}

SNode * SList::Search(int value)
{
SNode *node = nil->next;
while (node != nil && node->key != value)
{
node = node->next;
}
//如果找到相应结点
if (node != nil)
return node;
else
return NULL;
}

//删除值为x的结点
void SList::Delete(int x)
{
if (nil->next == nil)
{
std::cout << "链表为空！" << endl;
return;
}
SNode *node = Search(x);

if (node)
{
node->prev->next = node->next;
node->next->prev = node->prev;

delete node;

return;
}
else{
std::cout << "链表中无值为" << x << "的元素！" << endl;
return;
}
}

void SList::Display()
{
if (nil == nil)
{
std::cout << "链表为空!" << std::endl;
return;
}
SNode *node = nil->next;
while (node->next != nil)
{
cout << node->key << "\t";
node = node->next;
}
cout << endl;
}

#include "SList.h"
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
SList list;

//打印链表
list.Display();

//向链表插入元素
for (int i = 0; i < 10; i++)
list.Insert(i*i);

cout << "打印链表：" << endl;
list.Display();

//查找元素为9的结点
SNode *node = list.Search(9);
cout << "查找元素为9的结点" << node->key << endl;

//删除元素为10的结点
list.Delete(10);

cout << "打印链表：" << endl;
list.Display();

//删除元素为36的结点
list.Delete(36);
cout << "打印链表：" << endl;
list.Display();

system("pause");
return 0;
}

#### 算法导论 第十章 基本数据结构 练习10.1-2

2017-12-11 20:20:56

#### 算法导论第十章基本数据结构

2014-02-14 16:58:26

#### 算法导论10（基本数据结构）

2015-04-19 11:01:57

#### 算法导论读书笔记（10）基本数据结构

2017-05-16 11:58:07

#### 算法导论 第22章 图的基本算法 22.5 强联通分支

2012-08-07 18:42:42

#### 用Python实现算法导论中的算法 序

2017-05-04 10:00:50

#### 算法导论 第10章 思考题

2012-09-19 18:52:50

#### 算法导论基本数据结构

2017-05-21 00:31:56

#### 基本数据结构(算法导论)与python

2011-05-26 17:28:00

#### 算法导论 第10章 10.2 链表

2012-06-29 10:03:37