Sequential storage and chain code stored _ stack data structures implemented

Sequential storage and chain code stored _ stack data structures implemented

1. Basic concepts stack

  • concept:

    First, it is a linear table, that is, the stack elements have a linear relationship, that relationship predecessor successor. But it is a special linear form only. Is defined in said insert and delete operations table tail linear table, where the table refers to the end of the stack, rather than the bottom of the stack.

  • characteristic

    It's special because this limits the location of insertions and deletions of linear form, it is always only the top of the stack. This also makes: bottom of the stack is fixed, only the most advanced stack bottom of the stack.

  • operating
    •   Insert stack, called the push, but also to push. Similarly bullet into the magazine (as shown below)
    •   Stack delete operation, called a stack, and some called the Dan Stack, de-stacked. As the bullet from the clip magazine (as shown below)

 

    •   Create a stack
    •   Destruction stack
    •   Empty stack
    •   Push
    •   Pop
    •   Get the top element
    •   Gets the size of the stack 

Stack abstract data types

ADT Stack (Stack) 
the Data 
	on a linear table. Elements of the same type, the elements having the adjacent predecessor and successor relationships. 
Operation 
	// initialize, create an empty stack S 
	InitStack (* S); 
	// if stack exists, destroy it 
	DestroyStack (* S); 
	// the stack empty 
	ClearStack (* S); 
	// If the stack is empty return true, otherwise it returns to false 
	StackEmpty (S); 
	// if present and non-empty stack, return stack S with the element e 
	getTop (S, e *); 
	// stack S if present, inserting a new element to the stack S, e as its top element and 
	the Push (S *, E); 
	// remove the top element in the stack S, and returns its value E 
	Pop (S *, E *); 
	// returns the number of elements in the stack S 
	StackLength ( S); 
endADT

2. The code for sequential storage stack

  • basic concept

    Sequential storage structure stack short sequence of stack, which is limited operation sequence table. Sequentially storing stack structure: using a set of consecutive addresses are sequentially stored from the memory cell stack bottom to top of the stack of data elements , only while attached to top pointer position in the top element in the sequence table.

  • Design and Implementation

    Because the stack is a special linear form, so that the stack may be achieved by sequentially stored in the order of linear form.

SeqStack.h  

SEQSTACK_H #ifndef 
#define SEQSTACK_H 

#include <stdlib.h> 
#include <stdio.h> 

// stack array to simulate sequentially stored 
#define MAX_SIZE 1024 
#define SEQSTACK_TRUE. 1 
#define SEQSTACK_FALSE 0 

typedef struct {SEQSTACK 
	void * Data [MAX_SIZE ]; 
	int size; 
} SeqStack; 

// initialize stack 
SeqStack Init_SeqStack * (); 
// stack 
void Push_SeqStack (SeqStack stack *, void * Data); 
// returns the top element 
void * Top_SeqStack (SeqStack stack *); 
/ / pop 
void Pop_SeqStack (SeqStack stack *); 
// determines whether the air 
int the IsEmpty (SeqStack stack *); 
// returns the number of elements in the stack 
int Size_SeqStack (SeqStack stack *); 
// Clear stack 
void Clear_SeqStack (SeqStack stack *);
//销毁
void FreeSpace_SeqStack(SeqStack* stack);

#endif

SeqStack.c  

#include"SeqStack.h"

