DC -16 :链式栈

本文介绍了链式栈的基本概念及其实现方法,通过具体的代码示例展示了如何创建链式栈、进行入栈与出栈操作,并提供了完整的主函数运行流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zzzzzzzzzzzzzzzz程序猿的夜生活真的稳。。。。。今天改了签名:以梦为码,越敲越傻。。。愿大家的bug都可以得到完美解决。。只是这需要一点点的积累,一点点的进步。下面是链式栈的代码(错误文件昨天已传,不再传):头文件:

#ifndef __LINKSTACK_H__
#define __LINKSTACK_H__

#define SIZE 100

#define FALSE 0
#define TRUE  1


typedef int StackData;

typedef struct _node
{
	StackData data;
	struct _node *next;
}Node;

typedef struct _linkstack
{
	Node *top;
}Linkstack;



//创建
Linkstack *Create_stack();

//判栈空否
int StackEmpty (Linkstack *s);

//进栈
int Push (Linkstack *s, StackData x);	

//出栈
int Pop (Linkstack *s, StackData *x); 

 //取栈顶
int GetTop (Linkstack *s, StackData *x); 

//销毁栈
int Destroy (Linkstack *s);

函数文件:

#include "Linkstack.h"
#include <stdio.h>
#include <stdlib.h>





Linkstack *Create_stack()
{
	Linkstack *s = (Linkstack *)malloc(sizeof (Linkstack) /sizeof (char));
	if (s == NULL)
	{
		printf ("创建失败\n");
	}

	s->top = NULL;                   //	置空 栈
	return s;
	
	
}


int StackEmpty (Linkstack *s)
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	
	return s->top == NULL;    //如果是空栈,返回0
}


int Push (Linkstack *s, StackData x)
{                                          //栈无限大不用判断栈是否为满
	if (s == NULL)
	{
		return FALSE;
	}
	
	Node *node = (Node *)malloc(sizeof (Node) /sizeof (char));
	if (node == NULL)
	{
		return FALSE;
	}

	
	node->data = x;                     //头插新的节点
	node->next = s->top;
	s->top = node;
	//printf ("进栈元素为%d\n", node ->data);
	return TRUE;
	
	
}


int Pop (Linkstack *s, StackData *x)
{
	if (s == NULL)
	{

		return FALSE;
	}
	
	if ( StackEmpty(s) )
	{
		printf ("空栈无法出栈\n");
		return FALSE;
		
	}
	
	Node* p = s ->top;
	*x = p ->data;

    s ->top = p ->next;
    free(p);
	printf ("出栈元素%d",x);

	return 	TRUE;
}



int GetTop (Linkstack *s, StackData *x)
{
	if (s == NULL)
	{
		return FALSE;
	}
	
	if ( StackEmpty(s) )
	{
		return FALSE;
	}
	
    *x = s ->top->data;
	return 	TRUE;
	
}


int Destroy (Linkstack *s)
{
	if (s == NULL)
	{
		return FALSE;
	}
	int x;
	while (StackEmpty(s))
	{
		Pop (s,&x);
	}
	free(s);
	return TRUE;
	
	
	
	
}
主函数:

#include "Linkstack.h"
#include <stdio.h>



int main()
{
    Linkstack *s = Create_stack();
	if (s == NULL)
	{
		printf ("创建失败\n");
		return FALSE;
	}
	
	if (StackEmpty(s))
	{
		
		printf("空栈\n");
	}
		int x;
	Pop (s,&x);
	
	int i;
	for (i = 0; i < 10; i++)
    {
	    Push (s, i);
		
	}


	char str [100];
	for (i = 0; i < 10; i++)
	{
		if (Pop (s, &x) !=TRUE)
		{
			sprintf(str, "pop第%d个元素",i);
			myError(str);
			
		}
		printf("x=   %d",x);
	}
	
	if (Destroy (s) == FALSE)
	{
		printf ("销毁chenggong \n");
	}
	printf ("销毁失败");
		
	return  TRUE;
	
}

注意事项:

(1)栈是后进先出的。队列是先进先出的。这两句是灵魂。

(2)栈的模型可以看成一个大楼,但我们的规定是,从下向上依次存值,在上面的先出去,下面的才能出去。链式栈无限大,用一个top指针指向每个节点,所以进栈必定是头插,出栈的时候,考虑是否空栈就行了。理解这个机制,程序的实现如上。





