C++数据结构实验二栈和队列

实验内容

1.建立一个空栈,并按要求完成:(顺序存储)
(1)编写入栈函数,随机产生10个100以内的整数,并按输入顺序依次入栈,考虑判断上溢情况;
(2)编写出栈函数,从键盘输入出栈元素个数n(n<10),将n个元素出栈并显示出栈元素,考虑判断下溢情况;
(3)编写取栈顶元素函数,若栈不为空,则取出栈顶元素显示;否则提示栈空。
2. 建立一个空队列,并按要求完成:(链接存储)
(1)编写入队函数,随机产生10个100以内的整数,并按输入顺序依次入队,考虑判断队满情况;
(2)编写出队函数,从键盘输入出队元素个数n(n<10),将n个元素出队后显示队中数据结果,考虑判断队空情况;
(3)编写取队头元素函数,若队不为空,则取队头元素显示;否则提示队空。
3.设计算法实现将任一中缀表达式转换成对应的后缀表达式。

实验代码

1.
// Stack.h
#pragma once
#include

template<typename T>
class Stack
{
     
public:
	Stack();
	~Stack();
	void Push(T element);
	bool Pop(T* p_reception = NULL);
	bool GetTop(T*);
	int Length() {
      return m_length; }
private:
	T* m_data;
	int m_size;   // 最大尺寸
	int m_length; // 栈中元素数量
	int m_top;    // 栈顶索引
};

template<typename T>
Stack<T>::Stack() : 
	m_data(nullptr), m_size(10), m_length(0), m_top(0)
{
     
	m_data = new T[m_size];
}

template<typename T>
Stack<T>::~Stack() 
{
     
	delete[] m_data;
}


template<typename T>
void Stack<T>::Push(T element)
{
     
	if (m_length >= m_size)
	{
     
		std::cout << "栈已满!入栈会导致上溢!" << std::endl;
		return;
	}
	else
	{
     
		m_data[m_top] = element;
		m_top++;
		m_length++;
		return;
	}
}

template<typename T>
bool Stack<T>::Pop(T* p_reception)
{
     
	if (m_length <= 0)
	{
     
		std::cout << "栈为空!出栈会导致下溢!" << std::endl;
		return false;
	}
	else
	{
     
		m_top--;
		m_length--;
		*p_reception = m_data[m_top];
		return true;
	}
}

template<typename T>
void PopSome(Stack<T>& stack, int num)
{
     
	if (stack.Length() < num)
	{
     
		std::cout << "出栈会导致下溢!" << std::endl;
		return;
	}
	T* temp = new T;
	std::cout << "出栈元素有:";
	for (int i = 0; i < num; i++)
	{
     
		stack.Pop(temp);
		std::cout << *temp << " ";
	}
	delete temp;
	std::cout << std::endl;
}

template<typename T>
bool Stack<T>::GetTop(T* pTop)
{
     
	if (m_length > 0)
	{
     
		*pTop = m_data[m_top - 1];
		return true;
	}
	else
	{
     
		std::cout << "栈为空!,栈顶无元素!";
		return false;
	}
}
// main.cpp
#include
#include
#include
#include"Stack.h"

int RandomDataGenerator();

int main()
{
     
	Stack<int> myStack;

	// 入栈
	for (size_t i = 0; i < 10; i++)
	{
     
		myStack.Push(RandomDataGenerator());
	}

	// 上溢
	myStack.Push(1);

	int* temp1 = new int;
	// 显示栈顶元素
	if(myStack.GetTop(temp1))
		std::cout << "栈顶元素为:" << *temp1;
	delete temp1;

	std::cout << std::endl;

	PopSome(myStack, 10);

	std::cout << std::endl;

	// 下溢
	myStack.Pop();

	int* temp2 = new int;
	// 显示栈顶元素
	if (myStack.GetTop(temp2))
		std::cout << "栈顶元素为:" << *temp2;
	delete temp2;

	return 0;
}

int RandomDataGenerator()
{
     
	return rand() % 101;
}
2.
// LinkList.h
#pragma once
#include

template<typename T>
struct Node
{
     
    Node() :pNext(NULL)
    {
      }
    Node(T data) : data(data), pNext(NULL)
    {
      }
    T data;
    Node* pNext;
};

template<typename T>
class LinkList
{
     
public:
    template<typename T>
    friend LinkList<T> MergeListInOrder(const LinkList<T>& l1, const LinkList<T>& l2);
    
    LinkList();
    LinkList(T* data, int length);
    LinkList(const LinkList&);
    ~LinkList();
    void Add(T data);
    void Display();
    void Sort();
    //int Length() const { return m_length; }
private:
    Node<T>* m_pHead;
    Node<T>* m_pTail;
    int m_length;
};

template<typename T>
LinkList<T>::LinkList() :
    m_pHead(NULL), m_length(0)
{
     
    m_pHead = new Node<T>();
    m_pTail = m_pHead;
}

template<typename T>
LinkList<T>::LinkList(T* data, int length) :
    m_pHead(NULL), m_length(0)
{
     
    m_pHead = new Node<T>();
    m_pTail = m_pHead;
    for (int i = 0; i < length; i++)
    {
     
        Add(data[i]);
    }
}

template<typename T>
LinkList<T>::LinkList(const LinkList& another) :
        m_pHead(NULL), m_length(0)
{
     
    m_pHead = new Node<T>();
    m_pTail = m_pHead;
    Node<T>* pTemp = another.m_pHead->pNext;
    for (size_t i = 0; i < another.m_length; i++)
    {
     
        this->Add(pTemp->data);
        pTemp = pTemp->pNext;
    }
}