//初始化栈
SeqStack* Init_SeqStack() {
	SeqStack* stack = (SeqStack*)malloc(sizeof(SeqStack));
	for (int i = 0; i < MAX_SIZE; i++) {
		stack->data[i] = NULL;
	}
	stack->size = 0;

	return stack;
}
//入栈
void Push_SeqStack(SeqStack* stack, void* data) {
	if (stack == NULL) {
		return;
	}
	if (stack->size == MAX_SIZE) {
		return;
	}
	if (data == NULL) {
		return;
	}

	stack->data[stack->size] = data;
	stack->size++;
}
//返回栈顶元素
void* Top_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return NULL;
	}

	if (stack->size == 0) {
		return NULL;
	}

	return stack->data[stack->size - 1];
}
//出栈
void Pop_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return;
	}
	if (stack->size == 0) {
		return;
	}
	stack->data[stack->size - 1] = NULL;
	stack->size--;
}
//判断是否为空
int IsEmpty(SeqStack* stack) {
	if (stack == NULL) {
		return -1;
	}

	if (stack->size == 0) {
		return SEQSTACK_TRUE;
	}

	return SEQSTACK_FALSE;
}
//返回栈中元素的个数
int Size_SeqStack(SeqStack* stack) {
	return stack->size;
}
//清空栈
void Clear_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return;
	}
	for (int i = 0; i < stack->size; i++) {
		stack->data[i] = NULL;
	}
	stack->size = 0;
}
//销毁
void FreeSpace_SeqStack(SeqStack* stack) {
	if (stack == NULL) {
		return;
	}

	free(stack);
}

Sequential storage stack .c  

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SeqStack.h"

typedef struct PERSON {
	char name[64];
	int age;
}Person;

int main(void) {
	//创建栈
	SeqStack* stack = Init_SeqStack();

	//创建数据
	Person p1 = { "aaa", 10 };
	Person p2 = { "bbb", 20 };
	Person p3 = { "ccc", 30 };
	Person p4 = { "ddd", 40 };
	Person p5 = { "eee", 50 };

	//入栈
	Push_SeqStack(stack, &p1);
	Push_SeqStack(stack, &p2);
	Push_SeqStack(stack, &p3);
	Push_SeqStack(stack, &p4);
	// Output
	Push_SeqStack (Stack, & P5);

	the while (Size_SeqStack (Stack)> 0) { 
		// access the top element 
		the Person Person * = (the Person *) Top_SeqStack (Stack); 
		the printf ( "the Name: Age% S:% D \ n-", person-> name, Person -> Age); 
		// pop the top element 
		Pop_SeqStack (Stack); 
	} 

	// release memory 
	FreeSpace_SeqStack (Stack); 

	System ( "PAUSE"); 
	return 0; 
}

3. Stack chain store  

  • basic concept

    Storage Structure abbreviation stack link stack.

    Think about the following questions :

         Just do stack stack inserts and deletes, top of the stack on the head or tail of the list it?

    A: Because there are singly linked list head pointer, stack pointer is necessary, then why not let They combined it, so better way is to stack on the head of a single list. In addition already have top of the stack in the head, and the list of the more common single head node loses its meaning, usually for chain stack, it does not require the head node.

  • Design and Implementation

    Link stack is a special linear table, link stack may be achieved by a linear chain table.

 LinkStack.h

LINKSTACK_H #ifndef 
#define LINKSTACK_H 

#include <stdlib.h> 
#include <stdio.h> 

// stack node chain 
typedef struct {LINKNODE 
	struct * Next LINKNODE; 
} LinkNode; 

// stack chain 
typedef struct {LINKSTACK 
	LinkNode head; 
	int size; 
} LinkStack; 

// initialization function 
LinkStack Init_LinkStack * (); 
// stack 
void Push_LinkStack (LinkStack stack *, * Data LinkNode); 
// popped 
void Pop_LinkStack (LinkStack stack *); 
// return stack top element 
LinkNode Top_LinkStack * (* LinkStack stack); 
// returns the number of elements in the stack 
int Size_LinkStack (LinkStack stack *); 
// stack empty 
void Clear_LinkStack (LinkStack stack *); 
// stack destruction
void FreeSpace_LinkStack(LinkStack* stack);

#endif

LinkStack.c  

