一、線性表可分爲順序表和鏈表
順序表:邏輯相鄰,物理也相鄰
鏈表:邏輯相鄰但物理可能不相鄰
在seqlist.h這個頭文件裏:
#pragma once //防止頭文件被重複引用
//固定長度的順序表
#define SIZE 10 //順序表的長度
typedef struct SeqList
{
int elem[SIZE];//存放數據的數組
int length;//有效數據的個數
}SeqList,*PSeqList;
接着,在seqlist.cpp這個文件裏:
#include <assert.h>
#include "seqlist.h" //一定要記得引用外來的這個
{
assert(ps != NULL);
if(ps == NULL)
{
return ;
}
}
{
return ps->length == SIZE;
}
{
if(pos<0 || pos>ps->length || IsFull(ps))//判斷不合法的pos值
{
return false;
}
{
ps->elem[i+1] = ps->elem[i];
}
ps->elem[pos] = val; //將新的值插入
return true;
}
{
for(int i=0;i<ps->length;i++)
{
if(ps->elem[i] == key)
{
return i;
}
}
return -1;
}
{
int index = Search(ps,key);
if(index < 0)
{
return false;
}
/*for(int i=index;i<ps->length-1;i++)
{
ps->elem[i] = ps->elem[i+1];
}
return DeletePos(ps,index);
}
{
if(pos<0 || pos>=ps->length)//
{
return false;
}
{
ps->elem[i] = ps->elem[i+1];
}
}
{
return ps->length;
}
{
return ps->length == 0;
}
{
ps->length = 0; //(*ps).length = 0;,[],->
}
{
Clear(ps);
}
{
for(int i=0;i<ps->length;i++)
{
printf("%d ",ps->elem[i]);
}
printf("\n");
}
bool GetElem(PSeqList ps,int pos,int *rtval)
{
if(pos<0 || pos>=ps->length)
{
return false;
}
*rtval = ps->elem[pos];
}
測試用例:
int main()
{
Node head;//
InitList(&head);
for(int i=0;i<16;i++)
{
//Insert_head(&head,i);
Insert_tail(&head,i);//0 1 2 3 4 5
}
Insert_head(&head,1);
Insert_head(&head,2);
Insert_head(&head,3);
Insert_head(&head,4);
Show(&head);
//Reverse(&head);
//Show(&head);
Unique(&head);
Show(&head);
/*int val;
GetElem(&head,3,&val);
printf("%d\n",val);*/
Destroy(&head);
Destroy(&head);
return 0;
}
/*
#include <string.h>
int main()
{
printf("");
strlen("abc");
return 0;
}
*/
/*
struct Node
{
int data;
int *next;
};
int main()
{
Node na = {1,NULL};//
Node nb = {2,NULL};
Node nc = {3};
na.next = &nb.data;//int *,nb
na.next
return 0;
}
*/
#include "dseqlist.h"
int main()
{
DSeqList ds;//12
InitSeqList(&ds);
for(int i=0;i<15;i++)
{
Insert(&ds,i,i);
}
Show(&ds);
return 0;
}
/*
#include "seqlist.h"
int main()
{
SeqList s;//44
InitSeqList(&s);
for(int i=0;i<15;i++)
{
Insert(&s,i,i);
}
Show(&s);
return 0;
}
*/