棧-順序表(代碼、分析、匯編)

目錄:

    • 代碼:
    • 分析:
    • 匯編:

代碼:

SeqList.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_typedef void SeqList;//定義順序表類型
typedef void SeqListNode;//定義順序表節點類型SeqList* SeqList_Create(int capacity);void SeqList_Destroy(SeqList* list);void SeqList_Clear(SeqList* list);int SeqList_Length(SeqList* list);int SeqList_Capacity(SeqList* list);int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);SeqListNode* SeqList_Get(SeqList* list, int pos);SeqListNode* SeqList_Delete(SeqList* list, int pos);#endif

SeqList.c

#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"typedef unsigned int TSeqListNode;//定義實際使用的順序表節點類型typedef struct _tag_SeqList //定義實際使用的順序表類型
{int capacity;//容量int length;//當前長度長度TSeqListNode* node; //節點指針(數組)
} TSeqList;SeqList* SeqList_Create(int capacity) //定義創建表函數
{TSeqList* ret = NULL;if( capacity >= 0 ){ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);}if( ret != NULL )//如果創建成功{ret->capacity = capacity;//容量賦值ret->length = 0;//當前長度賦值ret->node = (TSeqListNode*)(ret + 1);//表內的指針等于第一個節點}return ret;//返回新建表
}void SeqList_Destroy(SeqList* list) //定義銷毀表函數
{free(list);
}void SeqList_Clear(SeqList* list) //定義清空重設表函數
{TSeqList* sList = (TSeqList*)list;if( sList != NULL ){sList->length = 0;//重設置0}
}int SeqList_Length(SeqList* list) //定義獲取表當前長度函數
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->length;//獲取當前表長度}return ret;//返回長度
}int SeqList_Capacity(SeqList* list) //定義獲取表容量函數
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->capacity;//獲取容量}return ret;//返回容量
}int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //定義插入節點函數
{TSeqList* sList = (TSeqList*)list;int ret = (sList != NULL);int i = 0;ret = ret && (sList->length + 1 <= sList->capacity);//判斷表是否為空與表是否有節點插入新節點ret = ret && (0 <= pos);//判斷pos是否正常if( ret ){if( pos >= sList->length )//如果pos大于當前長度{pos = sList->length;//將插入位置設為最后一位}for(i=sList->length; i>pos; i--)//節點數據后移{sList->node[i] = sList->node[i-1];}sList->node[i] = (TSeqListNode)node;//將地址轉成使用的表節點類型(數值)賦給節點sList->length++;//長度增加}return ret;
}SeqListNode* SeqList_Get(SeqList* list, int pos)//定義獲取節點數據函數
{TSeqList* sList = (TSeqList*)list;SeqListNode* ret = NULL;if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )//判斷表是否為空與pos是否在范圍{ret = (SeqListNode*)(sList->node[pos]);//獲取節點數據}return ret;
}SeqListNode* SeqList_Delete(SeqList* list, int pos)//定義刪除表節點函數 
{TSeqList* sList = (TSeqList*)list;SeqListNode* ret = SeqList_Get(list, pos);//取得要刪除節點int i = 0;if( ret != NULL )//節點不為空{for(i=pos+1; i<sList->length; i++)//節點數據前移更改要刪除節點數據{sList->node[i-1] = sList->node[i];}sList->length--;//長度減少}return ret;//返回刪除節點
}

SeqStack.h

#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

SeqStack.c

#include "SeqStack.h"
#include "SeqList.h"//注意: 該棧只是套多一層調用順序表的函數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);
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"int main(int argc, char *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\n", *(int*)SeqStack_Top(stack));printf("Capacity: %d\n", SeqStack_Capacity(stack));printf("Length: %d\n", SeqStack_Size(stack));while( SeqStack_Size(stack) > 0 ){printf("Pop: %d\n", *(int*)SeqStack_Pop(stack));}SeqStack_Destroy(stack);return 0;
}

分析:

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

匯編:

在這里插入圖片描述

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379251.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379251.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379251.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

sqlserver 數據庫日志文件過大的問題

