国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?/h1>

這篇具有很好參考價值的文章主要介紹了【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。OM麑Υ蠹矣兴鶐椭?。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法


【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解!?。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法

??一、棧的概念及結(jié)構(gòu)

??1、棧的概念定義

棧:一種特殊的線性表,其只允許在固定的一端進行插入和刪除元素操作。進行數(shù)據(jù)插入和刪除操作的一端稱為棧頂,另一端稱為棧底。棧中的數(shù)據(jù)元素遵守后進先出LIFO(Last In First Out)的原則。

  • 壓棧:棧的插入操作叫做進棧/壓棧/入棧,入數(shù)據(jù)在棧頂。
  • 出棧:棧的刪除操作叫做出棧。出數(shù)據(jù)也在棧頂。

??2、動圖演示

??入棧

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法

??出棧

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法

??整體過程

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法

??二、棧的實現(xiàn)

棧的實現(xiàn)一般可以使用數(shù)組或者鏈表實現(xiàn),相對而言數(shù)組的結(jié)構(gòu)實現(xiàn)更優(yōu)一些。因為數(shù)組在尾上插入數(shù)據(jù)的代價比較小

??三、數(shù)組結(jié)構(gòu)棧詳解

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法

??創(chuàng)建棧的結(jié)構(gòu)

??這里先創(chuàng)建三個文件:
1??:Stack.h文件,用于函數(shù)的聲明
2??:Stack.c文件用于函數(shù)的定義
3??:Test.c文件,用于測試函數(shù)
建立三個文件的目的: 將棧作為一個項目來進行編寫,方便我們的學習與觀察。

?接口1:定義結(jié)構(gòu)體(ST)

??請看代碼與注釋??

//自定義類型
typedef int STDataType;
//創(chuàng)建棧的結(jié)構(gòu)
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

?接口2:初始化(STInit)

??請看代碼與注釋??

//初始化
void STInit(ST* pst)
{
	//斷言傳入指針不為NULL
	assert(pst);

	pst->a = NULL;

	pst->top = -1;  //top指向棧頂數(shù)據(jù)
	pst->top = 0;   //top 指向棧頂數(shù)據(jù)的下一個位置

	pst->capacity = 0;
}

?接口3:銷毀(STDestroy)

??請看代碼與注釋??

//銷毀
void STDestroy(ST* pst)
{
	//斷言傳入指針不為NULL
	assert(pst);
	//釋放
	free(pst->a);

	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

?接口4:入棧(STPush)

??請看代碼與注釋??

//入棧
void STPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			return;
		}

		pst->a = temp;
		pst->capacity = newCapacity;
	}

	pst->a[pst->top] = x;
	pst->top++;
}

?接口5:出棧(STPop)

??請看代碼與注釋??

//出棧
void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	pst->top--;
}

?接口6:取棧頂數(shù)據(jù)(STTop)

??請看代碼與注釋??

//取棧頂數(shù)據(jù)
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	return pst->a[pst->top - 1];
}

?接口7:判空(STEmpty)

??請看代碼與注釋??

//判空
bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

?接口8:獲取棧的大?。⊿TSize)

??請看代碼與注釋??

//獲取棧的大小
int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

??四、完整代碼

??Stack.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化
void STInit(ST* pst);
//銷毀
void STDestroy(ST* pst);
//入棧
void STPush(ST* pst, STDataType x);
//出棧
void STPop(ST* pst);
//取棧頂數(shù)據(jù)
STDataType STTop(ST* pst);
//判空
bool STEmpty(ST* pst);
//獲取棧的大小
int STSize(ST* pst);

??Stack.c

#include"Stack.h"

//初始化
void STInit(ST* pst)
{
	assert(pst);

	pst->a = NULL;

	pst->top = -1;  //top指向棧頂數(shù)據(jù)
	pst->top = 0;   //top 指向棧頂數(shù)據(jù)的下一個位置

	pst->capacity = 0;
}

//銷毀
void STDestroy(ST* pst)
{
	assert(pst);

	free(pst->a);

	pst->a = NULL;
	pst->capacity = pst->top = 0;
}

//入棧
void STPush(ST* pst, STDataType x)
{
	if (pst->top == pst->capacity)
	{
		int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
		STDataType* temp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
		if (temp == NULL)
		{
			perror("realloc fail");
			return;
		}

		pst->a = temp;
		pst->capacity = newCapacity;
	}

	pst->a[pst->top] = x;
	pst->top++;
}

//出棧
void STPop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	pst->top--;
}

//取棧頂數(shù)據(jù)
STDataType STTop(ST* pst)
{
	assert(pst);
	assert(!STEmpty(pst));

	return pst->a[pst->top - 1];
}

//判空
bool STEmpty(ST* pst)
{
	assert(pst);

	return pst->top == 0;
}

//獲取棧的大小
int STSize(ST* pst)
{
	assert(pst);

	return pst->top;
}

??Test.c

#include"Stack.h"

//入棧測試

void TestStack1()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}

	STDestroy(&st);
}

//測試
void TestStack2()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	printf("%d ", STTop(&st));
	STPop(&st);

	STPush(&st, 3);
	STPush(&st, 4);
	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}

	STDestroy(&st);
}

int main()
{
	//TestStack1();
	//TestStack2();

	return 0;
}

??這期內(nèi)容相對比較簡單,希望烙鐵們可以理解消化哦!

總結(jié)??
以上就是 【數(shù)據(jù)結(jié)構(gòu)】?!狢語言版 的全部內(nèi)容啦????????
本文章所在【數(shù)據(jù)結(jié)構(gòu)與算法】專欄,感興趣的烙鐵可以訂閱本專欄哦??????
前途很遠,也很暗,但是不要怕,不怕的人面前才有路。??????
小的會繼續(xù)學習,繼續(xù)努力帶來更好的作品??????
創(chuàng)作寫文不易,還多請各位大佬uu們多多支持哦??????

【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。?數(shù)據(jù)結(jié)構(gòu)與算法,數(shù)據(jù)結(jié)構(gòu),c語言,棧,算法文章來源地址http://www.zghlxwxcb.cn/news/detail-691211.html

到了這里,關(guān)于【數(shù)據(jù)結(jié)構(gòu)】棧---C語言版(詳解?。。。┑奈恼戮徒榻B完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包