Home c++ Implementation of the task using the C++ stack

Implementation of the task using the C++ stack

Author

Date

Category

Task: split stack by 2 stack. In one – even numbers, in the other – odd. Completed with the implementation of the removal stack top view. Tell me where I was wrong.

# include & lt; iostream & gt;
Using Namespace STD;
struct stack {
  int info;
  Stack * Head;
} * b, * t;
STRUCT EVENSTACK {
  int info;
  Evenstack * Head;
} * begin1, * T1;
Struct oddstack {
  int info;
  OddStack * Head;
} * begin2, * T2;
Stack * Push (Stack * P, Int N) {
  Stack * T = New Stack;
  t- & gt; info = n;
  T- & GT; Head = P;
  RETURN T;
}
INT VIEW (Stack * P, Int N) {
  int temp = t- & gt; info;
  p = p- & gt; head;
  DELETE T;
  RETURN TEMP;
}
EVENSTACK * PUSHEVEN (Evenstack * P, Int N) {
  Evenstack * T1 = New Evenstack;
  T1- & gt; info = n;
  T1- & gt; Head = P;
  Return T1;
}
OddStack * Pushodd (oddstack * p, int n) {
  Oddstack * t2 = new oddstack;
  T2- & gt; info = n;
  T2- & gt; Head = P;
  Return T2;
}
Void OddPrint (oddstack * begin2) {
  COUT & LT; & LT; "ODD Stack:";
  Oddstack * r = begin2;
  While (R! = NULL) {Cout & LT; & LT; R- & GT; INFO & LT; & LT; ""; R = R- & GT; Head; }
}
Void EvenPrint (Evenstack * Begin1) {
  COUT & LT; & LT; "Even Stack:";
  Evenstack * R = Begin1;
  While (R! = NULL) {Cout & LT; & LT; R- & GT; INFO & LT; & LT; ""; R = R- & GT; Head; }
}
INT MAIN ()
{
  SETLOCALE (0, "");
  int n = 0, k, i = 0;
  COUT & LT; & LT; "Enter the number of items:";
  CIN & GT; & GT; k;
  COUT & LT; & LT; "Enter the items:" & lt; & lt; endl;
  While (i & lt; k) {
    CIN & GT; & GT; n;
    B = Push (B, N);
    COUT & LT; & LT; "Put in the stack:" & lt; & lt; n & lt; & lt; Endl;
    I ++;
  }
  While (T! = NULL) {
    int x = view (b, n);
    if (x% 2 == 0) {begin2 = pushodd (begin2, x); COUT & LT; & LT; "Put in a well-stack:" & lt; & lt; x & lt; & lt; Endl; }
    else {begin1 = pusheven (begin1, x); COUT & LT; & LT; "Put in the odd stack:" & lt; & lt; x & lt; & lt; Endl;
    };
  }
  EVENPRINT (Begin1);
  ODDPRINT (Begin2);
}

Answer 1, Authority 100%

Your problem is that you are trying to solve the same task three times. Most likely, when writing code, you felt that you repeat the code (perhaps even used Ctrl + C Ctrl + V ). Code Repeat This is an important signal of what you do something wrong and you need to stop and think about solving the problem.


I understand that you want to create two stacks – in one even numbers, and in another odd.
What is the stack?

stack (eng. Stack – Stack; Stack read) – Abstract data type, which is List of elements, organized on the LIFO principle (the latter came – the first left)

In our case, it is important that the stack is an entity that does not depend on the type of elements. (Indeed, the definition does not say what type of elements should be). There is no specific separate stack for strings, a separate stack for integers or a separate stack for even integers. There is just a stack in which elements can be either rows or integers, etc.

You tried to create a separate stack for even numbers and a separate stack for odd. I propose to create a stack, and then (depending on the needs) put everything that is necessary.


We implement just a stack for integers:

# include & lt; iostream & gt;
Using Namespace STD;
// Structure - Stack Element
struct stackitem.
{
  StackItem * Next = nullPTR; // Stores the pointer to the next element
  INT DATA;
};
// Add a new element to the top of the stack
StackItem * Push (StackItem * Top, Int Var)
{
  StackItem * new_top = new stackitem;
  new_top- & gt; data = var;
  new_top- & gt; next = TOP;
  Top = new_top;
  RETURN TOP;
}
// Remove the top of the stack and return the value.
StackItem * Pop (StackItem * TOP)
{
  if (TOP == NULLPTR) RETURN 0;
  AUTO OLD_TOP = TOP;
  top = old_top- & gt; next; // Now the top is the next item
  delete old_top;
  RETURN TOP;
}
Void Print (StackItem * TOP)
{
  AUTO ITER = TOP; 
While (Iter! = nullPTR) // If Iter == NullPTR - reached the end of the stack
  {
    STD :: COUT & LT; & LT; Iter- & gt; Data & lt; & lt; '';
    iTer = iTer- & gt; next; // withdrawn values, go to the next element
  }
  STD :: COUT & LT; & LT; '\ n';
}
INT MAIN ()
{
  Stackitem * top = nullptr;
  Top = Push (TOP, 5);
  Print (TOP);
  TOP = POP (TOP);
  Print (TOP);
}

Stack element – StackItem in HEAs in stores data (int) and pointer to the next element. The basic functions of working with such a stack – PUSH and POP are implemented. I advise you to figure out the code. Now, if necessary, you can create two stacks:

stackItem * oddtop = nullptr, eventtop = nullptr;
oddtop = push (oddtop, 5); // Place odd
Eventtop = Push (Eventtop, 4); // Place even

and odd and even numbers are integer and easily fall into the int. However, our stack still turned out to be dependent on the type of elements (for example, it is impossible to put a string in it).


If you solve the task further, you can move away from binding to the type by adding templates.

template & lt; TypeName T & GT; // announced a template named T
struct stackitem.
{
  StackItem & lt; t & gt; * next = nullptr; // Stores the pointer to the next template element of type stackItem & lt; T & GT;
  T DATA; // Stores the element type T
};
TEMPLATE & LT; TYPENAME T & GT;
StackItem & LT; T & GT; * Push (StackItem & LT; T & GT; * Top, Const T & AMP; var) // Added Push Pure Function
{
  StackItem & lt; t & gt; * new_top = new stackitem & lt; t & gt ;;
  new_top- & gt; data = var;
  new_top- & gt; next = TOP;
  Top = new_top;
  RETURN TOP;
}
StackItem & lt; int & gt; * int_stack; // stack where elements are integer values
StackItem & Lt; Double & GT; * Double_Stack;
StackItem & lt; STD :: String & GT; * String_Stack;
Push (int_stack, 5);
Push (String_Stack, "Hello");

In the above code, we “bought” from the type in stackItem and in the PUSH function. Now they will work for all types (with reservations). During compilation, instance (= substitution) of template methods and classes occurs, i.e. The compiler is looking at what types are used for template methods and generates the required code.
We use the StackItem class for the types of int, double, String – the compiler will create code for three classes and instead of ‘t’ will substitute int, double and string. An extension code for the PUSH function for Int and String types will also be created (code will not be created, TCs, we did not cause the corresponding Push method).

The next iteration will be the creation of a Stack class, which will store the pointer to the top (stackItem) and have Push, POP methods, displaying the screen and TD

Previous articleQuick Sort Descending on C
Next articleCreating a file

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions