数据结构的链栈基本操作

来源:互联网 发布:剑三脸型数据非法 编辑:程序博客网 时间:2024/06/08 06:11
本程序主要是实现
创建空栈、 进栈、 出栈、 清空栈、 判空、 取栈顶元素、 取栈底元素、

获取栈元素长度、 销毁

#include<stdio.h>#include<stdlib.h>#include"linkstack.h"int main(void){#if 0    linkstack h = malloc(sizeof(Lnode));    linkstack s1= malloc(sizeof(Lnode));    linkstack s2= malloc(sizeof(Lnode));    linkstack s3= malloc(sizeof(Lnode));    h->data = -1;    s1->data = 11;    s2->data = 22;    s3->data = 33;    h->next = s1; s1->next=s2; s2->next=s3; s3->next=NULL;#endif         linkstack mystack = CreateStack();        PushStack(mystack,123);  // 进栈    PushStack(mystack,456);    PushStack(mystack,500);    while(FALSE == StackEmpty(mystack)){        printf("%d\n",PopStack(mystack));    }        PushStack(mystack,100);    PushStack(mystack,200);    PushStack(mystack,300);    PushStack(mystack,400);    PushStack(mystack,500);    printf("len = %d\n",GetLenth(mystack)); // 栈顶长度    GetTop(mystack);              // 栈顶元素    GetBottom(mystack);           // 栈底元素    ClearStack(mystack);          // 清空栈    while(FALSE == StackEmpty(mystack)){        printf("%d\n",PopStack(mystack));    }    return FALSE;}
#include<stdio.h>#include<stdlib.h>#include"linkstack.h"// 清空栈int ClearStack(linkstack S)        {    while ( FALSE == StackEmpty(S) ) // 栈不为空进出栈    {        PopStack(S);    }    return OK;}// 获取栈的元素个数int GetLenth(linkstack S)           {    linkstack L = S->next;       int len = 0;    while ( L != NULL )    {        L = L->next;        len++;    }    //printf("%d\n",len);    return len;}// 获取栈底元素data_t GetBottom(linkstack S)         {      int len = GetLenth(S);    while ( len-- )    {        S = S->next;    }      printf("栈底元素 = %d\n",S->data);    return S->data;}// 获取栈顶元素data_t GetTop(linkstack S){    S = S->next;    printf("栈顶元素 = %d\n",S->data);    return S->data;}// 创建一个空栈linkstack CreateStack(void){    linkstack top = malloc(sizeof(Lnode));    if ( NULL == top )    {        printf("malloc error\n");        return NULL;    }    top->data = -1;    top->next = NULL;    return top;}// 进栈data_t PushStack(linkstack S,data_t val){    //新节点    linkstack x = malloc(sizeof(Lnode));    if(NULL == x)    {        printf("malloc error\n");        return ERROR;    }    x->data = val;    //插入(先链后断)    x->next = S->next;  // 头插法    S->next = x;    return FALSE;}// 判断栈是否为空int StackEmpty(linkstack S){    if ( NULL == S->next )    {        return TRUE;    }    else     {        return FALSE;    }}// 出栈data_t PopStack(linkstack S){    if(TRUE == StackEmpty(S))    {        printf("stack is empty\n");        return ERROR;    }    linkstack d = S->next;  //记录删除位置    data_t val = d->data;      //取值        S->next = d->next;      //删除     free(d);                //释放    return val;}// 销毁void Destroy(linkstack S){    while ( TRUE == StackEmpty(S) )    {        free(S);    }}
#ifndef _LINKSTACK_H#define _LINKSTACK_H#define TRUE 1#define FALSE 0#define ERROR -1#define OK 1typedef int data_t;struct node{    data_t data;    struct node *next;};typedef struct node Lnode;typedef struct node *linkstack;int ClearStack(linkstack S);                   //清空栈int GetLenth(linkstack S);                     // 获取栈的元素个数       data_t GetBottom(linkstack S);                 // 栈底元素     data_t GetTop(linkstack S);                    // 栈顶元素linkstack CreateStack(void);                   // 创建空栈data_t PushStack(linkstack S,data_t val);      // 进栈int StackEmpty(linkstack S);                   // 判断栈是否为空data_t PopStack(linkstack S);                  // 出栈void Destroy(linkstack S);                     // 销毁#endif