Building and operation of the stack structure in C + + the detailed parsing _c language

Source: Internet
Author: User
Tags data structures error handling

What is the stack structure

The stack structure is classified from the operation of the data, that is, the stack structure has special operation rules, namely: LIFO first out.

We can think of the stack as a large warehouse, the goods placed at the front of the warehouse (top) will be first taken out, and then take out the goods inside.

From the logical structure of data, the start of stack structure is a kind of linear structure.

If further partitioning from the storage structure of the data, the stack structure consists of two categories:
Sequential stack structure:

Save the data in the stack sequentially, even with a set of contiguous memory units. In a program, you can define a structure array of the specified size as the stack, the element with ordinal 0 is the stack low, and then define a variable top to save the number of the stack.
Chain-type Stack structure:

Save the values of each element of the stack in the form of a linked list. The list header (the element to which the head pointer is pointing) is the top of the stack, and the tail of the list (pointing to the address is null) is the bottom of the stack.

In the stack structure can only operate at one end, the operation is called the top of the stack, the other end is called the bottom of the stack. That is, the data saved and removed can only be done from one end of the stack structure. From the point of view of data operation, the stack structure is based on the principle of "LIFO" to deal with node data.

In the stack structure, only the stack top element is accessible, and the data operation of the stack structure is very simple. The basic operation of the general stack structure is only two:

into Stack (Push): An operation that saves data to the top of the stack. Before the stack operation, modify the top of the stack pointer so that it moves up one element position, and then save the data to the position indicated by the top of the stack pointer.

out Stack (POP): The operation that pops the top data out of the stack. By modifying the top pointer of the stack, it points to the next element in the stack.

Next, we use the C + + language to build the sequential stack and complete the basic operation of the sequential stack structure.
Preparing data

Prepare variables and data structures that need to be used in the stack operation.

Copy Code code as follows:

#define MAXLEN 50
struct DATA
{
String name;
int age;
};
struct Stacktype
{
DATA data[maxlen+1];
int top;
};


The length of the stack structure is defined, the data element type of the stack structure is maxlen, and the structure of the stack is stacktype. In the data structure stacktype, it is an element, and top is the ordinal of the stack. When Top=0, the stack is empty, and when Top=maxlen, the stack is full.

The array elements are all started with subscript 0, here for the sake of speaking and understanding, we start to record data nodes from Subscript 1, where the subscript 0 is not.
Initialize stack structure

Before using the stack structure, you first need to create an empty sequence stack, which is the initialization sequence stack. The initialization of the sequence stack is as follows:

(1) According to the symbol constant MaxLen specified size request a piece of memory space, used to save the data in the stack

(2) Set the top of the stack pointer value of 0, representing an empty stack.

The sample code is as follows:

Copy Code code as follows:

Stacktype *stinit ()
{
Stacktype *p;
if (p=new stacktype)//Application stack space
{
p->top=0; Set stack top to 0
return p; Back to top of stack pointer
}
return NULL;
}

First request memory with new, then set the top of the stack to 0, and then return the first address of the application memory, the request failed to return null;

Judging empty stacks

To determine whether the stack structure is empty, if it is an empty stack, it means that there is no data in the stack structure, at this time can be used in the stack operation, but can not carry out the stack operation.

The sample code is as follows:

Copy Code code as follows:

int Stisempty (Stacktype *s)
{
int t;
T= (s->top==0); Judging by the value of the top of the stack
return t;
}

The input parameter s is a pointer to the operation's stack. Judge whether the stack is empty according to the top pointer of the stack to determine if it is 0.

Judge Full stack

Determine if the stack structure is full. If the stack is full, it means there is no extra space in the stack structure to hold the extra data. The stack operation is not available at this time, but the stack operation can be carried out.

The sample code is as follows:

Copy Code code as follows:

int Stisfull (Stacktype *s)
{
int t;
T= (S->top==maxlen);
return t;
}

The input parameter s is a pointer to the operation's stack. Determine whether the stack is full, based on the top pointer of the stack to determine if it is equal to the maxlen.

Empty stack

The empty stack is where all the data in the stack is cleared. The sample code is as follows:

Copy Code code as follows:

void Stclear (Stacktype *s)
{
s->top=0;
}

Setting the top of the stack pointer to 0 indicates that the empty stack operation is performed. (This only logically empties the data in the stack, actually simply sets the top to 0, and then adds the data later to overwrite the original data)

Free space

Free space is the memory unit that frees up the stack structure and uses Delete to release the memory space requested with the new operator.

The sample code is as follows:

Copy Code code as follows:

void Stfree (Stacktype *s)
{
Delete S;
}

The delete operator is called directly in the program to free the allocated memory space. This function is typically called when the stack structure is not needed, especially at the end of the program.

Into the stack

The stack (push) is the basic operation of the stack structure, the main operation is to save the data elements to the stack structure. The specific steps for the stack operation are as follows:

(1) First of all, to determine the top of the stack, if the tops is greater than or equal to maxlen, it means overflow, error handling. Otherwise, perform the following actions.

