3.1.3栈的链接实现

// DataStructTest.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

typedef 
struct node
{
    
int data;
    
struct node * next;
}
LStackTp;

//初始化.书上的算法没有考虑到释放栈的空间
void InitStack(LStackTp ** ls)
{
    LStackTp 
* next=NULL;
    
while((*ls)!=NULL)
    
{
        next
=(*ls)->next;
        free((
*ls));
        (
*ls)=next;
    }

}

//入栈
int Push(LStackTp ** ls,int value)
{
    LStackTp 
* p=(LStackTp *)malloc(sizeof(node));
    p
->next=(*ls);
    p
->data=value;
    (
*ls)=p;
    
return 1;
}

//出栈
int Pop(LStackTp ** ls,int & value)
{
    LStackTp 
* p=NULL;
    
if ((*ls)==NULL)
        
return 0;
    value
=(*ls)->data;
    p
=(*ls)->next;
    free((
*ls));
    (
*ls)=p;
    
return 1;
}

//判断是否为空栈
int EmptyStack(LStackTp * ls)
{
    
if (ls==NULL)
        
return 1;
    
else
        
return 0;
}

//取顶元素
int GetTop(LStackTp * ls,int & value)
{
    
if (ls==NULL)
        
return 0;
    value
=ls->data;
    
return 1;
}

//显示
void Display(LStackTp * ls)
{
    LStackTp 
* p=ls;
    
while(p!=NULL)
    
{
        cout
<<"栈中元素:"<<p->data<<endl;
        p
=p->next;
    }

}

int main(int argc, char* argv[])
{
    LStackTp 
* ls=NULL;
    InitStack(
&ls);
    
//
    cout<<"分别入栈6,5,4,3,2,1"<<endl;
    Push(
&ls,6);
    Push(
&ls,5);
    Push(
&ls,4);
    Push(
&ls,3);
    Push(
&ls,2);
    Push(
&ls,1);
    Display(ls);
    
//
    int value=0;
    Pop(
&ls,value);
    cout
<<"退栈操作得到的结果为:"<<value<<endl;
    Display(ls);
    
//
    GetTop(ls,value);
    cout
<<"得到栈顶原素为:"<<value<<endl;
    
//
    InitStack(&ls);
    
return 0;
}




//--------------------------------------------参数上使用指针的引用形式,不使用二级指针---------------------------------------------------
// DataStructTest.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

typedef 
struct node
{
    
int data;
    
struct node * next;
}
LStackTp;

typedef node 
* lpnode;

//初始化.书上的算法没有考虑到释放栈的空间,参数上表明的是对指针类型的引用
void InitStack(LStackTp * & ls)
{
    LStackTp 
* next=NULL;
    
while(ls!=NULL)
    
{
        next
=ls->next;
        free(ls);
        ls
=next;
    }

}

//入栈
int Push(LStackTp * & ls,int value)
{
    LStackTp 
* p=(LStackTp *)malloc(sizeof(node));
    p
->next=ls;
    p
->data=value;
    ls
=p;
    
return 1;
}

//出栈
int Pop(LStackTp * & ls,int & value)
{
    LStackTp 
* p=NULL;
    
if (ls==NULL)
        
return 0;
    value
=ls->data;
    p
=ls->next;
    free(ls);
    ls
=p;
    
return 1;
}

//判断是否为空栈
int EmptyStack(LStackTp * ls)
{
    
if (ls==NULL)
        
return 1;
    
else
        
return 0;
}

//取顶元素
int GetTop(LStackTp * ls,int & value)
{
    
if (ls==NULL)
        
return 0;
    value
=ls->data;
    
return 1;
}

//显示
void Display(LStackTp * ls)
{
    LStackTp 
* p=ls;
    
while(p!=NULL)
    
{
        cout
<<"栈中元素:"<<p->data<<endl;
        p
=p->next;
    }

}

int main(int argc, char* argv[])
{
    LStackTp 
* ls=NULL;
    InitStack(ls);
    
//
    cout<<"分别入栈6,5,4,3,2,1"<<endl;
    Push(ls,
6);
    Push(ls,
5);
    Push(ls,
4);
    Push(ls,
3);
    Push(ls,
2);
    Push(ls,
1);
    Display(ls);
    
//
    int value=0;
    Pop(ls,value);
    cout
<<"退栈操作得到的结果为:"<<value<<endl;
    Display(ls);
    
//
    GetTop(ls,value);
    cout
<<"得到栈顶原素为:"<<value<<endl;
    
//
    InitStack(ls);
    
return 0;
}

posted @ 2007-06-25 11:35  吴东雷  阅读(296)  评论(0编辑  收藏  举报