(C/C++学习笔记)线性表的顺序存储

  • 线性表接口声明
#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

void SeqList_Destroy(SeqList* list);

void SeqList_Clear(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);



#endif  //__MY_SEQLIST_H__
  • 测试线性表
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"


typedef struct _Teacher
{
    char name[64];
    int age ;
    int buf;
}Teacher;

void main()
{
    int ret = 0, i = 0;
    Teacher t1, t2, t3;
    SeqList* list = NULL;

    t1.age = 10;
    t2.age = 20;
    t3.age = 30;

    list = SeqList_Create(10);


    ret = SeqList_Insert(list, (SeqListNode*) &t1, 0);
    ret = SeqList_Insert(list, (SeqListNode*) &t2, 0);
    ret = SeqList_Insert(list, (SeqListNode*) &t3, 0);

    //循环遍历
    for (i=0; ilist); i++)
    {
        Teacher *tmp = (Teacher *)SeqList_Get(list, i);
        printf("age:%d \n", tmp->age);
    }


     SeqList_Destroy(list);
    system("pause");
}

你可能感兴趣的:(C/C++)