c语言顺序表实现

  顺序表是程序员在编程中会经常用到的数据结构,简单来说,顺表就是用数组来存储

数据和数据与数据之间的联系。

 1 #pragma once
 2 #include<stdio.h>
 3 #include<malloc.h>
 4 
 5 typedef int ElemType; //假定线性表的元素类型为整型
 6 #define LIST_SIZE 1024 //假定线性表长度是1024
 7 #define TRUE 1
 8 #define FALSE 0
 9 typedef struct {
10     ElemType data[LIST_SIZE];
11     int last; //指向最后一个节点的位置
12 }SequenList;
13 
14 SequenList *lptr;
sequenceList.h
  1 #include "sequece.h"
  2 #include <iostream>
  3 using namespace std;
  4 
  5 //初始化
  6 SequenList *InitSeq(void) {
  7     SequenList *pList;
  8     pList =(SequenList*) malloc(sizeof(SequenList));
  9     if (pList!=NULL) {
 10         pList->last = 0;//初始化成功,且长度为0
 11     }
 12     return pList;
 13 }
 14 
 15 
 16 //求长度:求线性表中的元素个数
 17 int GetSizeSeq(SequenList* pList) {
 18     return pList->last;
 19 }
 20 
 21 //取元素:取给定位置的元素值
 22 //pList:目标元素顺序表,获取元素的下标:pos, e:将元素值放入
 23 int GetElemSqList(SequenList*pList, int pos, ElemType *e) {
 24     if (pos<0 || pos>pList->last) {
 25         return FALSE;
 26     }
 27     if (pList->last <= 0) {
 28         return FALSE;
 29     }
 30     //说明此时pos在0--n之间
 31     *e = pList->data[pos];
 32     return TRUE;
 33 }
 34 
 35 //查元素:查找给定元素的位置
 36 //相同值取第一个
 37 //返回值:-1表示没有找到,否则返回带查找元素的角标
 38 //pList:传入的数组顺序表,key比对值
 39 int LocateElemSeqList(SequenList*pList, ElemType key) {
 40     for (int i = 0; i < pList->last; ++i) {
 41         if (pList->data[i] == key) {
 42             return i;
 43         }
 44     }
 45     return -1;
 46 }
 47 
 48 //插入元素:在指定位置插入元素
 49 //插入的位置为K:k:0--n-1
 50 //顺序表:不满
 51 //pList:目标顺序表,x待插入的元素,k插入的位置
 52 int  InsertElemSqList(SequenList*pList, ElemType x, int k) {
 53 
 54     //顺序表尚未填满
 55     if (pList->last >= LIST_SIZE - 1)
 56     {
 57         return FALSE;
 58     }
 59     //表明K是有效位置
 60     if (k<0 || k>(pList->last+1)) {
 61         return FALSE;
 62     }
 63     for (int j = pList->last; j >= k; j--) {
 64         pList->data[j + 1] = pList->data[j];
 65     }
 66     pList->data[k] = x;
 67     pList->last++;
 68     return TRUE;
 69 }
 70 
 71 //删除:删除指定位置的元素或者删除给定的值
 72 //pList:目标数组,k表示需要删除的位置
 73 int DelElemSeqList(SequenList*pList, int k) {
 74     if ((k >= 0 && k <= pList->last-1) && (pList->last != 0)) {
 75         for (int j = k; j <= pList->last-1; j++) {
 76             pList->data[j] = pList->data[j + 1];
 77         }
 78         pList->last--;
 79         return TRUE;
 80     }
 81     return FALSE;
 82 }
 83 
 84 //遍历元素:从头到尾
 85 void showSeqList(SequenList*pList) {
 86     for (int i = 0; i < pList->last; ++i) {
 87         printf("%d ", pList->data[i]);
 88     }
 89 }
 90 
 91 int main()
 92 {
 93     lptr = InitSeq(); 
 94     if (lptr) {
 95         //todo:继续使用这个顺序表
 96         for (int i = 0; i < 10; ++i) {
 97             InsertElemSqList(lptr,i,i);
 98 
 99         }
100         cout << endl;
101         cout << "length=" << GetSizeSeq(lptr) << endl;
102         printf("************\n");
103         showSeqList(lptr);
104         DelElemSeqList(lptr, 9);
105         printf("************\n");
106         showSeqList(lptr);
107         cout << endl;
108         cout << "length=" << GetSizeSeq(lptr) << endl;
109         int pos = LocateElemSeqList(lptr, 6);
110         cout << "元素6位置" << pos << endl;
111     }
112     else {
113         //to do
114     }
115 
116 }
main.c

 

posted @ 2020-05-05 10:09  flyingswallow  阅读(282)  评论(0编辑  收藏  举报