国产成人精品三级麻豆,色综合天天综合高清网,亚洲精品夜夜夜,国产成人综合在线女婷五月99播放,色婷婷色综合激情国产日韩

當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 數(shù)據(jù)結(jié)構(gòu)之順序表

數(shù)據(jù)結(jié)構(gòu)之順序表 時(shí)間:2018-01-10      來源:未知

 什么是順序表?

首先順序表指的是在數(shù)據(jù)結(jié)構(gòu)中的一種線性存儲(chǔ)結(jié)構(gòu),區(qū)別于鏈?zhǔn)奖怼?/p>

其主要借助元素在存儲(chǔ)器中的相對(duì)位置來表示數(shù)據(jù)元素間的邏輯關(guān)系。通常存將數(shù)據(jù)儲(chǔ)在一片連續(xù)的空間上,例如數(shù)組。

結(jié)構(gòu)如下圖:

數(shù)據(jù)結(jié)構(gòu)順序表

順序表的實(shí)現(xiàn):

在C語言中,一維數(shù)組的元素也是存放于一片連續(xù)的存儲(chǔ)空間中,故可借助于C語言中一維數(shù)組類型來描述線性表的順序存儲(chǔ)結(jié)構(gòu)。

頭文件如下:
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
#include <stdio.h>
#include <stdlib.h>
#define N 10

//順序表結(jié)構(gòu)的定義

//data為數(shù)組,用于存儲(chǔ)數(shù)據(jù)

//last為整形數(shù),用來記錄數(shù)組中后一個(gè)元素的下標(biāo)

typedef struct seqlist

{

int data[N];

int last;

}seqlist_t;

//創(chuàng)建順序表

seqlist_t *seqlistCreate();

//判斷順序表是否未滿

intisFull(seqlist_t *s);

//判斷順序表是否為空

intisEmtpy(seqlist_t *s);

//插入數(shù)據(jù)

intseqlistInesert(seqlist_t *s,intvalue,int offset);

//刪除數(shù)據(jù)

intseqlistDelete(seqlist_t *s,int offset);

//查看順序表

void seqlistShow(seqlist_t *s);

#endif

函數(shù)實(shí)現(xiàn)如下:
#include "seqlist.h"
//創(chuàng)建順序表,返回順序表的地址
seqlist_t *seqlistCreate()
{
  seqlist_t *s = NULL;
  s = (seqlist_t*)malloc(sizeof(seqlist_t));
  if(NULL == s)
  {
        printf("create err,fail to malloc\n");
        return NULL;
  }
  s->last = -1;
 
  return s;
}
//判斷順序表是否未滿,last值為N-1時(shí)順序表為滿
intisFull(seqlist_t *s)
{
  return s->last == N - 1;
}
//判斷順序表是否為空
intisEmtpy(seqlist_t *s)
{
  return s->last == -1;
}
//插入數(shù)據(jù)
//value:插入數(shù)據(jù)
//offset:插入位置
intseqlistInesert(seqlist_t *s,intvalue,int offset)
{
  if(isFull(s))//判斷順序表是否為滿
  {
        printf("insert err,seqlist is full\n");
        return -1;
  }
  if(offset < 0 || offset > s->last + 1)//判斷插入的位置offset是否有誤
  {
        printf("insert err,err offset\n");
        return -1;
  }
  inti = s->last;//臨時(shí)變量i保存last;
  while(i>= offset)//移動(dòng)i所標(biāo)記的元素,后移動(dòng)的為offset標(biāo)記位置的元素
  {
        s->data[i + 1] = s->data[i];
        i--;
  }
  s->data[offset] = value;
  s->last++;//讓last標(biāo)記新的末尾元素。
 
  return 1;
}
//刪除數(shù)據(jù)
intseqlistDelete(seqlist_t *s,int offset)
{
  if(isEmtpy(s))
  {
        printf("delete err,seqlist is empty\n");
        return -1;
  }
  if(offset < 0 || offset > s->last)
  {
        printf("delete err,err offset\n");
        return -1;
  }
  int ret = s->data[offset];
  while(offset < s->last)
  {
        s->data[offset] = s->data[offset + 1];
        offset++;
  }
  s->last--;
 
  return ret;
}
//查看順序表
void seqlistShow(seqlist_t *s)
{
  inti = 0;
  while(i<= s->last)
  {
        printf("%d ",s->data[i]);
        i++;
  }
}

主函數(shù)用于測(cè)試:
#include "seqlist.h"
intmain()
{
  seqlist_t *s = seqlistCreate();
  seqlistInesert(s,1,0);
  seqlistInesert(s,2,1);
  seqlistInesert(s,3,2);
  seqlistInesert(s,4,3);
  seqlistInesert(s,5,4);
  seqlistShow(s);
  puts("");
  seqlistDelete(s,4);
  seqlistShow(s);
  puts("");
 
  return 0;
}

上一篇:標(biāo)準(zhǔn)IO 中對(duì)文件的基本操作

下一篇:音頻解碼的兩個(gè)標(biāo)準(zhǔn)AC97和IIS

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺(tái)專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號(hào)-5京公海網(wǎng)安備11010802025203號(hào)

回到頂部