網站首頁 編程語言 正文
靜態分配
編譯時已確定數組的大小和位置,程序運行期間不可改變。
源代碼如下:
#include<stdio.h>
#include<stdlib.h>
#define maxSize 1000
#define OVERFLOW 9999
#define ERROR -1
#define EMPTY -2
typedef struct{
int Selem[maxSize];
int length;
}SList;
//函數申明
void initList(SList *L);
void insertList(SList *L,int i,int e);
int listLength(SList L);
int getElem1(SList L,int i);
void getElem2(SList L,int i,int *e);
int locateElem(SList L,int e);
void listDelete(SList *L,int i,int *e);
void printList(SList L);
int emptyList(SList L);
void destroyList(SList *L);
int main(){
SList L;
initList(&L);
int e;
int i;
int o;
for(i=1;i<5;i++){
insertList(&L,i,i);
}
printList(L);
printf("Hello!\n");
int m = listLength(L);
printf("順序表的長度:\t%d\n",m);
insertList(&L,2,22);
printList(L);
printf("第二個元素:%d\t\n",getElem1(L,1));
getElem2(L,2,&e);
printf("第三個元素:%d\t\n",e);
printf("元素4的index=%d\n",locateElem(L,4));
listDelete(&L,3,&o);
printf("刪除的元素值:%d\n",o);
printList(L);
printf("%d\n",emptyList(L));
// destroyList(&L);
return 0;
}
// 初始化鏈表
void initList(SList *L){
L = malloc(sizeof(SList));
if(!L->Selem)
exit(OVERFLOW);
L->length = 0;
return;
}
//插入元素
void insertList(SList *L,int i,int e){
if(L->length==maxSize)
exit(OVERFLOW);
if(i<1 || i>L->length)
exit(ERROR);
int j;
for(j=L->length-1;j>=i-1;j--) //表示在線性表不為空的條件下
L->Selem[j+1] = L->Selem[j]; //從表尾開始到插入位置,數據元素依次后移一個位置
L->Selem[i-1] = e; // e插入到線性表的第i個位置
L->length++;
return;
}
// 求線性表的長度
int listLength(SList L){
return L.length-1;
}
// 獲取線性表的第i個元素
int getElem1(SList L,int i){
if(i<0 || i>L.length)
exit(ERROR);
return L.Selem[i];
}
// 獲取線性表的第i個元素
void getElem2(SList L,int i,int *e){
if(i<0 || i>L.length)
exit(ERROR);
*e = L.Selem[i];
return;
}
// 確定元素e是線性表中的第幾個元素
int locateElem(SList L,int e){
int i;
for(i=0;i<L.length-1;i++)
if(L.Selem[i]==e)
break;
if(i!=L.length)
return i;
else
return 0;
}
//刪除線性表中的第i個元素,將其放在變量e中
void listDelete(SList *L,int i,int *e){
if(L->length==0){
exit(EMPTY);
}
if(i<1 || i>L->length)
exit(ERROR);
*e = L->Selem[i-1]; // 線性表中的第i個元素存放于變量e中
int j;
for(j=i-1; j<L->length-1; j++)
{ //從第i個數據元素開始,依次將后面的元素前移一個位置
L->Selem[j] = L->Selem[j+1];
}
L->length--;
return;
}
//遍歷線性表
void printList(SList L){
int i;
for( i=0;i<L.length-1;i++){
printf("%d\t",L.Selem[i]);
}
printf("\n");
}
//判斷線性表為空,前提條件是線性表存在
int emptyList(SList L){
if(L.length > 0) return 1;
return 0;
}
//銷毀線性表
void destroyList(SList *L){
if(!L->Selem) //沒有可銷毀的內容
exit(ERROR);
free(L->Selem);
L->length =0;
}
運行結果
代碼來源于教材,經過組織修正得到的結果。
原文鏈接:https://blog.csdn.net/Austin_/article/details/108925615
相關推薦
- 2022-07-06 React項目中hook實現展示對話框功能_React
- 2021-12-03 關于Redis數據庫入門詳細介紹_Redis
- 2022-11-10 ASP.NET?MVC使用Quartz.NET執行定時任務_實用技巧
- 2022-05-28 python中flatten()參數示例詳解_python
- 2022-10-17 Qt線程池QThreadPool的使用詳解_C 語言
- 2022-10-16 Python?Celery動態添加定時任務生產實踐指南_python
- 2022-04-03 Rust?連接?PostgreSQL?數據庫的詳細過程_PostgreSQL
- 2023-03-18 C++虛函數和多態超詳細分析_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支