C語言實(shí)現(xiàn)的順序表功能完整實(shí)例

本文實(shí)例講述了C語言實(shí)現(xiàn)的順序表功能。分享給大家供大家參考,具體如下:

廣安網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),廣安網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為廣安近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的廣安做網(wǎng)站的公司定做!

seqlist.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__
#include<cstdio>
#include<malloc.h>
#include<assert.h>
#define SEQLIST_INIT_SIZE 8
#define INC_SIZE 3 //空間增量的大小
typedef int ElemType;
typedef struct Seqlist {
  ElemType *base;
  int capacity; //順序表容量
  int size; //表的大小
}Seqlist;
bool Inc(Seqlist *list);//增加順序表的容量
void InitSeqlist(Seqlist *list); //初始化順序表
void push_back(Seqlist *list, ElemType x); //在順序表的末尾插入元素
void push_front(Seqlist *list, ElemType x); //在順序表的頭部插入元素
void show_list(Seqlist *list); //顯示順序表中的元素
void pop_back(Seqlist *list); //刪除順序表最后一個(gè)元素
void pop_front(Seqlist *list); //刪除順序表第一個(gè)元素
void insert_pos(Seqlist *list, int pos, ElemType x);//在順序表的選定位置上插入數(shù)據(jù)
int find(Seqlist *list, ElemType key); //在順序表中查找元素key的下標(biāo)
int length(Seqlist *list);//求順序表的長(zhǎng)度
void delete_pos(Seqlist *list, int pos); //刪除順序表中特定位置的數(shù)據(jù)元素
void delete_val(Seqlist *list, int key);//刪除順序表中值為key的數(shù)據(jù)元素
void sort(Seqlist *list);//冒泡排序
void reverse(Seqlist *list);//逆置順序列表
void clear(Seqlist *list);//清除順序表中的所有元素
void destroy(Seqlist *list);//摧毀順序表
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb);//合并兩個(gè)順序列表
#endif //__SEQLIST_H__

seqlist.cpp

#include"seqlist.h"
bool Inc(Seqlist *list) {
  ElemType *newbase = (ElemType*)realloc(list, sizeof(ElemType)*(list->capacity + INC_SIZE)); //重新分配內(nèi)存空間
  if (newbase == NULL) {
    printf("內(nèi)存空間已滿,無法再分配內(nèi)存空間!\n");
    return false;
  }
  list->base = newbase;
  list->capacity += INC_SIZE;
  return true;
}
void InitSeqlist(Seqlist *list) {
  list->base = (ElemType*)malloc(sizeof(ElemType)*SEQLIST_INIT_SIZE);
  assert(list->base != NULL);
  list->capacity = SEQLIST_INIT_SIZE;
  list->size = 0;
}
void push_back(Seqlist *list, ElemType x) {
  if (list->size >= list->capacity && !Inc(list)) { //Inc(list)用來判斷增加順序表容量是否成功,只有在失敗的情況下才會(huì)進(jìn)入if語句中
    printf("順序表容量已滿,無法再在表尾繼續(xù)插入新元素!\n");
    return;
  }
  list->base[list->size] = x;
  list->size++;
}
void push_front(Seqlist *list, ElemType x) {
  if (list->size >= list->capacity && !Inc(list)) {
    printf("順序表容量已滿,無法再在表頭插入新元素!\n");
    return;
  }
  for (int i = list->size;i > 0;i--) {
    list->base[i] = list->base[i - 1];
  }
  list->base[0] = x;
  list->size++;
}
void show_list(Seqlist *list) {
  for (int i = 0;i < list->size;i++) {
    printf("%d ", list->base[i]);
  }
  printf("\n");
}
void pop_back(Seqlist *list) {
  if (list->size == 0) {
    printf("順序表已空,無法再在表尾刪除元素!\n");
    return;
  }
  list->size--;
}
void pop_front(Seqlist *list) {
  if (list->size == 0) {
    printf("順序表已空,無法再在表頭刪除元素!\n");
    return;
  }
  for (int i = 0;i < list->size - 1;i++) {
    list->base[i] = list->base[i + 1];
  }
  list->size--;
}
void insert_pos(Seqlist *list, int pos, ElemType x) {
  if (pos<0 || pos>list->size) {
    printf("插入位置不合法,無法插入元素!\n");
    return;
  }
  if (list->size >= list->capacity && !Inc(list)) {
    printf("順序表容量已滿,無法在插入新的元素!\n");
    return;
  }
  for (int i = list->size;i > pos;i--) {
    list->base[i] = list->base[i - 1];
  }
  list->base[pos] = x;
  list->size++;
}
int find(Seqlist *list, ElemType key) {
  for (int i = 0;i < list->size;i++) {
    if (list->base[i] == key)
      return i;
  }
  return -1;
}
int length(Seqlist *list) {
  return list->size;
}
void delete_pos(Seqlist *list, int pos) {
  if (pos < 0 || pos >= list->size) {
    printf("刪除位置不合法,無法刪除元素!\n");
    return;
  }
  for (int i = pos;i < list->size - 1;i++) {
    list->base[i] = list->base[i + 1];
  }
  list->size--;
}
void delete_val(Seqlist *list, int key) {
  int pos = find(list, key);
  if (pos == -1) {
    printf("順序表中沒有這個(gè)元素!\n");
    return;
  }
  delete_pos(list, pos);
}
void sort(Seqlist *list) {
  for (int i = 0;i < list->size - 1;i++) {//排序的趟數(shù)(例如5個(gè)數(shù)據(jù)需要比較4趟)
    for (int j = 0;j < list->size - 1 - i;j++) {//每一趟比較中的比較次數(shù)(例如5個(gè)數(shù)據(jù)在第0趟需要比較4次)
      if (list->base[j] > list->base[j + 1]) {
        ElemType temp = list->base[j];
        list->base[j] = list->base[j + 1];
        list->base[j + 1] = temp;
      }
    }
  }
}
void reverse(Seqlist *list) {
  if (list->size == 0 || list->size == 1) return;
  int low = 0, high = list->size - 1;
  while (low < high) {
    ElemType temp = list->base[low];
    list->base[low] = list->base[high];
    list->base[high] = temp;
    low++;
    high--;
  }
}
void clear(Seqlist *list) {
  list->size = 0;
}
void destroy(Seqlist *list) {
  free(list->base);
  list->base = NULL;
  list->capacity = 0;
  list->size = 0;
}
void merge(Seqlist *lt, Seqlist *la, Seqlist *lb) {
  lt->capacity = la->size + lb->size;
  lt->base = (ElemType*)malloc(sizeof(ElemType)*lt->capacity);
  assert(lt->base != NULL);
  int ia = 0, ib = 0, ic = 0;
  while (ia < la->size&&ib < lb->size) {
    if (la->base[ia] < lb->base[ib]) {
      lt->base[ic++] = la->base[ia++];
    }
    else {
      lt->base[ic++] = lb->base[ib++];
    }
  }
  while (ia < la->size) {
    lt->base[ic++] = la->base[ia++];
  }
  while (ib < lb->size) {
    lt->base[ic++] = lb->base[ib++];
  }
  lt->size = la->size + lb->size;
  show_list(lt);
}

