Data structure - stack 03

Concept:

Stack is a linear table (commonly known as stack) that is limited to insert and delete operations at one end. One end that allows operations is called "top of stack", the other fixed end is called "bottom of stack". When there is no element in the stack, it is called "empty stack". Features: LIFO.

Sequential stack

It is a kind of sequential table, which has the same storage structure as the sequential table. It is defined by array and performs various operations with top (relative pointer) of stack top represented by array subscript.

The definition and implementation of stack
typedef int datatype;

typedef struct node
{
	datatype *data;/*Define the data type of data elements in the stack*/
	int maxlen;/*Maximum number of elements in the current stack*/
	int top;/*Variable indicating the top of stack position (array subscript)*/
}SeqStack;

Create stack

SeqStack* Stack_create(int len)
{
	SeqStack *s;

	s = (SeqStack*)malloc(sizeof(SeqStack));

	if(s == NULL)
	{
		puts("malloc failed");
		return NULL;
	}
	
	s->data = (datatype*)malloc(sizeof(len*sizeof(datatype)))
	{
		puts("malloc failed");
		return NULL;
	}

	s->maxlen = len;
	s->top = -1;

	return s;
}

Emptying stack

void Stack_clear(SeqStack* s)
{
	s->top = -1;
}

Judge whether the stack is empty

int Stack_empty(SeqStack* s)
{
	return (s->top == -1 ? 1 : 0);
}

Enter stack

int Stack_push(SeqStack* s,datatype value)
{
	if(s->top == s->maxlen-1)
	{
		puts("stack full");
		return -1;
	}

	s->data = value;
	p->next = s->next;
	s->next = p;

	return 0
	
}

Stack out

datatype Stack_pop(SeqStack* s)
{
	s->top--;

	return (s->data[s->top+1]);
}

Top of stack element

datatype Stack_top(SeqStack* s)
{
	return (s->data[s->top]);
}

Destruction stack

void Stack_free(SeqStack *s)
{
	free(s->data);
	s->data = NULL;
	free(s);
	s = NULL;
}


It is a kind of sequential table, which has the same storage structure as the sequential table. It is defined by array and performs various operations with top (relative pointer) of stack top represented by array subscript.
typedef int datatype; / defines the data type of data elements in the stack/
typedef struct node
{
datatype data; / data field/
struct node* next; / * link pointer field*/
}LinkNode_T, *LinkList;

LinkList stack_create()
{
	LinkList s;

	s = (LinkList)malloc(sizeof(LinkNode_T));

	if(NULL == s)
	{
		puts(" malloc faliled" );
		return NULL;
	}

	s->data = 0;
	s->next = NULL;

	return s;


}

int stack_empty(LinkList s)
{
	return ( s->next == NULL ? 1 : 0 );
}

int stack_push(LinkList s,datatype value)
{
	LinkList p  =(LinkList)malloc(sizeof(LinkNode_T));

	if(NULL == p)
	{
		puts("malloc faliled ");
		return ERROR;
	}

	p->data = value;
	p->next = s->next;
	s->next = p;

	return SUCCESS;
}

datatype stack_pop(LinkList s)
{
	LinkList p;
	datatype ret;

	p = s->next;
	s->next = p->next;
	ret = p->data;
	free(p);
	p = NULL;
	
	return ret;

}


void stack_clear(LinkList s)
{
LinkList p;

	p = s->next;

	while(p)
	{
		s->next= p->next;
		printf("%d", p->data);
		free(p);
		p = s->next;
	}

}

datatype stack_top(LinkList s)
{
	return (s->next->data);
}

  void stack_free(LinkList s)
    {
    	LinkList p;
    
    	p = s;
    
    	while(p)
    	{
    		s = s->next;
    		printf("%d ",p->data);
    		free(p);
    		p = s;
    	}
    	puts("");
    }

Posted by nutt318 on Tue, 19 Nov 2019 10:27:27 -0800