C++编程实例(c++封装链表、栈、队列)
- 格式:doc
- 大小:68.00 KB
- 文档页数:19
编程实例是指在编程过程中具体的实际应用案例。
通过编程实例,可以帮助初学者更好地理解编程语言的运用和实际应用,提高编程技能和解决实际问题的能力。
下面将通过编程实例介绍一些常见的编程技巧和应用场景。
一、数据结构的应用1. 栈的应用:栈是一种后进先出的数据结构,常用于表达式求值、括号匹配等实际场景。
我们可以通过栈来实现一个简单的计算器,将中缀表达式转换为后缀表达式,然后利用栈求解后缀表达式得到最终结果。
2. 队列的应用:队列是一种先进先出的数据结构,常用于模拟排队和调度等场景。
可以通过队列实现一个简单的模拟银行排队系统,记录每位客户的到达时间和处理时间,模拟银行的办理业务过程。
3. 链表的应用:链表是一种动态数据结构,常用于实现各种高效的数据操作。
可以通过链表实现一个简单的通联方式簿程序,实现通联方式号码的增删改查操作。
二、算法的实际应用1. 排序算法的应用:排序算法是计算机领域中基础且常用的算法之一,各种排序算法的应用场景也非常广泛。
可以通过冒泡排序算法对学生成绩进行排序,或者通过快速排序算法对大量数据进行高效排序。
2. 查找算法的应用:查找算法也是编程中常用的算法之一,常用于在大量数据中快速准确地查找目标元素。
可以通过二分查找算法在有序数组中快速查找目标元素,提高查找效率。
3. 图算法的应用:图算法是一种复杂且广泛应用的算法,常用于网络拓扑分析、路径规划等实际场景。
可以通过最短路径算法对城市交通网络进行优化规划,提高交通效率。
三、面向对象的编程实例1. 类和对象的应用:面向对象编程是一种常用的编程范式,通过类和对象的概念可以更好地组织和管理程序的结构和数据。
可以通过类和对象实现一个简单的学生信息管理系统,将学生的基本信息封装到对象中,实现对学生信息的增删改查操作。
2. 继承和多态的应用:继承和多态是面向对象编程中重要的概念,可以帮助我们更好地复用和扩展已有的代码。
可以通过继承和多态实现一个简单的员工管理系统,定义员工类和经理类,实现对员工和经理的管理操作。
数据结构经典题目及c语言代码一、线性表1. 顺序表顺序表是一种利用连续存储空间存储元素的线性表。
以下是一个顺序表的经典题目及C语言代码实现:```c#define MaxSize 50typedef struct {int data[MaxSize]; // 存储元素的数组int length; // 顺序表的当前长度} SeqList;// 初始化顺序表void initList(SeqList *L) {L->length = 0;}// 插入元素到指定位置void insert(SeqList *L, int pos, int elem) {if (pos < 1 || pos > L->length + 1) {printf("插入位置无效\n");return;}if (L->length == MaxSize) {printf("顺序表已满,无法插入\n"); return;}for (int i = L->length; i >= pos; i--) { L->data[i] = L->data[i - 1];}L->data[pos - 1] = elem;L->length++;}// 删除指定位置的元素void delete(SeqList *L, int pos) {if (pos < 1 || pos > L->length) {printf("删除位置无效\n");return;}for (int i = pos - 1; i < L->length - 1; i++) {L->data[i] = L->data[i + 1];}L->length--;}// 获取指定位置的元素值int getElement(SeqList *L, int pos) {if (pos < 1 || pos > L->length) {printf("位置无效\n");return -1;}return L->data[pos - 1];}```2. 链表链表是一种利用非连续存储空间存储元素的线性表。
数据结构C语言版实验报告一、实验目的本次实验旨在通过使用 C 语言实现常见的数据结构,加深对数据结构基本概念、原理和操作的理解,提高编程能力和解决实际问题的能力。
二、实验环境操作系统:Windows 10编程环境:Visual Studio 2019编程语言:C 语言三、实验内容1、线性表顺序表的实现与操作链表的实现与操作2、栈和队列栈的实现与应用(表达式求值)队列的实现与应用(模拟排队)3、树和二叉树二叉树的遍历(前序、中序、后序)二叉搜索树的实现与操作4、图图的存储结构(邻接矩阵、邻接表)图的遍历(深度优先搜索、广度优先搜索)四、实验步骤及结果1、线性表顺序表的实现与操作定义顺序表的数据结构,包括数组和表的长度。
实现顺序表的初始化、插入、删除、查找等操作。
测试顺序表的各种操作,输出操作结果。
```cinclude <stdioh>include <stdlibh>define MAX_SIZE 100typedef struct {int dataMAX_SIZE;int length;} SeqList;//初始化顺序表void initList(SeqList L) {L>length = 0;}//插入元素到顺序表int insertList(SeqList L, int pos, int element) {if (L>length >= MAX_SIZE || pos < 0 || pos > L>length) {return 0;}for (int i = L>length 1; i >= pos; i) {L>datai + 1 = L>datai;}L>datapos = element;L>length++;return 1;}//删除顺序表中的元素int deleteList(SeqList L, int pos) {if (pos < 0 || pos >= L>length) {return 0;}for (int i = pos; i < L>length 1; i++){L>datai = L>datai + 1;}L>length;return 1;}//查找顺序表中的元素int searchList(SeqList L, int element) {for (int i = 0; i < Llength; i++){if (Ldatai == element) {return i;}}return -1;}int main(){SeqList L;initList(&L);insertList(&L, 0, 10);insertList(&L, 1, 20);insertList(&L, 2, 30);printf("顺序表元素: ");for (int i = 0; i < Llength; i++){printf("%d ", Ldatai);}printf("\n");int pos = searchList(L, 20);if (pos!=-1) {printf("元素 20 在顺序表中的位置: %d\n", pos);} else {printf("顺序表中未找到元素 20\n");}deleteList(&L, 1);printf("删除元素后的顺序表元素: ");for (int i = 0; i < Llength; i++){printf("%d ", Ldatai);}printf("\n");return 0;}```实验结果:成功实现顺序表的初始化、插入、删除、查找等操作,输出结果符合预期。
数据结构实验三栈和队列的应用数据结构实验三:栈和队列的应用在计算机科学领域中,数据结构是组织和存储数据的重要方式,而栈和队列作为两种常见的数据结构,具有广泛的应用场景。
本次实验旨在深入探讨栈和队列在实际问题中的应用,加深对它们特性和操作的理解。
一、栈的应用栈是一种“后进先出”(Last In First Out,LIFO)的数据结构。
这意味着最后进入栈的元素将首先被取出。
1、表达式求值在算术表达式的求值过程中,栈发挥着重要作用。
例如,对于表达式“2 + 3 4”,我们可以通过将操作数压入栈,操作符按照优先级进行处理,实现表达式的正确求值。
当遇到数字时,将其压入操作数栈;遇到操作符时,从操作数栈中弹出相应数量的操作数进行计算,将结果压回操作数栈。
最终,操作数栈中的唯一值就是表达式的结果。
2、括号匹配在程序代码中,检查括号是否匹配是常见的任务。
可以使用栈来实现。
遍历输入的字符串,当遇到左括号时,将其压入栈;当遇到右括号时,弹出栈顶元素,如果弹出的左括号与当前右括号类型匹配,则继续,否则表示括号不匹配。
3、函数调用和递归在程序执行过程中,函数的调用和递归都依赖于栈。
当调用一个函数时,当前的执行环境(包括局部变量、返回地址等)被压入栈中。
当函数返回时,从栈中弹出之前保存的环境,继续之前的执行。
递归函数的执行也是通过栈来实现的,每次递归调用都会在栈中保存当前的状态,直到递归结束,依次从栈中恢复状态。
二、队列的应用队列是一种“先进先出”(First In First Out,FIFO)的数据结构。
1、排队系统在现实生活中的各种排队场景,如银行排队、餐厅叫号等,可以用队列来模拟。
新到达的顾客加入队列尾部,服务完成的顾客从队列头部离开。
通过这种方式,保证了先来的顾客先得到服务,体现了公平性。
2、广度优先搜索在图的遍历算法中,广度优先搜索(BreadthFirst Search,BFS)常使用队列。
从起始节点开始,将其放入队列。
数据结构c语言实现数据结构是计算机科学中重要的一个领域,它研究不同的数据组织方式,以及在这些数据上进行各种操作的算法。
常见的数据结构包括数组、栈、队列、链表、树、图等。
在C语言中,数据结构是通过使用结构体来实现的。
结构体是由一组数据成员组合而成的自定义数据类型,可以包含不同数据类型的数据成员。
以下是如何在C语言中实现不同的数据结构。
数组数组是数据结构中最基本的数据结构之一。
C语言中的数组定义方式如下:```int array[5];```这个代码定义了一个名为array的数组,其中有5个元素,每个元素的类型是整数。
要访问数组中的元素,可以通过下标访问:这个代码设置了数组中第一个元素的值为1。
栈栈是一种后进先出(LIFO)的数据结构。
使用C语言中的数组可以实现栈。
以下是一个简单的栈实现:```#define MAXSIZE 100int stack[MAXSIZE];int top = -1;void push(int data){if(top<MAXSIZE-1){ //判断栈是否满了stack[++top] = data; //插入数据}}int isEmpty(){return top==-1; //栈是否为空}队列链表链表是一个由节点组成的数据结构,每个节点包含一个数据成员和一个指向下一个节点的指针。
在C语言中,链表可以使用结构体和指针来实现。
以下是一个单向链表的实现:```struct node{int data;struct node *next;};struct node *head = NULL;void insert(int data){struct node *new_node = (struct node*) malloc(sizeof(struct node)); //分配内存new_node->data = data; //初始化数据new_node->next = head; //新节点指向当前头节点head = new_node; //更新头节点}void delete(int data){struct node *current_node = head; //从头节点开始查找struct node *previous_node = NULL;while(current_node!=NULL&¤t_node->data!=data){ //查找节点previous_node = current_node;current_node = current_node->next;}if(current_node!=NULL){ //找到了节点if(previous_node!=NULL){ //非头节点previous_node->next = current_node->next; }else{ //头节点head = current_node->next;}free(current_node); //释放内存}}树。
c语言队列数据结构队列是一种常见的数据结构,它遵循先进先出(FIFO)的原则。
在C语言中,我们可以使用数组或链表来实现队列数据结构。
本文将介绍C语言中队列的实现方法及其应用。
一、数组实现队列数组是一种简单且常用的数据结构,可以用来实现队列。
在C语言中,我们可以使用数组来创建一个固定大小的队列。
下面是一个使用数组实现队列的示例代码:```c#include <stdio.h>#define MAX_SIZE 100int queue[MAX_SIZE];int front = -1;int rear = -1;void enqueue(int data) {if (rear == MAX_SIZE - 1) {printf("队列已满,无法插入元素。
\n");return;}if (front == -1) {front = 0;}rear++;queue[rear] = data;}void dequeue() {if (front == -1 || front > rear) {printf("队列为空,无法删除元素。
\n"); return;}front++;}int getFront() {if (front == -1 || front > rear) {printf("队列为空。
\n");return -1;}return queue[front];}int isEmpty() {if (front == -1 || front > rear) {return 1;}return 0;}int main() {enqueue(1);enqueue(2);enqueue(3);printf("队列的第一个元素:%d\n", getFront());dequeue();printf("队列的第一个元素:%d\n", getFront());return 0;}```在上述代码中,我们使用了一个数组`queue`来存储队列的元素。
以下是使用C语言实现链式队列的代码,可以实现输入数字入队,输入字符出队的功能:#include <stdio.h>#include <stdlib.h>#include <string.h>// 定义链式队列结构体typedef struct QueueNode {int data; // 存储数字struct QueueNode* next; // 指向下一个节点} QueueNode;// 定义链式队列结构体typedef struct {QueueNode* front; // 指向队头节点QueueNode* rear; // 指向队尾节点} LinkedQueue;// 初始化链式队列void InitQueue(LinkedQueue* queue) {queue->front = NULL;queue->rear = NULL;}// 入队操作void EnQueue(LinkedQueue* queue, int data) {QueueNode* newNode =(QueueNode*)malloc(sizeof(QueueNode)); // 创建新节点newNode->data = data; // 将数字存储到新节点中newNode->next = NULL; // 新节点的下一个节点为空if (queue->rear == NULL) { // 如果队列为空,将新节点设置为队头和队尾queue->front = newNode;queue->rear = newNode;} else { // 如果队列不为空,将新节点添加到队尾,并更新队尾指针queue->rear->next = newNode;queue->rear = newNode;}}// 出队操作,返回出队的字符,如果队列为空,返回-1char DeQueue(LinkedQueue* queue) {if (queue->front == NULL) { // 如果队列为空,返回-1表示失败return -1;} else { // 如果队列不为空,将队头节点从队列中删除,并返回其存储的字符,同时更新队头指针char data = queue->front->data;QueueNode* temp = queue->front;queue->front = queue->front->next;free(temp); // 释放已删除节点的内存空间return data;}}。
数据结构c语言版创建单链表的代码单链表作为常用的线性结构之一,常常用于解决以链式方式存储数据的问题。
创建单链表需要掌握一些基础的数据结构知识以及对C语言的熟练运用。
接下来,本文将分步骤地阐述数据结构C语言版创建单链表的代码。
第一步,定义单链表结构体并定义节点类型。
在C语言中,我们可以通过结构体的方式定义单链表,其中结构体中包含两个成员变量,分别为存储数据的data和指向下一个节点的指针next。
对于节点类型,我们可以使用typedef对节点类型进行定义,例如:```struct ListNode {int data;struct ListNode *next;};typedef struct ListNode ListNode;```在以上代码中,我们首先定义了一个结构体ListNode作为单链表的元素类型,其中包含存储数据的data和指向下一个元素的指针next。
接着我们使用typedef将结构体ListNode定义为仿函数ListNode,从而使其更加方便使用。
第二步,初始化单链表。
在创建单链表之前,我们需要先将单链表的头指针初始化为NULL,表示当前链表为空。
具体代码如下:```ListNode *createLinkedList() {ListNode *head = NULL;return head;}```以上代码中,函数createLinkedList用于创建并初始化单链表,其中head表示单链表头指针,我们将其初始化为NULL。
第三步,向单链表中添加元素。
在单链表中添加元素需要借助于指针的指向关系。
具体来说,我们需要先创建新的节点,将其数据添加到节点中,然后将新节点的next指针指向之前的头节点,最后将头指针指向新节点。
具体过程如下:```ListNode *addListNode(ListNode **head, int val) {ListNode *newNode = (ListNode *)malloc(sizeof(ListNode)); newNode->data = val;newNode->next = *head;*head = newNode;return *head;}```在以上代码中,函数addListNode接收一个指向头指针的指针head,以及需要添加的元素值val。
c++ 队列的编程题目队列是一种具有先进先出特性的数据结构,在编程中有着广泛的应用。
下面是一些关于队列的编程题目,供大家参考和学习。
1. 实现队列的基本操作题目描述:要求实现一个基本的队列数据结构,包括入队(enqueue)、出队(dequeue)、检查队首元素等基本操作。
可以使用数组或链表来实现队列。
解题思路:队列是一种先进先出数据结构,可以使用循环数组或链表来存储元素。
入队操作可以在尾部添加元素,出队操作可以从头部删除元素。
检查队首元素可以使用一个变量来记录队首元素的位置。
代码实现:```c#include <stdio.h>#define MAX_SIZE 100typedef struct {int data[MAX_SIZE];int front; // 队首元素的位置int rear; // 队尾元素的下一个位置} Queue;void init(Queue *q) {q->front = 0;q->rear = -1;}int is_empty(Queue *q) {return q->rear == -1;}int is_full(Queue *q) {return (q->rear + 1) % MAX_SIZE == q->front; }void enqueue(Queue *q, int value) {if (is_full(q)) {printf("Queue is full!\n");return;}q->rear = (q->rear + 1) % MAX_SIZE;q->data[q->rear] = value;}int dequeue(Queue *表示队列为空的返回值}int value = q->data[q->front];q->front = (q->front + 1) % MAX_SIZE;return value;}```2. 队列的应用场景及优化方案队列在编程中有着广泛的应用,例如在操作系统中用于实现进程调度、在编译器中用于实现语法分析等。
数据结构栈和队列实验报告一、实验目的本次实验的主要目的是深入理解和掌握数据结构中的栈和队列的基本概念、操作原理以及实际应用。
通过编程实现栈和队列的相关操作,加深对其特性的认识,并能够运用栈和队列解决实际问题。
二、实验环境本次实验使用的编程语言为C++,开发工具为Visual Studio 2019。
三、实验原理(一)栈栈(Stack)是一种特殊的线性表,其操作遵循“后进先出”(Last In First Out,LIFO)的原则。
可以将栈想象成一个只有一端开口的容器,元素只能从开口端进出。
入栈操作(Push)将元素添加到栈顶,出栈操作(Pop)则从栈顶移除元素。
(二)队列队列(Queue)也是一种线性表,但其操作遵循“先进先出”(FirstIn First Out,FIFO)的原则。
队列就像是排队买票的队伍,先到的人先接受服务。
入队操作(Enqueue)将元素添加到队列的末尾,出队操作(Dequeue)则从队列的头部移除元素。
四、实验内容(一)栈的实现与操作1、定义一个栈的数据结构,包含栈顶指针、存储元素的数组以及栈的最大容量等成员变量。
2、实现入栈(Push)操作,当栈未满时,将元素添加到栈顶,并更新栈顶指针。
3、实现出栈(Pop)操作,当栈不为空时,取出栈顶元素,并更新栈顶指针。
4、实现获取栈顶元素(Top)操作,返回栈顶元素但不进行出栈操作。
5、实现判断栈是否为空(IsEmpty)和判断栈是否已满(IsFull)的操作。
(二)队列的实现与操作1、定义一个队列的数据结构,包含队头指针、队尾指针、存储元素的数组以及队列的最大容量等成员变量。
2、实现入队(Enqueue)操作,当队列未满时,将元素添加到队尾,并更新队尾指针。
3、实现出队(Dequeue)操作,当队列不为空时,取出队头元素,并更新队头指针。
4、实现获取队头元素(Front)操作,返回队头元素但不进行出队操作。
5、实现判断队列是否为空(IsEmpty)和判断队列是否已满(IsFull)的操作。
二元方程封装(C++)//封装#ifndef _CBIEQUTION_H#define _CBIEQUTION_H#include <cmath>#include <iostream>using namespace std;typedef struct _x1x2{float x1;float x2;}X1X2;class CBiEqution{private:int _a;int _b;int _c;inline int getBB4AC()const{return _b*_b-4*_a*_c;}public:CBiEqution(int a=1,int b=2,int c=1);CBiEqution& operator+(const CBiEqution equ);CBiEqution& operator-(const CBiEqution equ);friend bool operator==(const CBiEqution equ1,const CBiEqution equ2);friend istream& operator>>(istream& is,CBiEqution &equ);friend ostream& operator<<(ostream& os,CBiEqution &equ);X1X2 getBiEquX1X2() const;};CBiEqution::CBiEqution(int a,int b,int c):_a(a),_b(b),_c(c){}CBiEqution& CBiEqution::operator+(const CBiEqution equ){return CBiEqution(_a+equ._a,_b+equ._b,_c+equ._c);}CBiEqution& CBiEqution::operator-(const CBiEqution equ){return CBiEqution(_a-equ._a,_b-equ._b,_c-equ._c);}bool operator==(const CBiEqution equ1,const CBiEqution equ2){if ((equ1._a==equ2._a)&&(equ2._b==equ1._b)&&(equ1._c==equ2._c)) {return true;}else{return false;}}istream& operator>>(istream &is,CBiEqution &equ){cout<<"please input the equ's A:";is>>equ._a;cout<<"please input the equ's B:";is>>equ._b;cout<<"please input the equ's C:";is>>equ._c;return is;}ostream& operator<<(ostream &os,CBiEqution &equ){os<<"the equ expression is:";os<<equ._a<<"x^2+("<<equ._b<<")x+("<<equ._c<<")=0"<<endl;os<<"the answer for equ is:"<<endl;if (equ.getBB4AC()>=0){os<<"x1="<<equ.getBiEquX1X2().x1<<endl;os<<"x2="<<equ.getBiEquX1X2().x2<<endl;}else{os<<"x1="<<equ.getBiEquX1X2().x1<<"+("<<equ.getBiEquX1X2().x2<<") i"<<endl;os<<"x2="<<equ.getBiEquX1X2().x1<<"-("<<equ.getBiEquX1X2().x2<<") i"<<endl;}return os;}X1X2 CBiEqution::getBiEquX1X2() const{int num;X1X2 temp;num=getBB4AC();if (num>=0){temp.x1=(-1.0*_b+sqrt((double)num))/(2*_a);temp.x2=(-1.0*_b-sqrt((double)num))/(2*_a);}else{temp.x1=(-1.0*_b)/(2*_a);temp.x2=sqrt((double)abs(num))/(2*_a);}return temp;}#endif//测试#include "CBiEquation.h"int main(){CBiEqution e1,e2;cin>>e1;cin>>e2;cout<<e1;cout<<e2;/*e2=e1+e2;cout<<e2;*/return 0;}二叉树封装//二叉树封装实现(建立二叉树、前序、中、后序遍历)#ifndef _CBITREE_H#define _CBITREE_H#include <iostream>using namespace std;typedef struct _node{int data;_node* left;_node* right;}NODE,*PNODE;class CBiTree{private:PNODE root;void destroy(PNODE r){if (r!=NULL){destroy(r->left);destroy(r->right);delete r;r=NULL;}}void clear(){destroy(root);}PNODE build(PNODE r){int data;cout<<"输入结点的值(0 for NULL):";cin>>data;if (data!=0){r=new NODE;r->data=data;r->left=build(r->left);r->right=build(r->right);}else{r=NULL;}return r;}void pre(PNODE temp){if (temp!=NULL){cout<<temp->data<<" ";pre(temp->left);pre(temp->right);}}void in(PNODE temp){if (temp!=NULL){in(temp->left);cout<<temp->data<<" ";in(temp->right);}}void post(PNODE temp){if (temp!=NULL){post(temp->left);post(temp->right);cout<<temp->data<<" ";}}public:CBiTree(){root=NULL;}void create(){root=build(root);}void preOrder(){pre(root);}void inOrder(){in(root);}void postOrder(){post(root);}~CBiTree(){clear();}};#endif//测试#include "CBiTree.h"int main(){CBiTree bt;bt.create();cout<<"前序遍历:"<<endl;bt.preOrder();cout<<endl;cout<<"中序遍历:"<<endl;bt.inOrder();cout<<endl;cout<<"后序遍历:"<<endl;bt.postOrder();cout<<endl;return 0;}线性循环队列实现//循环队列封装#ifndef _CCIRQUEUE_H#define _CCIRQUEUE_H#include <iostream>#include <cstdlib>using namespace std;const int QUEUE_SIZE=20;class CCirQueue{private:int data[QUEUE_SIZE];int rear;int front;bool isEmpty()const{if (rear==front){return true;}else{return false;}}bool isFull() const{if ((rear+1)%QUEUE_SIZE==front){return true;}else{return false;}}public:CCirQueue();int size(){return QUEUE_SIZE-1;}void pushQueue(int val);int popQueue();};CCirQueue::CCirQueue(){rear=0;front=0;}void CCirQueue::pushQueue(int val) {if (isFull()){cout<<"full......"<<endl;exit(0);}rear=(rear+1)%QUEUE_SIZE;data[rear]=val;}int CCirQueue::popQueue(){if (isEmpty()){cout<<"empty......"<<endl;exit(0);}front=(front+1)%QUEUE_SIZE;return data[front];}#endif//测试#include "CCirQueue.h"int main(){CCirQueue qq;cout<<"入队"<<endl;for (int i=0;i<qq.size();i++){qq.pushQueue(i+10);cout<<i+10<<"\t";}cout<<endl;cout<<"出队:"<<endl;for (int i=0;i<qq.size();i++){cout<<qq.popQueue()<<"\t";}return 0;}链式队列//封装#ifndef __CLINKQUEUE_H#define __CLINKQUEUE_H#include <iostream>using namespace std;typedef struct _node{int data;_node* next;public:_node(){}_node(int val,_node* _next=NULL){data=val;next=_next;}}NODE,*PNODE;class CLinkQueue{private:PNODE head;PNODE rear;void clear(){PNODE temp=head;while(temp->next!=NULL){head=head->next;delete temp;temp=head;}delete head;delete rear;}public:CLinkQueue();void enterQueue(int val);PNODE outQueue();~CLinkQueue(){clear();}};CLinkQueue::CLinkQueue(){head=new NODE(int(),NULL);rear=head;}void CLinkQueue::enterQueue(int val){PNODE temp1=new NODE(val,NULL);rear->next=temp1;rear=temp1;}PNODE CLinkQueue::outQueue(){PNODE temp=head->next;if (temp==NULL){return NULL;}head->next=head->next->next;return temp;}#endif//测试#include "CLinkQueue.h"int main(){CLinkQueue lq;PNODE temp;int iva;while(1){cout<<"input the number:";cin>>iva;if (iva==0){break;}/*cout<<iva<<endl;*/lq.enterQueue(iva);}cout<<endl;temp=lq.outQueue();while(temp!=NULL){cout<<temp->data<<"\t";temp=lq.outQueue();}return 0;}线性栈实现#ifndef _STACK_H#define _STACK_H#include <iostream>#include <cstdlib>using namespace std;const int STACKSIZE=20;class CStack{private:int data[STACKSIZE];int top;public:CStack();int getSize() const;void push(int);void pop();int getTop() const;bool isEmpty() const;};CStack::CStack(){top=-1;}int CStack::getSize()const{return STACKSIZE;}void CStack::push(int val){top++;if (top<STACKSIZE){data[top]=val;}else{cout<<"stack full..."<<endl;exit(0);}}void CStack::pop(){if (top==-1){cout<<"stack empty....."<<endl;exit(0);}top--;}int CStack::getTop()const{if (top!=-1||top!=STACKSIZE){return data[top];}cout<<"can not get the val..."<<endl;return 0;}bool CStack::isEmpty()const{return (top==-1?true:false);}#endif链式栈实现ifndef _CLINKSTACK_H#define _CLINKSTACK_H#include <iostream>using namespace std;typedef struct _node{int data;_node* next;public:_node(){}_node(int _data,_node* _next=NULL){data=_data;next=_next;}}NODE,*PNODE;class CLinkStack{private:PNODE head;void clear(){PNODE temp=head;if (head!=NULL){head=head->next;delete temp;while(head!=NULL){temp=head;head=head->next;delete temp;}}}public:CLinkStack();void push(int val);PNODE pop();/*bool isEmpty();*/~CLinkStack();};CLinkStack::CLinkStack(){head=new NODE(0,NULL);}bool CLinkStack::isEmpty() {PNODE temp=head->next;if (temp==NULL){return true;}else{return false;}}void CLinkStack::push(int val){PNODE temp=head->next;PNODE temp1=new NODE(val,NULL);if (temp!=NULL){temp1->next=temp;head->next=temp1;}else{head->next=temp1;}}PNODE CLinkStack::pop(){PNODE temp=head->next;if (temp==NULL){return NULL;}else{head->next=head->next->next;return temp;}}CLinkStack::~CLinkStack(){clear();}#endif//测试include "CLinkStack.h"#include <iostream>using namespace std;int main(){CLinkStack ls;PNODE temp=NULL;cout<<"push:"<<endl;int j=0;for(int i=0;i<10;i++){cout<<"push"<<"("<<i+10<<")";if ((i+1)%5==0){cout<<endl;}ls.push(i+10);}temp=ls.pop();while(temp!=NULL){j++;if ((j)%5==0){cout<<endl;}cout<<"pop:"<<temp->data;temp=ls.pop();}return 0;}单链表实现//封装ifndef _SLIST_H#define _SLIST_H#include <iostream>using namespace std;struct _node{int data;_node* next;public:_node(){}_node(int _data,_node* temp){data=_data;next=temp;}};class CList{private:_node* head;int size;void clear(){_node* temp;if (head!=NULL){temp=head;head=head->next;delete temp;while(head!=NULL){temp=head;head=head->next;delete temp;}}}_node* getNode(int n){int iNum=0;_node* temp1=head->next;if (n<=0||n>size){cout<<"invalid count of node...."<<endl;return NULL;}while(iNum<=size){iNum++;if (iNum==n){return temp1;}temp1=temp1->next;}return NULL;}public:CList();void createList();int getSize();/*_node* getValue(int n);*/void setValue(int n,int val);void insertNode(int n,int val);void delNode(int n);friend ostream& operator<<(ostream& os,CList&ll);~CList();};CList::CList(){head=new _node(0,NULL);size=0;}void CList::createList(){int nNode=0;int val;_node* temp1=head,*temp2;cout<<"please input the number of node:";while(1){cin>>nNode;if (nNode<=0){cout<<"invalid number!"<<endl;cout<<"please input the number of node again:";continue;}size=nNode;break;}for (int i=0;i<nNode;i++){cout<<"please input the"<<i+1<<" number 's value:";cin>>val;temp2=new _node(val,NULL);temp1->next=temp2;temp1=temp1->next;temp2=temp2->next;}}int CList::getSize(){return size;}void CList::setValue(int n,int val){_node* temp=getNode(n);if (temp==NULL){return;}temp->data=val;}void CList::insertNode(int n,int val)//after the n node inserting a node {_node* temp=getNode(n);_node* temp1,*temp2;if (temp==NULL){cout<<"insert error"<<endl;return;}temp1=new _node(val,NULL);temp2=temp->next;if (temp2==NULL){temp->next=temp1;}else{temp->next=temp1;temp1->next=temp2;}}void CList::delNode(int n)//del the n node{_node* temp;_node* temp1;if (n==1){temp=head->next;head->next=NULL;delete temp;return;}temp=getNode(n-1);if (temp==NULL){cout<<"del error"<<endl;return;}temp1=temp->next;temp->next=temp1->next;delete temp1;}ostream& operator<<(ostream& os,CList& ll) {os<<"the values of the list is:"<<endl;_node* temp=ll.head;temp=temp->next;if (temp==NULL){cout<<"0 values in list"<<endl;return os;}int iNum=0;while(temp!=NULL){iNum++;if (iNum%5==0){cout<<endl;}cout<<temp->data<<"\t";temp=temp->next;}return os;}CList::~CList(){clear();}#endif。