# Abstract data description of linear table:

### Operation set: linear table L ∈ List, integer i represents position, and element X ∈ ElementType. The basic operations of linear table mainly include:

• 1 List MakeEmpty(): initializes an empty linear table L;
• 2 ElementType findkth (int K, list L): returns corresponding elements according to bit order K;
• 3 int Find(ElementType X, List L): find the first occurrence position of X in the linear table L;
• 4 void Insert(ElementType X, int i, List L): insert a new element X before bit order I;
• 5 void Delete(int i, LIst L): delete the elements of the location order I;
• 6 int Length(List L): returns the length n of linear table L;

# Sequential storage implementation of linear table:

#### Use the continuous storage space of the array to store the elements of the linear table in sequence

```typedef struct LNode *List;
struct LNode
{
ElementType Data[MAXSIZE];
int Last;
};
struct LNode L;
List PtrL;
```

## Implementation of main operations:

### 1. Initialization (create an empty sequence table)

#### The code is as follows:

```List MakeEmpty()
{
List PtrL;
PtrL = (List)malloc(sizeof(struct LNode));
PtrL->Last = -1;
return PtrL;
}
```

### 2. Find

#### The code is as follows:

```int Find(ElementType X, List PtrL)
{
int i = 0;
while (i <= PtrL->Last && PtrL->Data[i] != X)
{
i++;
}
if (i > PtrL->Last) return -1;
else return i;
}
```

## Insert (insert a new element with value X at position I (1 < = I < = n + 1)) -Pay attention to the following move first!!!

### 3. Insert operation implementation

#### The code is as follows:

```void Insert(ElementType X, int i, List PtrL)
{
// Note that the i passed in is a position, not an array subscript
int j;
if (PtrL->Last == MAXSIZE-1)	//The tablespace is full and cannot be inserted
{
printf("Table full");
return;
}
if (i < 1 || i > PtrL->Last+2)	//Check the legitimacy of the insertion position
{
printf("Illegal location");
}
for (j = PtrL->Last;j >= i-1; j++)
{										//[] denotes subscript
PtrL->Data[j + 1] = PtrL->Data[j];	//Move a[i+1]~a[n] backward in reverse order
}
PtrL->Data[i - 1] = X;				//New element insertion
PtrL->Last++;						//Last still points to the last element
return;
}
```

## Delete (delete the element at position I (1 < = I < = n + 1)) of the table)

### 4. Delete operation implementation

#### The code is as follows:

```void Delete(int i,List PtrL)
{
int j;
if(i < 1 || PtrL->Last+1)		//Check the validity of empty tables and deleted locations
{
printf("Does not exist%d Elements",i);
return;
}
for(j=i;j <= PtrL->Last;j++)
{										//[] denotes subscript
PtrL->Data[j-1] = PtrL->Data[j];	/*Move a[i+1]~a[n] forward in sequence*/
}
PtrL->Last--							//Last still points to the last element
}
```

# Chain storage implementation of linear list:

#### Two logically adjacent elements are not required to be physically adjacent, and the logical relationship between data elements is established through "chain".

```typedef struct LNode*List;
struct LNode{
ElementType Data;
List Next;
};

struct LNode L;
List PtrL;
```

## Implementation of main operations:

### 1. Calculate meter length

#### The code is as follows:

```int Length(List PtrL)
{
List p = PtrL;	//p points to the first node of the table
int j = 0;
while(p)
{
p=p->next;
j++;		//The current p points to the j-th node
}
return j;
}
```

### 2. Find

#### The code is as follows:

```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;
//Find the K-th and return the pointer
else return NULL;
//Otherwise, it returns null
}
```

#### The code is as follows:

```List Find(ElementType X, List PtrL)
{
List p = PtrL;
while(p!=NULL && p->Data!=X)
{
p=p->next;
}
return p;
}
```

### 3. Insert (insert a new node with a value of X after the I-1 (1 < = I < = n + 1)) node)

#### Basic ideas

(1) First, construct a new node and point it with s;
(2) Then find the i-1 node of the linked list and point to it with p;
(3) Then modify the pointer and insert the node (the new node inserted after p is s)
tips: major sequencing issues

#### The code is as follows:

```List Insert(ElementType X, int i,List PtrL)
{
List p,s;
if(i == 1)						//Insert new node in header
{
s = (List)malloc(sizeof(struct LNode));	//Application and filling node
s->Data = X;
s->next = PtrL;
return s;					//Return new header pointer
}
p = FindKth(i-1,PtrL)	//Find node i-1
if(p == NULL)				//The i-1 does not exist and cannot be inserted
{
printf("parameter i wrong")；
return NULL;
}else{
s = (List)malloc(sizeof(struct LNode)) //Application and filling node
s->Data = X;
s->Next = p->Next;	//The new node is inserted after the i-1 node
p->Next = s;
return PtrL;
}
}
```

### 4. Delete (delete the node at position I (1 < = I < = n + 1)) in the linked list)

#### Basic ideas

(1) First find the i-1 node of the linked list and point to it with p;
(2) Then the pointer s points to the node to be deleted (the next node of p);
(3) Then modify the pointer and delete the node referred to by s

#### The code is as follows:

```List Delete(int i, List PtrL)
{
List p,s;
if(i == 1)		//To delete is the first node of the table
{
s = PtrL;		//s points to the first node
if(PtrL!=NULL)PtrL = PtrL->Next;	//Delete from linked list
else return NULL;
free(s);								//Release deleted node
return PtrL;
}
p = FindKth(i-1,PtrL);			//Find node i-1
if(p == NULL)
{
printf("The first%d Nodes do not exist",i-1);
return NULL;
}
else if(p->Next == NULL)
{
printf("The first%d Nodes do not exist",i);
return NULL;
}
else
{
s = p->Next;					//s points to the ith node
p->Next = s->Next;			//Delete from linked list
free(s);							//Release deleted node
return PtrL;
}
}
```

Tags: data structure

Posted on Wed, 29 Sep 2021 15:09:26 -0400 by roy