(2) Set top=top+1 (stack top pointer plus 1, point to into stack address)

(3) will be into the stack of U urea save to the top point position.

The sample code is as follows:

Copy Code code as follows:

int Pushst (Stacktype *s,data DATA)
{
if ((s->top+1) >maxlen)
{
cout<< "Stack Overflow" <<endl;
return 0;
}
s->data[++s->top]=data; Push the elements into the stack
return 1;
}

The input parameter s is a pointer to the Operation Stack, and the input parameter data is an element that needs to be in the stack. The program first to determine whether the stack overflow, if the overflow gives a warning, do not go into the stack operation, or modify the top of the stack pointer, that is, first plus 1, and then put the data to the upper now points to the unit.

Out Stack

Out Stack (POP) is the basic operation of the dog, the main operation in contrast to the stack, it is from the top of the stack to eject a data element, the specific steps of the stack operation are as follows:

(1) First of all, the top of the stack is judged, if tops equals 0, then the panic is where the error is handled. Otherwise, perform the following action.

(2) returns the element of the position to which the top of the stack pointer is pointing (actually the returned pointer)

(3) The top of the minus 1, pointing to the next element of the stack, the original stack of elements are ejected.

Copy Code code as follows:

DATA * POPST (Stacktype *s)
{
if (s->top==0)
{
cout<< "Stack is empty, can no longer output!" "<<endl;
Exit (0);
}
Return & (s->data[s->top--]);
}

When there is data in the stack, the function return value is a pointer to the data type.

Read Point structure

The reading point structure is the data that reads the nodes in the stack structure. Because the stack structure can only operate at one end, so the read operation here is actually read the data of the site.

It should be noted that the read node data operation and the stack operation is different. Read node operation is only to display the top node data content, and the stack operation will be the top data pop-up.

The sample code is as follows:

Copy Code code as follows:

DATA *peekst (Stacktype *s)
{
if (s->top==0)
{
cout<< "Stack is empty" <<endl;
Exit (0);
}
Return & (S->data[s->top]);
}

Comparing the sample code of the stack, it is not difficult to find that the read point structure also returns the address of the top node of the stack, but it does not reduce the peak by 1.

Complete example

The following is a complete example of the basic operation of the stack:

Program code:

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;
#define MAXLEN 50
struct DATA
{
String name;
int age;
};
struct Stacktype
{
DATA data[maxlen+1];
int top;
};
/****************** initialization Stack Structure ****************/
Stacktype *stinit ()
{
Stacktype *p;
if (p=new stacktype)//Application stack space
{
p->top=0; Set stack top to 0
return p; Back to top of stack pointer
}
return NULL;
}
/**************** Judge Empty Stack **********************/
int Stisempty (Stacktype *s)
{
int t;
T= (s->top==0); Judging by the value of the top of the stack
return t;
}
/********************** Judgment full Stack ****************/
int Stisfull (Stacktype *s)
{
int t;
T= (S->top==maxlen);
return t;
}
/********************** Empty Stack **********************/
void Stclear (Stacktype *s)
{
s->top=0;
}
/******************** Free Space ********************/
void Stfree (Stacktype *s)
{
Delete S;
}
/********************** into Stack ***********************/
int Pushst (Stacktype *s,data DATA)
{
if ((s->top+1) >maxlen)
{
cout<< "Stack Overflow" <<endl;
return 0;
}
s->data[++s->top]=data; Push the elements into the stack
return 1;
}
/************************ out Stack ***********************/
DATA * POPST (Stacktype *s)
{
if (s->top==0)
{
cout<< "Stack is empty, can no longer output!" "<<endl;
Exit (0);
}
Return & (s->data[s->top--]);
}
/********************** Read Point structure *******************/
DATA *peekst (Stacktype *s)
{
if (s->top==0)
{
cout<< "Stack is empty" <<endl;
Exit (0);
}
Return & (S->data[s->top]);
}
/***************** into the main function **********************/
int main ()
{
Stacktype *stack;
DATA Data,*p_data;
Stack=stinit ();
cout<< "=============== into stack operation: =============" <<endl;
cout<< "Enter name, age for stack operation:" <<endl;
Perform an in-stack operation
while (1)
{
cin>>data.name>>data.age;
if (data.name== "0")
{
Break Exit input when the name and age are 0
}else
{
Pushst (Stack,data);
}

}
P_data=popst (stack);
cout<< "pop-up stack top element" <<endl;
cout<< "Name:" <<p_data->name<< ", Age:" <<p_data->age<<endl;
P_data=peekst (stack);
cout<< "Output stack top element" <<endl;
cout<< "Name:" <<p_data->name<< ", Age:" <<p_data->age<<endl;
cout<< "================ will have all the data out of the stack: =============" <<endl;
while (1)
{
P_data=popst (stack);
cout<< "Name:" <<p_data->name<< ", Age:" <<p_data->age<<endl;
}
Stfree (stack);
return 0;
}


Program Run Interface:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.