這幾天搞連通域的問題;其中用的資料結構就是順序的數組實作的類似連結清單的操作,思想是一樣的,但他沒有寫成标準的形式,總是感覺怪怪的。根據《中國大學MOOC-陳越、何欽銘-資料結構-2017春》學習計劃,突然了解,線性表的順序存儲又分為靜态的和動态的,即初始化的方法差別,在嵌入式的系統中用靜态的,提前開辟一段記憶體較好。
/*!
* \file 線性表的順序存儲實作.cpp
*
* \author ranjiewen
* \date 三月 2017
*
*
*/
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef int Position;
typedef struct LNode *List;
#define MAXSIVE 100
struct LNode
{
ElemType Data[MAXSIVE];
Position Last; //數組的下标
};
/*初始化*/
List MakeEmpty()
{
List L;
L = (List)malloc(sizeof(struct LNode));
L->Last = -1; //初始元素位置為0
return L;
}
#define ERROR -1
Position Find(List L, ElemType x)
{
Position i = 0;
while (i<=L->Last&&L->Data[i]!=x)
{
i++;
}
if (i>L->Last)
{
return ERROR; //如果沒有找到,傳回錯誤資訊
}
else
{
return i; //找到了傳回存儲位置
}
}
/*插入*/
bool Insert(List L, ElemType X, Position P)
{ /* 在L的指定位置P前插入一個新元素X */
Position i;
if (L->Last == MAXSIVE - 1) {
/* 表空間已滿,不能插入 */
printf("表滿");
return false;
}
if (P<0 || P>L->Last + 1) { /* 檢查插入位置的合法性 */
printf("位置不合法");
return false;
}
for (i = L->Last; i >= P; i--)
L->Data[i + 1] = L->Data[i]; /* 将位置P及以後的元素順序向後移動 */
L->Data[P] = X; /* 新元素插入 */
L->Last++; /* Last仍指向最後元素 */
return true;
}
/* 删除 */
bool Delete(List L, Position P)
{ /* 從L中删除指定位置P的元素 */
Position i;
if (P<0 || P>L->Last) { /* 檢查空表及删除位置的合法性 */
printf("位置%d不存在元素", P);
return false;
}
for (i = P + 1; i <= L->Last; i++)
L->Data[i - 1] = L->Data[i]; /* 将位置P+1及以後的元素順序向前移動 */
L->Last--; /* Last仍指向最後元素 */
return true;
}
//表長操作順序存儲即數組大小
int main()
{
//List L = MakeEmpty(); //正确的初始化
//List L = NULL;//不合理的初始化
struct LNode list_; //合理的初始化
list_.Last = -1; //必須指派;
for (int i = 0; i<4;i++)
{
list_.Data[i] = 10 + i;
list_.Last++;
}
List L = &list_;
for (int i = 9; i > 0; i--)
{
Insert(L, i, 9 - i);
}
for (int i = 0; i <= L->Last;i++)
{
printf("%d ", L->Data[i]);
}
printf("\n");
return 0;
}
輸出:
9 8 7 6 5 4 3 2 1 10 11 12 13
請按任意鍵繼續. . .
鍊式存儲:
/*!
* \file 線性表的鍊式存儲實作.cpp
*
* \author ranjiewen
* \date 三月 2017
*
*
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode *PtrToNode;
typedef int ElemType;
struct LNode
{
ElemType Data;
PtrToNode Next;
};
typedef PtrToNode Position;
typedef PtrToNode List;
#define ERROR NULL
int Length(List ptrL)
{
List p = ptrL;
int j = 0;
while (p)
{
p = p->Next;
j++;
}
return j;
}
/*查找*/
Position Find(List L, ElemType x) //按值查找
{
Position p = L;/*p指向L的第1個結點*/
while (p&&p->Data!=x)
{
p = p->Next;
}
if (p)
{
return p;
}
else
{
return ERROR;
}
}
List FindKth(int K, List ptrL)//按序号查找
{
List p = ptrL;
int i = 1;
while (p!=NULL&&i<K)
{
p = p->Next;
i++;
}
if (i==K)
{
return p;
}
else
{
return NULL;
}
}
/*插入*/
bool Insert(List L, ElemType x, Position p) //在p的前一節點插入
{
Position temp, pre;
for (pre = L; pre&&pre->Next != p; pre = pre->Next); //找到p的前一個結點
if (pre == NULL)
{
printf("插入位置參數錯誤。\n");
return false;
}
else
{
temp = (Position)malloc(sizeof(struct LNode));
temp->Data = x;
temp->Next = p;
pre->Next = temp;
return true;
}
}
List Insert(ElemType x, int i, List ptrL) //按序号位置插入
{
List p, s;
if (i == 1) //新節點插入在表頭
{
s = (List)malloc(sizeof(struct LNode));
s->Data = x;
s->Next = NULL;
return s;
}
p = FindKth(i - 1, ptrL);
if (p==NULL)
{
printf("參數i錯誤");
return NULL;
}
else
{
s = (List)malloc(sizeof(struct LNode));
s->Data = x;
s->Next = p->Next;
p->Next = s;
return ptrL;
}
}
bool Delete(List L, Position p)
{
Position temp, pre;
for (pre = L; pre&&pre->Next != p; pre = pre->Next);
if (pre==NULL||p==NULL)
{
printf("删除位置參數錯誤!\n");
return false;
}
else
{
pre->Next = p->Next;
free(p);
return true;
}
}
int main()
{
List L=NULL;
for (int i = 1; i < 10;i++)
{
L=Insert(9 - i, i, L);
}
//Delete(L, );
printf("連結清單長度:%d", Length(L));
return 0;
}
C/C++基本文法學習
STL
C++ primer