template<typename T>
LinkList<T>::~LinkList()
{
     
    Node<T>* pNode = m_pHead->pNext;
    while (pNode->pNext != NULL)
    {
     
        Node<T>* pTemp = pNode->pNext;
        delete pNode;
        pNode = pTemp;
    }
    delete m_pHead;
}

template<typename T>
void LinkList<T>::Add(T data)
{
     
    m_pTail->pNext = new Node<T>(data);
    m_pTail = m_pTail->pNext;
    m_length++;
}

template<typename T>
void LinkList<T>::Display()
{
     
    Node<T>* pTemp = m_pHead->pNext;
    while (pTemp != NULL)
    {
     
        std::cout << pTemp->data << " ";
        pTemp = pTemp->pNext;
    }
    std::cout << std::endl;
}

template<typename T>
void LinkList<T>::Sort()
{
     
    for (size_t i = 0; i < m_length; i++)
    {
     
        Node<T>* pTemp = m_pHead->pNext;
        for (size_t j = 0; j < m_length - 1 - i; j++)
        {
     
            if (pTemp->data > pTemp->pNext->data)
            {
     
                T temp = pTemp->data;
                pTemp->data = pTemp->pNext->data;
                pTemp->pNext->data = temp;
            }
            pTemp = pTemp->pNext;
        }
    }
}

// 添加一些数据到链表中
template<typename T>
void AddSomeData(LinkList<T>& linkList, T* data, int number)
{
     
    T temp;
    for (size_t i = 0; i < number; i++)
    {
     
        temp = data[i];
        linkList.Add(temp);
    }
}

template<typename T>
LinkList<T> MergeListInOrder(const LinkList<T>& l1, const LinkList<T>& l2)
{
     
    Node<T>* pTemp = NULL;
    T* data1 = new T[l1.m_length];
    T* data2 = new T[l2.m_length];
    pTemp = l1.m_pHead->pNext;
    for (int i = 0; i < l1.m_length; i++)
    {
     
        data1[i] = pTemp->data;
        pTemp = pTemp->pNext;
    }
    pTemp = l2.m_pHead->pNext;
    for (int i = 0; i < l2.m_length; i++)
    {
     
        data2[i] = pTemp->data;
        pTemp = pTemp->pNext;
    }
    LinkList<T> newList;
    AddSomeData(newList, data1, l1.m_length);
    AddSomeData(newList, data2, l2.m_length);
    newList.Sort();

    delete[] data1;
    delete[] data2;
    return newList;
}
// main.cpp
#include
#include
#include
#include"LinkList.h"

int* RandomDataGenerator(int* a, int length = 10);

int main()
{
     
	int arr1[10];
	RandomDataGenerator(arr1);
	LinkList<int> linkList1(arr1, 10);

	//-------------------
	time_t current_time;
	time_t start_time;

	time(&start_time);

	do
	{
     
		time(&current_time);
	} while ((current_time - start_time) < 1);
	//-------------------

	int arr2[10];
	RandomDataGenerator(arr2);
	LinkList<int> linkList2(arr2, 10);

	linkList1.Sort();
	linkList2.Sort();

	std::cout << "第一个链表:" << std::endl;
	linkList1.Display();
	std::cout << "第二个链表:" << std::endl;
	linkList2.Display();

	LinkList<int> linkList3 = MergeListInOrder(linkList1, linkList2);
	std::cout << "第三个链表:" << std::endl;
	linkList3.Display();

	return 0;
}

int* RandomDataGenerator(int* a, int length)
{
     
	std::srand((unsigned int)time(0));
	for (int i = 0; i < length; i++)
	{
     
		a[i] = rand() % 101;
	}

	return a;
}
3.
// main.cpp
#include 
#include
using namespace std;

// 得到运算符优先级
int GetPriority(char c)
{
     
    if (c == '^')
        return 3;
    else if (c == '*' || c == '/')
        return 2;
    else if (c == '+' || c == '-')
        return 1;
    else
        return -1;
}

void InfixToPostfix(string s)
{
     
    std::stack<char> st; // 运算符栈
    st.push('N'); // 表示空
    int l = s.length();
    string ns; // 输出用的字符串

    for (int i = 0; i < l; i++)
    {
     
        // 如果扫描到的是字符,则直接输出
        if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9'))
            ns += s[i];
        // 如果扫描到的是左括号,压入栈中
        else if (s[i] == '(')
            st.push('(');
        // 如果扫描到的是右括号,则将栈中运算符全部出栈并输出,直到遇到左括号
        else if (s[i] == ')')
        {
     
            while (st.top() != 'N' && st.top() != '(')
            {
     
                char c = st.top();
                st.pop();
                ns += c;
            }
            // 最后将左括号出栈
            if (st.top() == '(')
            {
     
                char c = st.top();
                st.pop();
            }
        }
        // 如果扫描到一个运算符
        else {
     
            // 如果栈不为空且该运算符比栈顶运算符的优先级小则将栈顶运算符出栈并输出
            while (st.top() != 'N' && GetPriority(s[i]) <= GetPriority(st.top()))
            {
     
                char c = st.top();
                st.pop();
                ns += c;
            }
            // 最后将该运算符入栈
            st.push(s[i]);
        }
    }

    // 扫描完成后,将所有还在栈中的运算符输出
    while (st.top() != 'N')
    {
     
        char c = st.top();
        st.pop();
        ns += c;
    }
    cout << ns << endl;
}

int main()
{
     
    string exp;
    std::cin >> exp;

    InfixToPostfix(exp);
    return 0;
}

你可能感兴趣的:(课程实验,数据结构)