#include"LinkStack.h"
//初始化函数
LinkStack* Init_LinkStack() {
	LinkStack* stack = (LinkStack*)malloc(sizeof(LinkStack));
	stack->head.next = NULL;
	stack->size = 0;

	return stack;
}
//入栈
void Push_LinkStack(LinkStack* stack, LinkNode* data) {
	if (stack == NULL) {
		return;
	}

	if (data == NULL) {
		return;
	}

	data->next = stack->head.next;
	stack->head.next = data;
	stack->size++;
}
//出栈
void Pop_LinkStack(LinkStack* stack) {
	if (stack == NULL) {
		return;
	}

	if (stack->size == 0) {
		return;
	} 

	// first valid node 
	LinkNode * = stack- pNext> head.next; 
	stack-> head.next = pNext-> Next; 

	stack-> size--; 
} 
// Returns the top element 
LinkNode * Top_LinkStack ( stack * LinkStack) { 
	IF (stack == NULL) { 
		return NULL; 
	} 
	IF (stack-> size == 0) { 
		return NULL; 
	} 
	return stack-> head.next; 
} 
// returns the number of elements in the stack 
int Size_LinkStack (LinkStack stack *) { 
	IF (stack == NULL) { 
		return -1; 
	} 
	return stack-> size; 
} 
// stack empty 
void Clear_LinkStack (LinkStack stack *) { 
	IF (stack == NULL) { 
		return; 
	} 
	stack- > head.next = NULL;
	stack->size = 0;
}
//销毁栈
void FreeSpace_LinkStack(LinkStack* stack) {
	if (stack == NULL) {
		return;
	}
	free(stack);
}

Stack of linked storage .c  

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "LinkStack.h"

typedef struct PERSON {
	LinkNode node;
	char name[64];
	int age;
}Person;

int main(void) {
	//创建栈
	LinkStack* stack = Init_LinkStack();

	//创建数据
	Person p1, p2, p3, p4, p5;
	strcpy(p1.name, "aaa");
	strcpy(p2.name, "bbb");
	strcpy(p3.name, "ccc");
	strcpy(p4.name, "ddd");
	strcpy(p5.name, "eee");

	p1.age = 10;
	p2.age = 20;
	p3.age = 30;
	p4.age = 40;
	p5.age = 50;

	// stack
	Push_LinkStack (Stack, (LinkNode *) & P1);
	Push_LinkStack (Stack, (LinkNode *) & P2); 
	Push_LinkStack (Stack, (LinkNode *) & P3); 
	Push_LinkStack (Stack, (LinkNode *) & P4); 
	Push_LinkStack (Stack, (LinkNode *) & P5); 

	// output of 
	the while (Size_LinkStack (Stack)> 0) { 
		// remove the top element 
		the Person * P = (the Person *) Top_LinkStack (Stack); 
		the printf ( "the name: Age% S:% D \ n-", p-> name, p-> Age); 
		// pop the top element 
		Pop_LinkStack (stack); 
	} 

	// stack destruction 
	FreeSpace_LinkStack (stack); 

	System ( "PAUSE"); 
	return 0; 
}

4. Stack the application (Case)

4.1 Case 1: the nearest match

Almost all compilers have the ability to detect whether the brackets match, then how to achieve symbol pair detect compiler? The following character string:

#include <stdio.h> int main() { int a[4][4]; int (*p)[4]; p = a[0]; return 0;}
  • Algorithm thinking
    •  From the first character to start scanning
    •  When met ordinary characters are ignored,
    •  When pushed on the stack symbol meet left
    •  When the right met symbols from the symbol stack pop stack, and matching
    •  Matching Success: Continue to read the next character
    •  Match failed: stop immediately and report an error
    •  End:
    •  Success: All characters have been scanned, and the stack is empty
    •  Failure: failure to match or all of the characters have been scanned, but the stack is not empty
  • to sum up
    •  When things appear to be detected in pairs adjacent to each other but can use the stack "last in, first out" feature
    •  Stack is ideally suited for "near match" situation

Case Code:

#include "LinkStack.h" 

// Case nearest matching a 
void Test02 () { 

	char * STR = "#include <stdio.h> int main () {int A [. 4] [. 4]; int (* P) [ . 4]; P = A [0]; return 0;} "; 
	// initialize stack 
	LinkStack * lstack InitLinkStack = (); 
	// matching bracket 
	char * = pCurrent STR; 
	the while (* pCurrent = '\ 0'!) { 
		IF (pCurrent == * '(') { 
			PushLinkStack (lstack, pCurrent); 
		} 
		the else IF (* pCurrent == ')') { 
			char * P = (char *) TopLinkStack (lstack); 
			IF (P == * '(') { 
				PopLinkStack (lstack); 
			} 
		} 
		pCurrent ++; 
	} 
	IF (GetLengthLinkStack (lstack)> 0) { 
		the printf ( "fails to match \ n!");
	} 
	// destroy the stack
	DestroyLinkStack(lstack);
}

