Learning notes of Chapter 3 of Dahua data structure -- linear table

Posted by Shroder01 on Mon, 17 Jan 2022 12:07:00 +0100

Definition of linear table

Linear table: a finite sequence of zero or more data elements.

The number n of linear table elements is defined as the length of the linear table. When n is 0, it is an empty table.

In a complex linear table, a data element can be composed of several data items.

Storage structure of linear table

Sequential storage structure

It can be implemented with one-dimensional array in C language, and the type of each data element is the same.

Three attributes are required to describe the sequential storage structure:

1. Starting location of storage space: array date. Its storage location is the storage location of storage space..

2. Maximum storage capacity of linear table: array length maxsize.

3. Current length of linear table: length

Difference between data length and linear table length

Length of array: the length of the storage space for storing the linear table. This amount is generally unchanged after storage allocation.

Length of linear table: it is the number of data elements in the linear table. This quantity changes with the insertion and deletion of the linear table.

At any time, the length of the linear table should be less than the length of the array.

Address calculation method

Each storage unit in the memory has its own number, which is called the address.

The calculation formula is:

LOC(a)=LOC(a1)+(i-1)*c;

Random access structure: the storage or retrieval of data at each linear table location is the same time for the computer, that is, a constant.

Insertion and deletion of sequential storage structure

Similar to array, that is, find the position to be inserted or deleted, move all the elements behind the position back or forward, and then add or subtract one from the table length.

Advantages and disadvantages of linear table sequential storage structure

advantage:

1. There is no need to add additional storage space to represent the logical relationship between elements in the table

2. You can quickly access elements at any position in the table

Disadvantages:

1. Inserting and deleting elements requires moving a large number of elements

2. When the length of the linear table changes greatly, it is difficult to determine the capacity of the storage space

3. "Debris" causing storage space

Linked Storage Structure

definition

n nodes are linked into a linked list, which is the linked storage structure of linear list.

characteristic

The chain storage structure of linear table is characterized by using a group of arbitrary storage units to store the data elements of linear table, which can be continuous or discontinuous. This means that these data elements can exist anywhere in memory that is not occupied.

In the chain structure, each data element should store not only the data element information (data field), but also the storage address (pointer field) of its subsequent elements.

Similarities and differences between header pointer and header node

Head node: in order to facilitate the operation of the linked list, a node is attached before the first node of the single linked list, which is called the head node.

Head pointer: the storage location of a node in the linked list is called head pointer.

Header pointer:

1. The head pointer is the pointer to the first node of the linked list. If the linked list has a head node, it is the pointer to the head node.

2. The head pointer has the function of marking, so the head pointer is often labeled with the name of the linked list

3. No matter the linked list is not empty, the header pointer is not empty. The header pointer is a necessary element of the linked list

Header node:

1. The head node is to facilitate the operation of the linked list. A node is attached before the first node of the single linked list. Its data field is generally meaningless and can also store the length of the linked list

2. With the header node, the operations of inserting and deleting the first node before the first element node are unified with those of other nodes.

3. The head node is not necessarily a necessary element of the linked list.

Single linked list

Read, insert and delete single linked list

read

/* Initial condition: linked linear table l already exists, 1 ≤ i ≤ ListLength(L) */
/* Operation result: use e to return the value of the ith data element in L */
Status GetElem(LinkList L,int i,ElemType *e)
{
	int j;
	LinkList p;		/* Declare a node p */
	p = L->next;		/* Let p point to the first node of the linked list L */
	j = 1;		/*  j For counter */
	while (p && j<i)  /* p When it is not empty or the counter j is not equal to i, the loop continues */
	{   
		p = p->next;  /* Let p point to the next node */
		++j;
	}
	if ( !p || j>i ) 
		return ERROR;  /*  The ith element does not exist */
	*e = p->data;   /*  Take the data of the i th element */
	return OK;
}

Insert

 

/* Initial condition: linked linear table l already exists, 1 ≤ i ≤ ListLength(L), */
/* Operation result: insert a new data element e before the ith position in L, and add 1 to the length of L */
Status ListInsert(LinkList *L,int i,ElemType e)
{ 
	int j;
	LinkList p,s;
	p = *L;   
	j = 1;
	while (p && j < i)     /* Find the ith node */
	{
		p = p->next;
		++j;
	} 
	if (!p || j > i) 
		return ERROR;   /* The ith element does not exist */
	s = (LinkList)malloc(sizeof(Node));  /*  Generate a new node (C language standard function) */
	s->data = e;  
	s->next = p->next;      /* Assign the successor node of p to the successor node of s  */
	p->next = s;          /* Assign s to the successor of p */
	return OK;
}

delete

 

/* Initial condition: linked linear table l already exists, 1 ≤ i ≤ ListLength(L) */
/* Operation result: delete the ith data element of L and return its value with e. the length of L is reduced by 1 */
Status ListDelete(LinkList *L,int i,ElemType *e) 
{ 
	int j;
	LinkList p,q;
	p = *L;
	j = 1;
	while (p->next && j < i)	/* Traverse to find the ith element */
	{
        p = p->next;
        ++j;
	}
	if (!(p->next) || j > i) 
	    return ERROR;           /* The ith element does not exist */
	q = p->next;
	p->next = q->next;			/* Assign the successor of q to the successor of p */
	*e = q->data;               /* Give the data in the q node to e */
	free(q);                    /* Let the system reclaim this node to free up memory */
	return OK;
}

Creation and deletion of the whole single linked list

establish

Head interpolation, that is, let the new node always be in the first position

/*  Randomly generate the values of n elements and establish a single chain linear table L with header node (header interpolation) */
void CreateListHead(LinkList *L, int n) 
{
	LinkList p;
	int i;
	srand(time(0));                         /* Initialize random number seed */
	*L = (LinkList)malloc(sizeof(Node));
	(*L)->next = NULL;                      /*  First establish a single linked list of leading nodes */
	for (i=0; i<n; i++) 
	{
		p = (LinkList)malloc(sizeof(Node)); /*  Generate new node */
		p->data = rand()%100+1;             /*  Randomly generate numbers within 100 */
		p->next = (*L)->next;    
		(*L)->next = p;						/*  Insert into header */
	}
}

Tail interpolation method, put the new node behind the terminal node

void creak(struct node **p,int n)
{
    struct node *p1,*p2;
    *p=(struct node *)malloc(sizeof(struct node));
    p1=*p;
    for(int i=0; i<n; i++)
    {
        p2=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&(p2->date));
        p1->next=p2;
        p1=p2;
    }
    p1->next=NULL;
}

delete

Status clearlist(LinkList *L)
{
   LinkList p,q;
   p=(*L)->next;
   while(p)
   {
      q=p->next;
      free(p);
      p=q;
   }
  (*L)->next=NULL;
   return ok;
}

Advantages and disadvantages of single linked list structure and sequential storage structure

If the linear table needs frequent lookup and few insert and delete operations, the sequential storage structure is suitable.

When the number of elements in a linear table changes greatly or you don't know how big it is, it's best to use a single linked list structure

Topics: Algorithm data structure linked list