栈的实现与操作(C语言实现)

栈的定义
 1, 栈是一种特殊的线性表
 2,栈仅能在线性表的一端进行操作
 3,栈顶(Top): 同意操作的一端 同意操作的一端
 4,栈底(Bottom): ,不同意操作的一端 不同意操作的一端


这里我做出了 栈的顺序实现 和 链式实现。分别例如以下:

=========================================华丽丽的切割线==========================================================

栈的顺序实现:

首先,我们要弄明确的是。栈本质上就是线性表。是一种特殊的线性表。仅仅是仅仅能在一端进行操作罢了。故而非常多操作线性表是非常类似的。因此,我们能够将之前写的  顺序线性表的实现及操作(C语言实现) 中的代码直接拿过来使用,以达到代码复用的效果(代码就不在此处追述了)。

头文件:

#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

源文件:

// 栈.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <malloc.h>
#include <stdlib.h>
#include "SeqStack.h"
#include "SeqList.h"



int _tmain(int argc, _TCHAR* 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
", *(int*)SeqStack_Top(stack));
    printf("Capacity: %d
", SeqStack_Capacity(stack));
    printf("Length: %d
", SeqStack_Size(stack));
    
    while( SeqStack_Size(stack) > 0 )
    {
        printf("Pop: %d
", *(int*)SeqStack_Pop(stack));
    }
    
    SeqStack_Destroy(stack);
	system("pause");
	return 0;
}
//创建栈
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);
}

执行结果:

Top: 9
Capacity: 20
Length: 10
Pop: 9
Pop: 8
Pop: 7
Pop: 6
Pop: 5
Pop: 4
Pop: 3
Pop: 2
Pop: 1
Pop: 0
请按随意键继续. . .


=====================================我是切割线========================================================

栈的链式实现:

     同栈的顺序实现一样,栈的链式实现本质上事实上就是单链表的形式,也仅仅是在一头操作罢了,因此我们这里亦採用代码复用的方法,详细代码请參阅:链表的实现与操作(C语言实现) 


头文件:

#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_

typedef void LinkStack;

LinkStack* LinkStack_Create();

void LinkStack_Destroy(LinkStack* stack);

void LinkStack_Clear(LinkStack* stack);

int LinkStack_Push(LinkStack* stack, void* item);

void* LinkStack_Pop(LinkStack* stack);

void* LinkStack_Top(LinkStack* stack);

int LinkStack_Size(LinkStack* stack);

#endif

源文件:

#include "stdafx.h"
#include "LinkList.h"
#include "LinkStack.h"
#include <malloc.h>
#include <stdlib.h>



int main(int argc, char *argv[]) 
{
    LinkStack* stack = LinkStack_Create();
    int a[10];
    int i = 0;
    
    for(i=0; i<10; i++)
    {
        a[i] = i;
        
        LinkStack_Push(stack, a + i);
    }
    
    printf("Top: %d
", *(int*)LinkStack_Top(stack));
    printf("Length: %d
", LinkStack_Size(stack));
    
    while( LinkStack_Size(stack) > 0 )
    {
        printf("Pop: %d
", *(int*)LinkStack_Pop(stack));
    }
    
    LinkStack_Destroy(stack);
	system("pause");
	return 0;
}


typedef struct _tag_LinkStackNode
{
    LinkListNode header;
    void* item;
} TLinkStackNode;
//创建
LinkStack* LinkStack_Create()
{
    return LinkList_Create();
}

//销毁
void LinkStack_Destroy(LinkStack* stack)
{
    LinkStack_Clear(stack);
    LinkList_Destroy(stack);
}

//清空
void LinkStack_Clear(LinkStack* stack)
{
    while( LinkStack_Size(stack) > 0 )
    {
        LinkStack_Pop(stack);
    }
}


//压栈
int LinkStack_Push(LinkStack* stack, void* item)
{
    TLinkStackNode* node = (TLinkStackNode*)malloc(sizeof(TLinkStackNode));
    int ret = (node != NULL) && (item != NULL);
    
    if( ret )
    {
        node->item = item;
        
        ret  = LinkList_Insert(stack, (LinkListNode*)node, 0);
    }
    
    if( !ret )
    {
        free(node);
    }
    
    return ret;
}

//出栈
void* LinkStack_Pop(LinkStack* stack)
{
    TLinkStackNode* node = (TLinkStackNode*)LinkList_Delete(stack, 0);
    void* ret = NULL;
    
    if( node != NULL )
    {
        ret = node->item;
        
        free(node);
    }
    
    return ret;
}

//获得栈顶
void* LinkStack_Top(LinkStack* stack)
{
    TLinkStackNode* node = (TLinkStackNode*)LinkList_Get(stack, 0);
    void* ret = NULL;
    
    if( node != NULL )
    {
        ret = node->item;
    }
    
    return ret;
}

//获得栈的大小
int LinkStack_Size(LinkStack* stack)
{
    return LinkList_Length(stack);
}


执行结果:

Top: 9
Length: 10
Pop: 9
Pop: 8
Pop: 7
Pop: 6
Pop: 5
Pop: 4
Pop: 3
Pop: 2
Pop: 1
Pop: 0
请按随意键继续. . .



如有错误,望不吝指出~



原文地址:https://www.cnblogs.com/brucemengbm/p/7367477.html