int main(){

	test02();

	system("pause");
	return EXIT_SUCCESS;

4.2 Case 2 : infix and postfix expression

  • l postfix expression (proposed by Polish scientists in the 1950s)
    •  The numbers on the back of the operator === "compliance of computing
    •  We are accustomed to mathematical expression is called infix expression === "consistent with human thinking habits
  • Examples l
    •  5 + 4 => 5 4 + 
    •  1 + 2 * 3 => 1 2 3 * + 
    •  8 + ( 3 – 1 ) * 5 => 8 3 1 – 5 * +
  • l infix suffix transfer algorithm:

  Traversal infix expressions of numbers and symbols:

    •  For digital: Direct output
    •   For Symbol:
      •    Left parenthesis: push 
      •    Operational sign: prioritizing symbols compared with the top of the stack
    •   If the low-priority stack symbol: This symbol onto the stack (the default stack if left parenthesis, left bracket lowest priority)
    •   If the stack is not low priority symbols: the symbol stack and pop outputs, then push
  • l right parenthesis: pop the stack and output symbols, until the matching left parenthesis

  Traversal End: all symbols will pop the stack and output

  • l infix suffix pseudocode priority transfected
Transform (exp) 
{ 
	create a stack S; 
	I = 0; 
	the while (exp [I] = '\ 0'!) 
{ 
		IF (exp [I] is a number) 
		{ 
			the Output (exp [I]); 
		} 
		the else IF (exp [i] symbol) 
		{ 
			the while (exp [i] priority <= symbol stack priority) 
			{ 
				Output (top of the stack symbols); 
				Pop (S); 
			} 
			the Push (S, exp [i]); 
		} 
		the else IF (exp [i] to the left bracket) 
		{ 
			the Push (S, exp [I]); 
		} 
		the else IF (exp [i] is the right parenthesis) 
		{ 
			the while (top of the stack is not left bracket symbol) 
			{ 
				Output (symbol stack) ; 
				pop (S); 
			} 
			eject S from the left bracket; 
		} 
		the else 
		{  
			error, stop the loop;
		}
		i++;
}
while(size(S) > 0 && exp[i] == ‘\0’)
{
		output(栈顶符号);
		Pop(S);
}
}
  • l hands-on exercises

  Convert infix expression of our love of reading into a computer like the suffixes expression

  Infix: 8 + (3 - 1) * 5

       Postfix expression: 831--5 + *

4.3 Case 3: How computer calculated based on postfix expression

  • l think

    The computer is based on how to calculate postfix expression?

    For example: 831--5 + *

  • l calculation rules

    Traversal postfix expression of numbers and symbols

    •   For digital: push
    •   For Symbol:
      •    Pop right operand from the stack
      •    The left operand popped from the stack
      •    Calculates according to the symbol
      •    The calculation result onto the stack

    Traversing the end: a unique number in the stack as the calculation results

  • l realize Code (pseudo code) Express
Compute (exp) 
{ 
	create a stack; 
	int I = 0; 
	the while (! exp [I] = '\ 0') 
	{ 
		IF (exp [I] is a number) 
		{ 
			the Push (S, exp [I]); 
		} 
		the else IF (exp [i] symbol) 
		{ 
1. right operand from the stack pop; 
2 eject from the left operand stack; 
3. the sign operation; 
4. the Push (stack, result); 
		} 
		the else 
		{ 
			error, stop cycles; 
		} 
		I ++; 
	} 
	IF (Size (S). 1 && == exp [I] == '\ 0') 
	{ 
		stack is a unique number calculation result; 
	} 
	return result; 
}

  

Guess you like

Origin www.cnblogs.com/wanghui1234/p/11315567.html
Recommended