main.cpp

#include"seqlist.h"
void main() {
  Seqlist list;
  InitSeqlist(&list);
  ElemType item;
  int pos;
  int select = 1;
  while (select) {
    printf("*******************************************\n");
    printf("*[1] push_back    [2] push_front  *\n");
    printf("*[3] show_list    [4] pop_back   *\n");
    printf("*[5] pop_front    [6] insert_pos  *\n");
    printf("*[7] find       [8] length    *\n");
    printf("*[9] delete_pos    [10] delete_value *\n");
    printf("*[11] sort       [12] reverse    *\n");
    printf("*[13] clear      [14] merge     *\n");
    printf("*[0] quit_system             *\n");
    printf("*******************************************\n");
    printf("請(qǐng)選擇:>>");
    scanf("%d", &select);
    if (select == 0) break;
    switch (select) {
    case 1:
      printf("請(qǐng)輸入要插入的數(shù)據(jù)(-1結(jié)束):>");
      while (scanf("%d", &item), item != -1) {//先輸入item的值,只要item不等于-1就接著循環(huán)
        push_back(&list, item);
      }
      break;
    case 2:
      printf("請(qǐng)輸入要插入的數(shù)據(jù)(-1結(jié)束):>");
      while (scanf("%d", &item), item != -1) {
        push_front(&list, item);
      }
      break;
    case 3:
      show_list(&list);
      break;
    case 4:
      pop_back(&list);
      break;
    case 5:
      pop_front(&list);
      break;
    case 6:
      printf("請(qǐng)輸入要插入的數(shù)據(jù):>");
      scanf("%d", &item);
      printf("請(qǐng)輸入要插入的位置:>");
      scanf("%d", &pos);
      insert_pos(&list, pos, item);
      break;
    case 7:
      printf("請(qǐng)輸入要查找的數(shù)據(jù):>");
      scanf("%d", &item);
      pos = find(&list, item);
      if (pos == -1)
        printf("查找的數(shù)據(jù)元素不在順序表中!\n");
      else
        printf("查找的數(shù)據(jù)元素在順序表中的下標(biāo)位置為%d\n", pos);
      break;
    case 8:
      printf("順序表的長(zhǎng)度為%d\n", length(&list));
      break;
    case 9:
      printf("請(qǐng)輸入要?jiǎng)h除數(shù)據(jù)在順序表中的下標(biāo)位置:>");
      scanf("%d", &pos);
      delete_pos(&list, pos);
      break;
    case 10:
      printf("請(qǐng)輸入要?jiǎng)h除數(shù)據(jù)的值:>");
      scanf("%d", &item);
      delete_val(&list, item);
      break;
    case 11:
      sort(&list);
      break;
    case 12:
      reverse(&list);
      break;
    case 13:
      clear(&list);
      break;
    case 14:
      Seqlist mylist, yourlist;
      ElemType item1, item2;
      InitSeqlist(&mylist);
      InitSeqlist(&yourlist);
      printf("請(qǐng)輸入順序表1中的元素值(-1結(jié)束):>");
      while (scanf("%d", &item1), item1 != -1) {
        push_back(&mylist, item1);
      }
      printf("請(qǐng)輸入順序表2中的元素值(-1結(jié)束):>");
      while (scanf("%d", &item2), item2 != -1) {
        push_back(&yourlist, item2);
      }
      merge(&list, &mylist, &yourlist);
      destroy(&mylist);
      destroy(&yourlist);
      break;
    default:
      printf("輸入的選擇錯(cuò)誤!請(qǐng)重新輸入!\n");
      break;
    }
  }
  destroy(&list);
}

希望本文所述對(duì)大家C語言程序設(shè)計(jì)有所幫助。

分享文章:C語言實(shí)現(xiàn)的順序表功能完整實(shí)例
當(dāng)前URL:http://bm7419.com/article42/igcihc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計(jì)公司、商城網(wǎng)站、小程序開發(fā)、網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化