USE[master] GO ALTER DATABASE 【數據庫】 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 【數據庫】 SET RECOVERY SIMPLE --簡單模式 GO USE 【數據庫】 GO DBCC SHRINKFILE (N【數據庫日志文件名&#xff08;如&#xff1a;msdb_log&#xff09;】 , 11, TRUNCATE…

SQl Server存儲過程基礎

一、存儲過程的概念 存儲過程是SQL語句和可選控制流語句的預編譯集合&#xff0c;存儲在數據庫中&#xff0c;可由應用程序通過一個調用執行&#xff0c;而且允許用戶聲明變量、有條件執行以及其他強大的編程功能。 在SQL Server中存儲過程分為兩類&#xff1a;即系統提供的存儲…

isinfinite_Java Double類isInfinite()方法與示例

isinfinite雙類isInfinite()方法 (Double class isInfinite() method) isInfinite() method is available in java.lang package. isInfinite()方法在java.lang包中可用。 isInfinite() method is used to check infinity (i.e. either positive infinity or negative infinity…

MySql學習(一)

SQL語句的分類&#xff1a;DDL&#xff08;數據定義語言&#xff09;、DML&#xff08;數據操作語言&#xff09;、DCL&#xff08;數據控制語句&#xff09;MySql的命令語句以&#xff1b;或\g結束建庫&#xff1a;Create database name;建表&#xff1a;Create tablle name;選…

C盤爆紅的解決辦法

方法一&#xff1a;開始—>搜索—>cleanmgr—>磁盤清理—>選擇你所需要清理的磁盤即可 方法二&#xff1a;我的電腦右擊—>管理—>磁盤管理—>右鍵選擇你要壓縮的磁盤—>壓縮卷—>輸入壓縮空間量—>壓縮—>右鍵 拓展卷—>下一步—>輸入…

棧應用_檢測成對符號是否正確使用(代碼、分析、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; LinkList.h LinkList.c LinkStack.h LinkStack.c 棧-線性表 main.c #include <stdio.h> #include <stdlib.h> #include "LinkStack.h"//該程序是檢查字符串中的出…

Oracle 參數分類 和 參數的查看方法

Oracle數據庫系統根據初始化參數文件init.ora中設置的參數來配置自身的啟動&#xff0c;每個實例在啟動之前&#xff0c;首先讀取這些參數文件中設置的不同參數。 Oracle系統中的參數&#xff0c;根據系統使用情況可以簡單分為兩大類&#xff1a; 普通參數&#xff1a;也就是Or…

java 方法 示例_Java集合checkedList()方法與示例

java 方法 示例集合類checkedList()方法 (Collections Class checkedList() method) checkedList() Method is available in java.lang package. CheckedList()方法在java.lang包中可用。 checkedList() Method is used to return the typesafe view of the given List at runt…

ParameterizedTypeImpl

關于“通過反射獲得泛型的參數化類型”的問題&#xff1a; 下面是張老師 通過反射獲得泛型的參數化類型的一段代碼import java.util.*; import java.lang.reflect.*;public class GenericalReflection { private Vector<Date> dates new Vector<Date>(); public v…

Spyder打開報錯解決辦法

刪除C:\Users\Administrator路徑下.matplotlib和.spyder-py3&#xff08;你的有可能是.spyder2或.spyder3&#xff09;這兩個文件夾&#xff0c;然后再重啟Spyder即可 &#xff08;本人也是查找多篇大佬博客然后最后找到的解決方法&#xff09;

ERP軟件銷售的方法論--SPIN銷售法(SPIN Selling)

有許多人一直在做的ERP軟件產品的銷售&#xff0c;也接受了許多關于銷售方法的培訓&#xff0c;我所了解的就包括有C139&#xff0c;用友的《策略九問》等方法&#xff0c;但其實行業內早就有先驅創立了相關的銷售方法&#xff0c;那就是SPIN銷售法&#xff0c;之前一直都是只知…

ffmpeg - AVPacket內存問題分析(AVFrame一樣的)

目錄&#xff1a;1、av_packet_alloc()和av_packet_free()2、av_init_packet()的問題3、av_packet_move_ref()的問題4、av_packet_clone()的問題5、AVPacket的引用計數問題6、 AVFrame一樣的1、av_packet_alloc()和av_packet_free() 源碼中av_packet_unref()調用av_buffer_unre…

Java類class isSynthetic()方法及示例

類的類isSynthetic()方法 (Class class isSynthetic() method) isSynthetic() method is available in java.lang package. isSynthetic()方法在java.lang包中可用。 isSynthetic() method is used to check whether this Class is a synthetic class or not. isSynthetic()方法…

BNU OJ 第26303 題 Touchscreen Keyboard

BNU OJ第26303題Touchscreen Keyboard&#xff08;題目鏈接&#xff09;的解題報告。 原題如下&#xff1a; Touchscreen Keyboard Problem Description Nowadays, people do not use hardware keyboards but touchscreens. Usually, they touch on the wrong letters with the…

列表(二)

1&#xff0c;什么是列表&#xff1f; 列表由一系列按特定順序排列的元素組成。得知列表內的元素是有序的。 在Python中&#xff0c;用方括號&#xff08;[]&#xff09;來表示列表&#xff0c;并用逗號來分隔其中的元素。 color [red,blue,black,yellow]#定義一個字符串列表…

Zigbee在.Net Micro Framework系統中的應用

Zigbee是IEEE 802.15.4協議的代名詞。根據這個協議規定的技術是一種短距離、低功耗的無線通信技術。這一名稱來源于蜜蜂的八字舞&#xff0c;由于蜜蜂(bee)是靠飛翔和“嗡嗡”(zig)地抖動翅膀的“舞蹈”來與同伴傳遞花粉所在方位信息&#xff0c;也就是說蜜蜂依靠這樣的方式構成…

ffmpeg-AVFrame分配內存問題

目錄&#xff1a;1、格式&#xff1a;交錯式2、格式&#xff1a;平坦式3、總結&#xff1a;1、格式&#xff1a;交錯式 LRLRRLRLRLRLRLRLRLR 2、格式&#xff1a;平坦式 LLLLLLRRRRRR 3、總結&#xff1a; 兩種方式的內存排列在AVFrame中分配是有區別的 交錯式在一個buf…

stl中map函數_map :: empty()函數以及C ++ STL中的Example

stl中map函數C STL映射:: empty() (C STL map::empty()) It is built-in function in C STL and used to check whether the map container is empty or not i.e whether its size is 0 or not? 它是C STL中的內置函數&#xff0c;用于檢查地圖容器是否為空&#xff0c;即其…

C#使用Dotfuscator混淆代碼以及加密

C#編寫的代碼如果不進行一定程度的混淆和加密&#xff0c;那么是非常容易被反編譯進行破解的&#xff0c;特別是對于一些商業用途的C#軟件來說&#xff0c;因為盯著的人多&#xff0c;更是極易被攻破。使用Dotfuscator可以實現混淆代碼、變量名修改、字符串加密等功能。 這里介…

操作列表(三)

1&#xff0c;for循環(for 變量名 in 列表名:) phone [iphone 8, xiaomi10pro, huaweiv30pro, honor20, jianguopro]#定義一個列表phone for tel in phone:print("手機的類型為&#xff1a;" tel.title())#當然這里的每個元素也可以調用title()等一些方法 print(&…