Stacks and queues are linear data structures, from the perspective of data structures, stacks and queues are two special linear tables. Stack - "Back", Queue - "Advanced Surprises". They are a limited linear table.
The concept and operating stack of the stack are linear tables that define only one end of the table to insert and delete the calculation. One end of the insertion and deletion is allowed to be called a stack. One end of the insertion and deletion is not allowed to be called the bottom (Bottom). There is no element in the table, referred to as empty stack.
Stack storage structure and operation implementation
Similar to the linear form, the stack has two storage representations. The stack represented by the sequential storage structure is called a sequence table, and the stack indicated by the chain storage structure is called a chain stack.
Sequential stack #define maxsiztypedPedef struct {datatype data [maxsize]; int top; int stcksize;} seqstack; seqstack * s
1, set the air stack vidinitstract (SEQSTACK * S) {s.data = (DataType *) Malloc (MaxSize * size)); S-> TOP = -1; S-> stacksize = maxsize;} 2, judgment Empty stack int EMPTYSTACK (SEQSTACK * S,) {IF (S-> TOP> 0) Return 0; Else Return 1;} 3, in the stack VOID PUSH (SEQSTACK * S, DATATYPE X) {IF (S-> TOP = = MaxSize-1) Printf ("overflow / n"); Else {S-> Top ; S-> Data [S-> Top] = x;}} 4, out of the stack VOID POP (SEQSTACK * S, DATATYPE & X) {IF (EmptyStack (S)) Printf ("Underflow Stack IS Empty / N); Else {x = S-> Data [S-> Top]; S-> Top -;}} 5, take the top elements Void gettop (seqstack s, datatype & x) {if (EmptyStack (s)) Printf ("stack is empty / n"); else {x = S-> Data [S-> TOP];}}
Link stack typedef struct stacknode {dataype data; struct stacknode * next;} linkstack; linkstack * top; 1, link stack initialization void INITSTACK (linkstack * top) {top = NULL;} 2, sentence empty stack int EMPTYSTACK (linkstack * top ) {IF (top! = Null) return 0; Else Return 1;} 3, in the stack VOID PUSH (LinkStack * Top, DataType X) {linkstack * p; p = malloc (slinkstack); P-> Data = x; p-> next = null; TOP-> Next = P; TOP = P;} 4, out of the stack VOID POP (Linkstack * Top, DataType & X) {linkstack * p; if (EmptyStack) Printf "Underflow / n"); else {p = top; x = TOP-> data; TOP = TOP-> next; free (p);}} 5, taking the top elements Void gettop (Linkstack * Top, DataType & X) {IF ("Underflow, Stack IS Empty / N); Else X = TOP-> DATA;} Queue's concept and operation queue (Queue) is limited to only insert in one end of the table, And can only be deleted on the other end. One end that allows the deletion of the deletion is called a team header; the one end queue (REAR) is allowed.
Sequential queue #define max 100typedef struct {datatype data [max]; int around; IntR;} sequeue; sequeue * sq; Since the cycle queue can solve this problem.
Cycle queue operation 1, initialization void initqueue (sequeue * sq, int max = 100) {sq-> data = (Datatype *) Malloc ((MAX 1) * sizeof (datatype)); sq-> front = 0; SQ-> Rear = 0;} 2, judging INT EMPTYQUE (S-> Front == S-> REAR) RETURN 1; Else Return 0;} 3, enter the team void enqueue (sequeue * SQ, DATATYPE X) {IF ((S-> Rear 1)% Max == SQ-> Front) Printf ("overflow"); Else {SQ-> Data [SQ-> REAR] = X; SQ-> Rear ;}} 4, out of the queueueueue (sequeue * sq, datatype & x) {if (Emptyqueue) Printf ("underflow"); else {x = sq-> data [sq-> front]; sq-> front- -;}} 5, taking a team head element void getfront (sequeue * SQ, DATATYPE & X) {if ("underflow queue is empty"); else {x = SQ-> DataP [SQ-> Front]; }
Chain queue typedef struct queuenode {datatype data; struct queuenode * next;} queuenode; typedef struct linkqueue {queuenode * front, * rear;} linkqueue; linkqueue * sq; 1, blanking queue void INITQUEUE (linkqueue * sq) {sq- > front = malloc (SizeOf (queuenode)); sq-> rest = sq-> front = null;} 2 Return 1; else return 0;} 3 ; SQ-> Rear-> next = p; sq-> rest = p;} 4 Else {P = SQ-> front; x = p-> data; sq-> front = p-> next; free (p);}} 5, taking the team void getfront (linkqueue * sq, datatype & x) { LinkQueue * p; if (EmptyQueue) Printf ("underflow / n"); else {x = SQ-> front-> data;}}