The sequential instance Seqstack implementation of the stack

Source: Internet
Author: User

1. #include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"

/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */

int main (int argc, char *argv[])
{
seqstack* stack = seqstack_create (20);
int a[10];
int i = 0;

for (i=0; i<10; i++)
{
A[i] = i;

Seqstack_push (Stack, a + i);
}

printf ("Top:%d\n", * (int*) seqstack_top (stack));
printf ("Capacity:%d\n", seqstack_capacity (stack));
printf ("Length:%d\n", seqstack_size (stack));

while (seqstack_size (stack) > 0)
{
printf ("Pop:%d\n", * (int*) Seqstack_pop (stack));
}

Seqstack_destroy (stack);

return 0;
}

2. #include <stdio.h>
#include <malloc.h>
#include "SeqList.h"

typedef unsigned int tseqlistnode;

typedef struct _TAG_SEQLIST
{
int capacity;
int length;
tseqlistnode* node;
} tseqlist;
Creating stacks
seqlist* seqlist_create (int capacity)//O (1)
{
tseqlist* ret = NULL;

if (capacity >= 0)
{
ret = (tseqlist*) malloc (sizeof (tseqlist) + sizeof (tseqlistnode) * capacity);
}

if (ret! = NULL)
{
Ret->capacity = capacity;
ret->length = 0;
Ret->node = (tseqlistnode*) (ret + 1);
}

return ret;
}
Destroying stacks
void Seqlist_destroy (seqlist* list)//O (1)
{
Free (list);
}
Clear Stack
void Seqlist_clear (seqlist* list)//O (1)
{
tseqlist* sList = (tseqlist*) list;

if (sList! = NULL)
{
slist->length = 0;
}
}
Remove stack length
int seqlist_length (seqlist* list)//O (1)
{
tseqlist* sList = (tseqlist*) list;
int ret =-1;

if (sList! = NULL)
{
RET = slist->length;
}

return ret;
}

Take out the memory space of the stack
int seqlist_capacity (seqlist* list)//O (1)
{
tseqlist* sList = (tseqlist*) list;
int ret =-1;

if (sList! = NULL)
{
RET = slist->capacity;
}

return ret;
}
Press Stack
int Seqlist_insert (seqlist* list, seqlistnode* node, int pos)//O (n)
{
tseqlist* sList = (tseqlist*) list;
int ret = (sList! = NULL);
int i = 0;

RET = ret && (slist->length + 1 <= slist->capacity);
RET = ret && (0 <= POS);

if (ret)
{
if (POS >= slist->length)
{
pos = slist->length;
}

For (i=slist->length; i>pos; i--)
{
Slist->node[i] = slist->node[i-1];
}

Slist->node[i] = (tseqlistnode) node;

slist->length++;
}

return ret;
}
Remove the stack point element
seqlistnode* seqlist_get (seqlist* list, int pos)//O (1)
{
tseqlist* sList = (tseqlist*) list;
seqlistnode* ret = NULL;

if ((sList! = NULL) && (0 <= Pos) && (POS < slist->length))
{
ret = (seqlistnode*) (Slist->node[pos]);
}

return ret;
}
Out of the stack
seqlistnode* seqlist_delete (seqlist* list, int pos)//O (n)
{
tseqlist* sList = (tseqlist*) list;
seqlistnode* ret = seqlist_get (list, POS);
int i = 0;

if (ret! = NULL)
{
for (i=pos+1; i<slist->length; i++)
{
SLIST-&GT;NODE[I-1] = slist->node[i];
}

slist->length--;
}

return ret;
}

3. #ifndef _seqlist_h_
#define _seqlist_h_

typedef void Seqlist;
typedef void Seqlistnode;

seqlist* seqlist_create (int capacity);

void Seqlist_destroy (seqlist* list);

void Seqlist_clear (seqlist* list);

int seqlist_length (seqlist* list);

int seqlist_capacity (seqlist* list);

int Seqlist_insert (seqlist* list, seqlistnode* node, int pos);

seqlistnode* seqlist_get (seqlist* list, int pos);

seqlistnode* seqlist_delete (seqlist* list, int pos);

#endif

4. #include "SeqStack.h"
#include "SeqList.h"

seqstack* seqstack_create (int capacity)
{
return seqlist_create (capacity);
}

void Seqstack_destroy (seqstack* stack)
{
Seqlist_destroy (stack);
}

void Seqstack_clear (seqstack* stack)
{
Seqlist_clear (stack);
}

int Seqstack_push (seqstack* stack, void* item)
{
Return Seqlist_insert (Stack, item, seqlist_length (stack));
}

void* Seqstack_pop (seqstack* stack)
{
Return Seqlist_delete (Stack, seqlist_length (stack)-1);
}

void* seqstack_top (seqstack* stack)
{
Return Seqlist_get (Stack, seqlist_length (stack)-1);
}

int seqstack_size (seqstack* stack)
{
return seqlist_length (stack);
}

int seqstack_capacity (seqstack* stack)
{
return seqlist_capacity (stack);
}
5. #ifndef _seqstack_h_
#define _seqstack_h_

typedef void Seqstack;

seqstack* seqstack_create (int capacity);

void Seqstack_destroy (seqstack* stack);

void Seqstack_clear (seqstack* stack);

int Seqstack_push (seqstack* stack, void* item);

void* Seqstack_pop (seqstack* stack);

void* seqstack_top (seqstack* stack);

int seqstack_size (seqstack* stack);

int seqstack_capacity (seqstack* stack);

#endif


The sequential instance Seqstack implementation of the stack

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.