.text:100F8C1E ?getFunctionIndirectType@UnDecorator@@CA?AVDName@@ABV2@@Z proc near .text:100F8C1E ; CODE XREF: UnDecorator::getPrimaryDataType(DName const &)+13C↓p .text:100F8C1E ; UnDecorator::getPtrRefType(DName const &,DName const &,char const *)+63↓p .text:100F8C1E .text:100F8C1E var_38 = DName ptr -38h .text:100F8C1E var_30 = DName ptr -30h .text:100F8C1E returnType = DName ptr -28h .text:100F8C1E var_20 = DName ptr -20h .text:100F8C1E rd = DName ptr -18h .text:100F8C1E thisType = DName ptr -10h .text:100F8C1E fitType = DName ptr -8 .text:100F8C1E result = dword ptr 8 .text:100F8C1E superType = dword ptr 0Ch .text:100F8C1E .text:100F8C1E push ebp .text:100F8C1F mov ebp, esp .text:100F8C21 mov ecx, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8C27 sub esp, 38h .text:100F8C2A mov al, [ecx] .text:100F8C2C test al, al .text:100F8C2E jnz short loc_100F8C48 .text:100F8C30 push [ebp+superType] ; rd .text:100F8C33 push 1 ; st .text:100F8C35 push [ebp+result] ; result .text:100F8C38 call ??H@YA?AVDName@@W4DNameStatus@@ABV0@@Z ; operator+(DNameStatus,DName const &) .text:100F8C3D add esp, 0Ch .text:100F8C40 .text:100F8C40 loc_100F8C40: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+40↓j .text:100F8C40 mov eax, [ebp+result] .text:100F8C43 jmp loc_100F9023 .text:100F8C48 ; --------------------------------------------------------------------------- .text:100F8C48 .text:100F8C48 loc_100F8C48: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+10↑j .text:100F8C48 cmp al, 36h ; '6' .text:100F8C4A jl short loc_100F8C50 .text:100F8C4C cmp al, 39h ; '9' .text:100F8C4E jle short loc_100F8C60 .text:100F8C50 .text:100F8C50 loc_100F8C50: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+2C↑j .text:100F8C50 cmp al, 5Fh ; '_' .text:100F8C52 jz short loc_100F8C60 .text:100F8C54 mov ecx, [ebp+result] ; this .text:100F8C57 push 2 ; st .text:100F8C59 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8C5E jmp short loc_100F8C40 .text:100F8C60 ; --------------------------------------------------------------------------- .text:100F8C60 .text:100F8C60 loc_100F8C60: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+30↑j .text:100F8C60 ; UnDecorator::getFunctionIndirectType(DName const &)+34↑j .text:100F8C60 push ebx .text:100F8C61 movsx ebx, al .text:100F8C64 sub ebx, 36h ; '6' .text:100F8C67 inc ecx .text:100F8C68 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8C6E cmp ebx, 29h ; ')' .text:100F8C71 jnz short loc_100F8CA2 .text:100F8C73 mov al, [ecx] .text:100F8C75 test al, al .text:100F8C77 jz short loc_100F8C90 .text:100F8C79 movsx ebx, al .text:100F8C7C sub ebx, 3Dh ; '=' .text:100F8C7F inc ecx .text:100F8C80 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8C86 cmp ebx, 4 .text:100F8C89 jl short loc_100F8CAB .text:100F8C8B cmp ebx, 7 .text:100F8C8E jmp short loc_100F8CA9 .text:100F8C90 ; --------------------------------------------------------------------------- .text:100F8C90 .text:100F8C90 loc_100F8C90: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+59↑j .text:100F8C90 push [ebp+superType] ; rd .text:100F8C93 push 1 ; st .text:100F8C95 push [ebp+result] ; result .text:100F8C98 call ??H@YA?AVDName@@W4DNameStatus@@ABV0@@Z ; operator+(DNameStatus,DName const &) .text:100F8C9D add esp, 0Ch .text:100F8CA0 jmp short loc_100F8CBD .text:100F8CA2 ; --------------------------------------------------------------------------- .text:100F8CA2 .text:100F8CA2 loc_100F8CA2: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+53↑j .text:100F8CA2 test ebx, ebx .text:100F8CA4 js short loc_100F8CAB .text:100F8CA6 cmp ebx, 3 .text:100F8CA9 .text:100F8CA9 loc_100F8CA9: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+70↑j .text:100F8CA9 jle short loc_100F8CAE .text:100F8CAB .text:100F8CAB loc_100F8CAB: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+6B↑j .text:100F8CAB ; UnDecorator::getFunctionIndirectType(DName const &)+86↑j .text:100F8CAB or ebx, 0FFFFFFFFh .text:100F8CAE .text:100F8CAE loc_100F8CAE: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &):loc_100F8CA9↑j .text:100F8CAE cmp ebx, 0FFFFFFFFh .text:100F8CB1 jnz short loc_100F8CC5 .text:100F8CB3 mov ecx, [ebp+result] ; this .text:100F8CB6 push 2 ; st .text:100F8CB8 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8CBD .text:100F8CBD loc_100F8CBD: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+82↑j .text:100F8CBD mov eax, [ebp+result] .text:100F8CC0 jmp loc_100F9022 .text:100F8CC5 ; --------------------------------------------------------------------------- .text:100F8CC5 .text:100F8CC5 loc_100F8CC5: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+93↑j .text:100F8CC5 and [ebp+thisType.node], 0 .text:100F8CC9 and dword ptr [ebp+thisType._bf_4], 0FFFF0000h .text:100F8CD0 push esi .text:100F8CD1 mov esi, [ebp+superType] .text:100F8CD4 push edi .text:100F8CD5 mov edi, ebx .text:100F8CD7 mov eax, [esi] .text:100F8CD9 mov [ebp+fitType.node], eax .text:100F8CDC mov eax, [esi+4] .text:100F8CDF mov dword ptr [ebp+fitType._bf_4], eax .text:100F8CE2 and edi, 2 .text:100F8CE5 jz loc_100F8DD7 .text:100F8CEB cmp byte ptr [ecx], 40h ; '@' .text:100F8CEE jz loc_100F8D96 .text:100F8CF4 lea eax, [ebp+fitType] .text:100F8CF7 push eax ; rd .text:100F8CF8 lea eax, [ebp+rd] .text:100F8CFB push eax ; result .text:100F8CFC push offset asc_10131938 ; "::" .text:100F8D01 lea ecx, [ebp+var_20] ; this .text:100F8D04 call ??0DName@@QAE@PBD@Z ; DName::DName(char const *) .text:100F8D09 mov ecx, eax ; this .text:100F8D0B call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D10 mov eax, [ebp+rd.node] .text:100F8D13 mov [ebp+fitType.node], eax .text:100F8D16 mov eax, dword ptr [ebp+rd._bf_4] .text:100F8D19 mov dword ptr [ebp+fitType._bf_4], eax .text:100F8D1C mov eax, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8D21 cmp byte ptr [eax], 0 .text:100F8D24 jz short loc_100F8D69 .text:100F8D26 lea eax, [ebp+var_30] .text:100F8D29 push eax ; result .text:100F8D2A call ?getScope@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getScope(void) .text:100F8D2F pop ecx .text:100F8D30 push eax ; rd .text:100F8D31 lea eax, [ebp+var_20] .text:100F8D34 push eax ; result .text:100F8D35 push 20h ; ' ' ; ch .text:100F8D37 lea ecx, [ebp+var_38] ; this .text:100F8D3A call ??4DName@@QAEAAV0@D@Z ; DName::operator=(char) .text:100F8D3F mov ecx, eax ; this .text:100F8D41 call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D46 mov eax, [ebp+var_20.node] .text:100F8D49 lea ecx, [ebp+rd] ; this .text:100F8D4C mov [ebp+rd.node], eax .text:100F8D4F mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8D52 mov dword ptr [ebp+rd._bf_4], eax .text:100F8D55 lea eax, [ebp+fitType] .text:100F8D58 push eax ; rd .text:100F8D59 call ??YDName@@QAEAAV0@ABV0@@Z ; DName::operator+=(DName const &) .text:100F8D5E mov eax, [ebp+rd.node] .text:100F8D61 mov [ebp+fitType.node], eax .text:100F8D64 mov eax, dword ptr [ebp+rd._bf_4] .text:100F8D67 jmp short loc_100F8D8B .text:100F8D69 ; --------------------------------------------------------------------------- .text:100F8D69 .text:100F8D69 loc_100F8D69: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+106↑j .text:100F8D69 lea eax, [ebp+fitType] .text:100F8D6C push eax ; rd .text:100F8D6D lea eax, [ebp+var_20] .text:100F8D70 push eax ; result .text:100F8D71 push 1 ; st .text:100F8D73 lea ecx, [ebp+var_38] ; this .text:100F8D76 call ??0DName@@QAE@W4DNameStatus@@@Z ; DName::DName(DNameStatus) .text:100F8D7B mov ecx, eax ; this .text:100F8D7D call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8D82 mov eax, [ebp+var_20.node] .text:100F8D85 mov [ebp+fitType.node], eax .text:100F8D88 mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8D8B .text:100F8D8B loc_100F8D8B: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+149↑j .text:100F8D8B mov ecx, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName .text:100F8D91 mov dword ptr [ebp+fitType._bf_4], eax .text:100F8D94 jmp short loc_100F8D9D .text:100F8D96 ; --------------------------------------------------------------------------- .text:100F8D96 .text:100F8D96 loc_100F8D96: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+D0↑j .text:100F8D96 inc ecx .text:100F8D97 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8D9D .text:100F8D9D loc_100F8D9D: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+176↑j .text:100F8D9D mov al, [ecx] .text:100F8D9F test al, al .text:100F8DA1 jz loc_100F8E4B .text:100F8DA7 cmp al, 40h ; '@' .text:100F8DA9 jnz loc_100F8E44 .text:100F8DAF mov eax, ?disableFlags@UnDecorator@@0KA ; ulong UnDecorator::disableFlags .text:100F8DB4 inc ecx .text:100F8DB5 and eax, 60h .text:100F8DB8 mov ?gName@UnDecorator@@0PBDB, ecx ; char const * const UnDecorator::gName .text:100F8DBE cmp al, 60h ; '`' .text:100F8DC0 lea eax, [ebp+var_38] .text:100F8DC3 push eax ; result .text:100F8DC4 jz short loc_100F8E33 .text:100F8DC6 call ?getThisType@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getThisType(void) .text:100F8DCB pop ecx .text:100F8DCC mov ecx, [eax] .text:100F8DCE mov eax, [eax+4] .text:100F8DD1 mov [ebp+thisType.node], ecx .text:100F8DD4 mov dword ptr [ebp+thisType._bf_4], eax .text:100F8DD7 .text:100F8DD7 loc_100F8DD7: ; CODE XREF: UnDecorator::getFunctionIndirectType(DName const &)+C7↑j .text:100F8DD7 ; UnDecorator::getFunctionIndirectType(DName const &)+224↓j .text:100F8DD7 test bl, 4 .text:100F8DDA jz loc_100F8E77 .text:100F8DE0 mov eax, ?disableFlags@UnDecorator@@0KA ; ulong UnDecorator::disableFlags .text:100F8DE5 shr eax, 1 .text:100F8DE7 not eax .text:100F8DE9 test al, 1 .text:100F8DEB lea eax, [ebp+var_38] .text:100F8DEE push eax ; result .text:100F8DEF jz short loc_100F8E68 .text:100F8DF1 call ?getBasedType@UnDecorator@@CA?AVDName@@XZ ; UnDecorator::getBasedType(void) .text:100F8DF6 pop ecx .text:100F8DF7 push eax ; rd .text:100F8DF8 lea eax, [ebp+var_20] .text:100F8DFB push eax ; result .text:100F8DFC push 20h ; ' ' ; ch .text:100F8DFE lea ecx, [ebp+var_30] ; this .text:100F8E01 call ??4DName@@QAEAAV0@D@Z ; DName::operator=(char) .text:100F8E06 mov ecx, eax ; this .text:100F8E08 call ??HDName@@QBE?AV0@ABV0@@Z ; DName::operator+(DName const &) .text:100F8E0D mov eax, [ebp+var_20.node] .text:100F8E10 lea ecx, [ebp+rd] ; this .text:100F8E13 mov [ebp+rd.node], eax .text:100F8E16 mov eax, dword ptr [ebp+var_20._bf_4] .text:100F8E19 mov dword ptr [ebp+rd._bf_4], eax .text:100F8E1C lea eax, [ebp+fitType] .text:100F8E1F push eax ; rd .text:100F8E20 call ??YDName@@QAEAAV0@ABV0@@Z ; DName::operator+=(DName const &) .text:100F8E25 mov eax, [ebp+rd.node] .text:100F8E28 mov [ebp+fitType.node], eax .text:100F8E2B mov eax, dword ptr [ebp+rd._bf_4] .text:100F8E2E mov dword ptr [ebp+fitType._bf_4], eax .text:100F8E31 jmp short loc_100F8E77 .text:100F8E33 ; --------------------------------------------------------------------------- .text:100F8E33 为什么只在text:100F8D1C mov eax, ?gName@UnDecorator@@0PBDB ; char const * const UnDecorator::gName 崩溃 在其他地方也用到了gname
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值