溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言實現(xiàn)靜態(tài)順序表的實例詳解

發(fā)布時間:2020-08-19 16:26:57 來源:腳本之家 閱讀:148 作者:SuLiJuan66 欄目:編程語言

C語言實現(xiàn)靜態(tài)順序表的實例詳解

線性表

定義一張順序表也就是在內(nèi)存中開辟一段連續(xù)的存儲空間,并給它一個名字進行標識。只有定義了一個順序表,才能利用該順序表存放數(shù)據(jù)元素,也才能對該順序表進行各種操作。

接下來看看靜態(tài)的順序表,直接上代碼:

SeqList.h

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define MAX 10

typedef int DataType;

typedef struct SeqList
{
 DataType data[MAX];
 int sz;
}SeqList,*pSeqList;

void InitSeqList(pSeqList ps);
void PushBack(pSeqList ps, DataType d);
void PopBack(pSeqList ps);
void PushFront(pSeqList ps, DataType d);
void PopFront(pSeqList ps);
void Display(const pSeqList ps);
int Find(pSeqList ps, DataType d);
void Insert(pSeqList ps, DataType d, int pos);
void Remove(pSeqList ps, DataType d);
void RemoveAll(pSeqList ps, DataType d);
void Reverse(pSeqList ps);
void Sort(pSeqList ps);
int BinarySearch(pSeqList ps, DataType d);

#endif//__SEQLIST_H__

SeqList.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "SeqList.h"

void InitSeqList(pSeqList ps)
{
 assert(ps);
 ps->sz = 0;
 memset(ps->data, 0, sizeof(DataType)*MAX);
}
void PushBack(pSeqList ps, DataType d)
{
 assert(ps);
 if (ps->sz == MAX)
 {
  return;
 }
 ps->data[ps->sz] = d;
 ps->sz++;
}
void PopBack(pSeqList ps)
{
 assert(ps);
 if (ps->sz == 0)
 {
  return;
 }
 ps->sz--;
}
void PushFront(pSeqList ps, DataType d)
{
 int i = 0;
 assert(ps);
 for (i = ps->sz; i >= 1; i--)
 {
  ps->data[i] = ps->data[i - 1];
 }
 ps->data[0] = d;
 ps->sz++;
}
void PopFront(pSeqList ps)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  ps->data[i] = ps->data[i + 1];
 }
 ps->sz--;
}
void Display(const pSeqList ps)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  printf("%d ", ps->data[i]);
 }
 printf("\n");
}
int Find(pSeqList ps, DataType d)
{
 int i = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  if (ps->data[i] == d)
  {
   return i;
  }
 }
 return -1;
}
void Insert(pSeqList ps, DataType d, int pos)
{
 int i = 0;
 assert(ps);
 if (ps->sz == MAX)
 {
  return;
 }
 //方式一
 //for (i = ps->sz - 1; i >= pos; i--)
 //{
 // ps->data[i + 1] = ps->data[i];
 //}
 //方式二
 memmove(ps->data + pos + 1, ps->data + pos, sizeof(DataType)*(ps->sz - pos));
 ps->data[pos] = d;
 ps->sz++;
}
void Remove(pSeqList ps, DataType d)
{
 int i = 0;
 int pos = 0;
 assert(ps);
 pos = Find(ps, d);
 if (pos != -1)
 {
  for (i = pos; i < ps->sz; i++)
  {
   ps->data[i] = ps->data[i + 1];
  }
  ps->sz--;
 }
}
void RemoveAll(pSeqList ps, DataType d)
{
 int i = 0;
 int pos = 0;
 assert(ps);
 pos = Find(ps, d);
 while ((pos = Find(ps, d)) != -1)
 {
  for (i = pos; i < ps->sz; i++)
  {
   ps->data[i] = ps->data[i + 1];
  }
  ps->sz--;
 }
}
void Reverse(pSeqList ps)
{
 int left = 0;
 int right = ps->sz - 1;
 assert(ps);
 while (left < right)
 {
  DataType tmp = ps->data[right];
  ps->data[right] = ps->data[left];
  ps->data[left] = tmp;
  left++;
  right--;
 }
}
void Sort(pSeqList ps)
{
 int i = 0;
 int j = 0;
 assert(ps);
 for (i = 0; i < ps->sz; i++)
 {
  for (j = 0; j < ps->sz - i - 1; j++)
  {
   if (ps->data[j]>ps->data[j + 1])
   {
    DataType tmp = ps->data[j];
    ps->data[j] = ps->data[j + 1];
    ps->data[j + 1] = tmp;
   }
  }
 }
}
int BinarySearch(pSeqList ps, DataType d)
{
 int left = 0;
 int right = ps->sz - 1;
 while (left <= right)
 {
  int mid = left - ((left - right) >> 1);
  if (d > ps->data[mid])
  {
   left = mid + 1;
  }
  else if (d < ps->data[mid])
  {
   right = mid - 1;
  }
  else
  {
   return mid;
  }
 }
 return -1;
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "SeqList.h"

void test1()
{
 SeqList list;
 InitSeqList(&list);
 PushBack(&list, 1);
 PushBack(&list, 2);
 PushBack(&list, 3);
 PushBack(&list, 4);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
 PopBack(&list);
 Display(&list);
}

void test2()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 PushFront(&list, 2);
 Display(&list);
 pos = Find(&list, 3);
 printf("%d\n", list.data[pos]);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
 PopFront(&list);
 Display(&list);
}
void test3()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 PushFront(&list, 2);
 Display(&list);
 Insert(&list, 6, 2);
 Display(&list);
 Remove(&list, 1);
 Display(&list);
 RemoveAll(&list, 2);
 Display(&list);
}
void test4()
{
 int pos = 0;
 SeqList list;
 InitSeqList(&list);
 PushFront(&list, 1);
 PushFront(&list, 2);
 PushFront(&list, 3);
 PushFront(&list, 2);
 PushFront(&list, 4);
 Display(&list);
 Reverse(&list);
 Display(&list);
 Sort(&list);
 Display(&list);
 pos = BinarySearch(&list, 3);
 printf("%d\n", list.data[pos]);
}
int main()
{
 test4();
 system("pause");
 return 0;
}

動態(tài)順序表的實現(xiàn):https://www.jb51.net/article/120876.htm

 以上使用C語言動態(tài)順序表的實現(xiàn)如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI