顺序表
线性表的顺序存储是指用一组地址连续的存储单元依次存储线性表中的各个元素,使得线性表在逻辑结构上相邻的元素存储在连续的物理存储单元中,即:通过数据元素物理存储的连续性来反应元素之间逻辑上的相邻关系。采用顺序存储结构存储的线性表通常简称为顺序表。
老师的代码如下:
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
/**
* Linear list of integers. The key is data.
*/
typedef struct SequentialList {
int actualLength;
int data[LIST_MAX_LENGTH]; //The maximum length is fixed.
} *SequentialListPtr;
/**
* Output the list.
*/
void outputList(SequentialListPtr paraList) {
for(int i = 0; i < paraList->actualLength; i ++) {
printf("%d ", paraList->data[i]);
}// Of for i
printf("\r\n");
}// Of outputList
/**
* Output the memeory for the list.
*/
void outputMemory(SequentialListPtr paraListPtr) {
printf("The address of the structure: %ld\r\n", paraListPtr);
printf("The address of actualLength: %ld\r\n", ¶ListPtr->actualLength);
printf("The address of data: %ld\r\n", ¶ListPtr->data);
printf("The address of actual data: %ld\r\n", ¶ListPtr->data[0]);
printf("The address of second data: %ld\r\n", ¶ListPtr->data[1]);
}// Of outputMemory
/**
* Initialize a sequential list. No error checking for this function.
* @param paraListPtr The pointer to the list. It must be a pointer to change the list.
* @param paraValues An int array storing all elements.
*/
SequentialListPtr sequentialListInit(int paraData[], int paraLength) {
SequentialListPtr resultPtr = (SequentialListPtr)malloc(sizeof(struct SequentialList));
for (int i = 0; i < paraLength; i ++) {
resultPtr->data[i] = paraData[i];
}// Of for i
resultPtr->actualLength = paraLength;
return resultPtr;
}//Of sequentialListInit
/**
* Insert an element into a sequential linear list.
* @param paraListPtr The pointer to the list. It must be a pointer to change the list.
* @param paraPosition The position, e.g., 0 stands for inserting at the first position.
* @param paraValue The value to be inserted.
*/
void sequentialListInsert(SequentialListPtr paraListPtr, int paraPosition, int paraValue) {
// Step 1. Space check.
if (paraListPtr->actualLength >= LIST_MAX_LENGTH) {
printf("Cannot insert element: list full.\r\n");
return;
}//Of if
// Step 2. Position check.
if (paraPosition < 0) {
printf("Cannot insert element: negative position unsupported.");
return;
}//Of if
if (paraPosition > paraListPtr->actualLength) {
printf("Cannot insert element: the position %d is bigger than the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return;
}//Of if
// Step 3. Move the remaining part.
for (int i = paraListPtr->actualLength; i > paraPosition; i --) {
paraListPtr->data[i] = paraListPtr->data[i - 1];
}//Of for i
// Step 4. Insert.
paraListPtr->data[paraPosition] = paraValue;
// Step 5. Update the length.
paraListPtr->actualLength ++;
}// Of sequentialListInsert
/**
* Test the insert function.
*/
void sequentialInsertTest() {
int i;
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialInsertTest begins. ----\r\n");
// Initialize.
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// Insert to the first.
printf("Now insert to the first, the list is: ");
sequentialListInsert(tempList, 0, 8);
outputList(tempList);
// Insert to the last.
printf("Now insert to the last, the list is: ");
sequentialListInsert(tempList, 6, 9);
outputList(tempList);
// Insert beyond the tail.
printf("Now insert beyond the tail. \r\n");
sequentialListInsert(tempList, 8, 9);
printf("The list is:");
outputList(tempList);
// Insert to position 3.
for (i = 0; i < 5; i ++) {
printf("Inserting %d.\r\n", (i + 10));
sequentialListInsert(tempList, 0, (i + 10));
outputList(tempList);
}//Of for i
printf("---- sequentialInsertTest ends. ----\r\n");
}// Of sequentialInsertTest
/**
* Delete an element from a sequential linear list.
* @param paraListPtr The pointer to the list. It must be a pointer to change the list.
* @param paraPosition The position, e.g., 0 stands for inserting at the first position.
* @return The deleted value.
*/
int sequentialListDelete(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. Position check.
if (paraPosition < 0) {
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}//Of if
if (paraPosition >= paraListPtr->actualLength) {
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}//Of if
// Step 2. Move the remaining part.
int resultValue = paraListPtr->data[paraPosition];
for (int i = paraPosition; i < paraListPtr->actualLength; i ++) {
paraListPtr->data[i] = paraListPtr->data[i + 1];
}//Of for i
// Step 3. Update the length.
paraListPtr->actualLength --;
// Step 4. Return the value.
return resultValue;
}// Of sequentialListDelete
/**
* Test the delete function.
*/
void sequentialDeleteTest() {
int tempArray[5] = {3, 5, 2, 7, 4};
printf("---- sequentialDeleteTest begins. ----\r\n");
// Initialize.
SequentialListPtr tempList = sequentialListInit(tempArray, 5);
printf("After initialization, the list is: ");
outputList(tempList);
// Delete the first.
printf("Now delete the first, the list is: ");
sequentialListDelete(tempList, 0);
outputList(tempList);
// Delete to the last.
printf("Now delete the last, the list is: ");
sequentialListDelete(tempList, 3);
outputList(tempList);
// Delete the second.
printf("Now delete the second, the list is: ");
sequentialListDelete(tempList, 1);
outputList(tempList);
// Delete the second.
printf("Now delete the 5th, the list is: ");
sequentialListDelete(tempList, 5);
outputList(tempList);
// Delete the second.
printf("Now delete the (-6)th, the list is: ");
sequentialListDelete(tempList, -6);
outputList(tempList);
printf("---- sequentialDeleteTest ends. ----\r\n");
outputMemory(tempList);
}// Of sequentialDeleteTest
/**
* Locate an element in the list.
* @param paraListPtr The pointer to the list.
* @param paraValue the indicated value.
* @return The position of the value, or -1 indicating not exists
*/
int locateElement(SequentialListPtr paraListPtr, int paraValue) {
for (int i = 0; i < paraListPtr->actualLength; i ++) {
if (paraListPtr->data[i] == paraValue) {
return i;
}// Of if
}//Of for i
return -1;
}// Of locateElement
/**
* Get an element in the list.
* @param paraListPtr The pointer to the list.
* @param paraPosition The given position.
* @return The position of the value, or -1 indicating not exists
*/
int getElement(SequentialListPtr paraListPtr, int paraPosition) {
// Step 1. Position check.
if (paraPosition < 0) {
printf("Invalid position: %d.\r\n", paraPosition);
return -1;
}//Of if
if (paraPosition >= paraListPtr->actualLength) {
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, paraListPtr->actualLength);
return -1;
}//Of if
return paraListPtr->data[paraPosition];
}// Of locateElement
/**
* Clear elements in the list.
* @param paraListPtr The pointer to the list.
* @return The position of the value, or -1 indicating not exists
*/
void clearList(SequentialListPtr paraListPtr) {
paraListPtr->actualLength = 0;
}// Of clearList
/**
The entrance.
*/
void main() {
sequentialInsertTest();
sequentialDeleteTest();
}// Of main
我的代码如下:
头文件
#include <stdio.h>
#include <malloc.h>
#define LIST_MAX_LENGTH 10
定义结构体
typedef struct List {
int Length;
int data[LIST_MAX_LENGTH];
} *ListPtr;
输出顺序表数据
void OutputList(ListPtr L) {
int i=0;
for(i = 0 ; i < L->Length; i ++)
{
printf("%d ", L->data[i]);
}
if(i==0)
{
printf("the list is empty!");
}
printf("\n");
}
打印地址
void OutputMemory(ListPtr L) {
printf("The address of the structure: %ld\n", L);
printf("The address of actualLength: %ld\n", &L->Length);
printf("The address of data: %ld\n", &L->data);
printf("The address of actual data: %ld\n", &L->data[0]);
printf("The address of second data: %ld\n", &L->data[1]);
}
初始化
ListPtr ListInit(int paraData[], int paraLength) { //将数组赋值给线性表
ListPtr resultPtr = (ListPtr)malloc(sizeof(struct List));
int i;
for (i = 0; i < paraLength; i ++) {
resultPtr->data[i] = paraData[i];
}
resultPtr->Length = paraLength;
printf("\n");
return resultPtr;
}
在顺序表中插入元素
void ListInsert(ListPtr L, int paraPosition, int paraValue) {
//判断空间是否已满
if (L->Length >= LIST_MAX_LENGTH)
{
printf("Cannot insert element: list is full\n");
return;
}
// 判断插入位置是否合法
if (paraPosition < 0||paraPosition > L->Length)
{
printf("Cannot insert element: the position %d is bigger than the list length %d.\n", paraPosition, L->Length);
return;
}
//移动数据
int i;
for (i = L->Length; i > paraPosition; i --)
{
L->data[i] = L->data[i - 1];
}
//插入
L->data[paraPosition] = paraValue;
//更新长度
L->Length ++;
}
测试插入功能
void InsertTest() {
int i;
int tempArray[5] = {5,0,3,7,6};
printf("---- sequentialInsertTest begins. ----\n");
//创建线性表
ListPtr tempList = ListInit(tempArray, 5);
printf("After initialization, the list is: ");
OutputList(tempList);
//在首位插入一个元素
printf("Now insert to the first, the list is: ");
ListInsert(tempList, 0, 9);
OutputList(tempList);
// 在末尾插入一个元素
printf("Now insert to the last, the list is: ");
ListInsert(tempList, 6, 4);
OutputList(tempList);
//在超越尾部插入一个元素
printf("Now insert beyond the tail. \r\n");
ListInsert(tempList, 9,8 );
printf("The list is:");
OutputList(tempList);
// 连续插入元素
for (i = 0; i < 5; i ++) {
printf("Inserting %d\n", (i + 10));
ListInsert(tempList, 0, (i + 10));
OutputList(tempList);
}
printf("---- sequentialInsertTest ends. ----\n");
printf("\n");
}
从顺序表表中删除元素
int ListDelete(ListPtr L, int paraPosition) {
//判断位置是否合法
if (paraPosition < 0||paraPosition >= L->Length)
{
printf("Cannot delete element: the position %d is beyond the list length %d.\r\n", paraPosition, L->Length);
return -1;
}
//移动数据
int resultValue = L->data[paraPosition];
int i;
for (i = paraPosition; i < L->Length; i ++)
{
L->data[i] = L->data[i + 1];
}
//更新长度
L->Length --;
//返回的值
return resultValue;
}
测试删除功能
void DeleteTest() {
int tempArray[5] = {5,0,3,7,6};
printf("---- sequentialDeleteTest begins. ----\n");
ListPtr tempList = ListInit(tempArray, 5);
printf("After initialization, the list is: ");
OutputList(tempList);
//删除第一个元素
printf("Now delete the first, the list is: ");
ListDelete(tempList, 0);
OutputList(tempList);
//删除最后一个元素
printf("Now delete the last, the list is: ");
ListDelete(tempList, 3);
OutputList(tempList);
//删除第四个元素
printf("Now delete the second, the list is: ");
ListDelete(tempList, 3);
OutputList(tempList);
//删负
printf("Now delete the (-6)th, the list is: ");
ListDelete(tempList, -6);
OutputList(tempList);
printf("---- sequentialDeleteTest ends. ----\n");
OutputMemory(tempList);
}
在顺序表里找一个元素
int locateElement(ListPtr L, int paraValue) {
int i;
for(i = 0; i < L->Length; i ++)
{
if (L->data[i] == paraValue)
return i;
}
return -1;
}
在顺序表中获取一个元素
int getElement(ListPtr L, int paraPosition) {
//判断位置是否合法
if (paraPosition < 0||paraPosition >= L->Length)
{
printf("Cannot delete element: the position is beyond the list.\r\n");
return -1;
}
//返回值
return L->data[paraPosition];
}
清除列表中的元素
void clearList(ListPtr L) {
L->Length = 0;
}
int main() {
InsertTest();
DeleteTest();
}
运行结果如下:
---- sequentialInsertTest begins. ----
After initialization, the list is: 5 0 3 7 6
Now insert to the first, the list is: 9 5 0 3 7 6
Now insert to the last, the list is: 9 5 0 3 7 6 4
Now insert beyond the tail.
Cannot insert element: the position 9 is bigger than the list length 7.
The list is:9 5 0 3 7 6 4
Inserting 10
10 9 5 0 3 7 6 4
Inserting 11
11 10 9 5 0 3 7 6 4
Inserting 12
12 11 10 9 5 0 3 7 6 4
Inserting 13
Cannot insert element: list is full
12 11 10 9 5 0 3 7 6 4
Inserting 14
Cannot insert element: list is full
12 11 10 9 5 0 3 7 6 4
---- sequentialInsertTest ends. ----
---- sequentialDeleteTest begins. ----
After initialization, the list is: 5 0 3 7 6
Now delete the first, the list is: 0 3 7 6
Now delete the last, the list is: 0 3 7
Now delete the second, the list is: Cannot delete element: the position 3 is beyond the list length 3.
0 3 7
Now delete the (-6)th, the list is: Cannot delete element: the position -6 is beyond the list length 3.
0 3 7
---- sequentialDeleteTest ends. ----
The address of the structure: 1643536
The address of actualLength: 1643536
The address of data: 1643540
The address of actual data: 1643540
The address of second data: 1643544
--------------------------------
Process exited after 0.04188 seconds with return value 37