Инициализация, добавление и удаление элементов в СТЕКЕ через связный список - C (СИ)
Формулировка задачи:
День добрый,
нужна помощь со стеками.
Передо мною стоит задача:
1) Инициализировать СТЕК через связный список
2) Добавить элемент в СТЕК, вывести на экран СТЕК
3) Удалить элемент со СТЕКА, вывести на экран СТЕК
Мне кажется, что я создал не совсем СТЕК, а СТЕК с элементами ОЧЕРЕДИ, звучит как бред но... в общем я запутался.
Вот мой код:
Буду благодарен за помощь.
// Stack (Test).cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int del = 3; //number of elements that we will delete
struct STACK
{
float Value;
STACK *Link;
};
struct Using_Stack
{
STACK *Top, *Bot;
void (*Init)(Using_Stack*);
void (*Push)(Using_Stack*, float );
float (*Pop)(Using_Stack*);
};
void Init_Sub (Using_Stack *MyStack)
{
MyStack->Top = 0;
MyStack->Bot = 0;
};
void Push_Sub (Using_Stack *MyStack, float Character)
{
STACK *New_El = new STACK;
New_El->Value = Character;
New_El->Link = 0;
if (MyStack->Top == 0)
{
MyStack->Top = New_El;
MyStack->Bot = New_El;
}
else
{
MyStack->Bot->Link = New_El;
MyStack->Bot = New_El;
}
};
float Pop_Sub (Using_Stack *MyStack)
{
STACK *Del;
float Reading;
if (MyStack->Top != 0)
{
Del = MyStack->Top;
Reading = Del->Value;
MyStack->Top = MyStack->Top->Link;
delete Del;
}
return Reading;
};
void main ()
{
Using_Stack *MyStack;
MyStack = new Using_Stack;
MyStack->Init = Init_Sub;
MyStack->Push = Push_Sub;
MyStack->Pop = Pop_Sub;
MyStack->Init (MyStack); //Initialization of Top and Bot
int Add;
float Symb;
int i = 0;
MyStack->Push (MyStack, 10); //Recording of one value
MyStack->Push (MyStack, 20);
printf("Our Stack is...");
printf("\n\n %c", MyStack->Top->Value);
printf("\n %c", MyStack->Top->Link->Value);
printf("\n\n Enter the value you want to ADD \n");
scanf_s("%c", &Symb);
MyStack->Push (MyStack,Symb);
printf("\n\n So, now our Stack is");
printf("\n\n %c", MyStack->Top->Value);
printf("\n %c", MyStack->Top->Link->Value);
printf("\n %c \n\n", MyStack->Top->Link->Link->Value); //???
printf("\n And now we will DELETE one value \n\n\n");
float Read;
Read = MyStack->Pop (MyStack); //Reading of one value
printf("\n Value which we will delete is - ");
printf("%c \n\n", Read);
printf("And now, our Stack is");
printf("\n\n %c", MyStack->Top->Value);
printf("\n %c \n\n\n", MyStack->Top->Link->Value);
}Решение задачи: «Инициализация, добавление и удаление элементов в СТЕКЕ через связный список»
textual
Листинг программы
#include <cstddef>
#include <iterator>
#include <stdexcept>
template <class T>
class Stack
{
public:
Stack();
template <class InputIterator>
Stack(InputIterator beg, InputIterator end);
Stack(const Stack &);
Stack& operator= (const Stack &);
~Stack();
bool empty() const;
void push(const T& obj);
void pop();
std::size_t size() const;
const T& top() const;
T& top();
private:
T& getTopData() const;
void swap(const Stack &) throw();
struct Node
{
T data_;
Node *next_;
Node(const T &data, Node *nextNode);
Node(const Stack::Node &); // prohibit copy
Node& operator= (const Stack::Node &); // and assignment
} *Top;
std::size_t counter_;
};
template <class T>
Stack<T>::Stack() : Top(nullptr), counter_(0) {}
template <class T>
template <class InputIterator>
Stack<T>::Stack(InputIterator beg, InputIterator end) : Stack()
{
while(beg != end)
{
push(*beg);
++beg;
}
}
template <class T>
Stack<T>::Stack(const Stack &s) : Stack()
{
Stack tmp;
for (Node *n = s.Top; n; n = n->next_)
tmp.push(n->data_);
for (Node *n = tmp.Top; n; n = n->next_)
this->push(n->data_);
}
template <class T>
Stack<T>& Stack<T>::operator= (const Stack &s)
{
if(this != &s)
Stack(s).swap(*this);
return *this;
}
template <class T>
Stack<T>::~Stack()
{
while(Top)
pop();
}
template <class T>
bool Stack<T>::empty() const
{
return Top == nullptr;
}
template <class T>
void Stack<T>::push(const T& obj)
{
Top = new Node(obj, Top);
counter_++;
}
template <class T>
void Stack<T>::pop()
{
if (!Top)
return;
Node *tmp = Top;
Top = Top->next_;
delete tmp;
counter_--;
}
template <class T>
std::size_t Stack<T>::size() const
{
return counter_;
}
template <class T>
const T& Stack<T>::top() const
{
return getTopData();
}
template <class T>
T& Stack<T>::top()
{
return getTopData();
}
template <class T>
T& Stack<T>::getTopData() const
{
try
{
if (!Top) throw std::out_of_range("Trying to access to nothing");
return Top->data_;
}
catch (const std::exception &e)
{
std::cerr << e.what() << std::endl;
}
}
template <class T>
Stack<T>::Node::Node(const T &data, Node *next) : data_(data), next_(next) {}