算法學習----線性表

線性表

線性表是最基本的、最簡單的、也是最常見的數據結構。線性表是n個具有相同特性的數據元素的有限序列

線性表的特徵:(非空)

  • 有且僅有一個開始結點a1,沒有直接前趨結點,有且僅有一個直接後繼結點a2
  • 有且僅有一個終結結點an,沒有直接後繼結點,有且僅有一個直接前趨結點an-1
  • 其餘內部結點ai(2<= i <= n-1),都有且僅有一個前趨結點和一個直接後繼結點
  • 對於同一線性表,各數據元素a必須具有相同的數據類型,且數據元素長度相同

線性表的兩種存儲方法分別爲:順序存儲和鏈式存儲

順序表

順序表是在一塊內存空間連續存儲的,只需知道首地址和數據元素的長度,即可定位到數據位置

順序表的基本操作

  1. 初始化數據表
  2. 計算數據表的長度
  3. 插入結點
  4. 增加結點
  5. 刪除結點
  6. 查找結點
  7. 顯示所有結點

順序表操作實例代碼如下:

#include <iostream>
using namespace std;
#define MAXSIZE 100

typedef struct {
	int num;
}Data;

typedef struct {
	Data ListData[MAXSIZE];
	int length;
} List;

void InitList(List *l)
{
	l->length = 0;
}

int LenList(List *l)
{
	return l->length;
}

void InsertList(List *l, int n,int value)
{
	int i, num = 0;
	if (n >= l->length || n < 0)
		cout << "插入位置不合法!" << endl;
	else
	{
		for (i = l->length - 1; i >= n - 1; i--)
		{
			l->ListData[i+1].num = l->ListData[i].num;
		}

		l->ListData[n-1].num = value;
		l->length++;
	}
}

void AddList(List *l,int value)
{
	l->ListData[l->length].num = value;
	l->length++;
}

void DelList(List *l, int n)
{
	int i;
	if (n >= l->length || n < 0)
		cout << "刪除位置不合法!" << endl;
	else
	{
		for (i = n - 1; i < l->length; i++)
		{
			l->ListData[i].num = l->ListData[i + 1].num;
		}
		l->length--;
	}
}

int SelectList(List *l, int n)
{
	if (n >= l->length || n < 0)
		cout << "查詢位置不合法!" << endl;
	else
	{
		return  l->ListData[n].num;
	}
}

void ShowList(List *l)
{
	for (int i = 0; i < l->length; i++)
	{
		cout << l->ListData[i].num << "\t";
	}
	cout << endl;
}

int main()
{
	List *l = new List;
	l->length = 10;
	int i;
	for (i = 0; i < l->length; i++)
	{
		l->ListData[i].num = i + 1;
	}
	ShowList(l);
	cout << LenList(l) << endl;

	AddList(l,101);
	ShowList(l);

	InsertList(l, 5, 999);
	ShowList(l);

	DelList(l, 6);
	ShowList(l);

	InitList(l);
	cout << LenList(l) << endl;

    system("pause");
	return 0;
}

程序執行結果如下:
在這裏插入圖片描述

順序表結構的缺陷性:

  • 在插入或刪除結點時,往往需要移動大量的數據
  • 當數據表比較大時,往往因爲無法申請到足夠的連續空間而無法存儲數據

鏈表

鏈表結構是一種動態存儲分配的結構形式,可以動態申請所需的內存單元

鏈表的結構

鏈表的結構包括兩部分:數據、地址

鏈表的一般結構包括頭指針、中間結點、尾指針,其中每個結點的地址部分總是指向下一結點

鏈表的基本操作

  1. 追加結點
  2. 插入結點
  3. 刪除結點
  4. 計算鏈表長度
  5. 展示所有結點

單鏈表操作實例如下:

#include <iostream>
using namespace std;

typedef struct Node
{
	int num;
	struct Node *next;
}List;

List *createList()
{
	List *head, *tail, *p;
	head = tail = p = new List;

	cout << "請輸入鏈表數據,以'0'結束:" << endl;
	cin >> p->num;
	while (p->num != 0)
	{
		tail->next = p;
		tail = p;

		p = new List;
		cin >> p->num;
	}
	tail->next = NULL;
	return head;
}

void delList(List *l, int value)
{
	List *p = l;
	while (p->next->num != value)
	{
		p = p->next;
	}
	p->next = p->next->next;
}

void insertList(List *l,int  num,int value)
{
	int i;
	List *p, *q;
	q = new List;
	p = l;
	for (i = 1; i < num-1; i++)
	{
		p = p->next;
	}
	q->num = value;
	q->next = p->next;
	p->next = q;
}

void editList(List *l,int num, int value)
{
	int i;
	List *p;
	p = l;
	for (i = 1; i < num ; i++)
	{
		p = p->next;
	}
	p->num = value;
}

int lenList(List *l)
{
	int len = 0;
	while (l != NULL)
	{
		len++;
		l = l->next;
	}
	return len;
}

void selectList(List *l,int num)
{
	int i;
	List *p;
	p = l;
	for (i = 1; i < num; i++)
	{
		p = p->next;
	}
	cout << "第"<< num <<"個數據爲:"<< p->num << endl;
}

void showList(List *l)
{
	List *p;
	p = l;
	while(p != NULL)
	{
		cout << p->num << " ";
		p = p->next;
	}
	cout << endl;
}

int main()
{
	List *l;
	l = createList();
	showList(l);

	delList(l,2);
	insertList(l,3,9);
	showList(l);

	editList(l,3,8);
	showList(l);

	selectList(l,3);
	cout << "鏈表長度爲:" << lenList(l) << endl;
	system("pause");
    return 0;
}

程序運行結果如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章