Linear table - Table sequence - singly linked list (IV)

Before talking about the list, tell a little story:
Long, long ago, I bought from the market a husky, well-known, husky, doing nothing is not commonly known, in order to let him run away, I need a chain leash to it, but I have to go home, how to do it? I need a do-it-yourself, a chain made of? Of course, losing is a small chain, with the way you must have seen, each section has a small port for connecting the next section, so I do a small chain of steadily, and a one all together, then, we also need to determine which end is who took it, which side is the dog sets, so I made a rope connected to the chain for some of them. Later, two of Kazakhstan to bring me a 20-million renovation plan ,,,,,, Well, the story finished. With Photo! !

Here Insert Picture Description
Before the question, please remember chains, small chains, rope three words and the corresponding list of nouns.

word List the corresponding nouns
chain List
Small chains node
rope Head node

Now Laijiangjiang way linked list, first of all, the different lists and arrays, the memory address of the array elements is continuous, and the memory element address in the list is random. Since the data type of a linked list of nodes is the same and is a random storage, so we did not like array only needs to know the head node can access the entire list, we have to address the next node stored in each node, like dog chain in the hole in each section, the next section is used to connect the chain, the only way that we can access a one entire list.
Array is the use of memory, the memory addresses are sequential

Here Insert Picture Description
The list is the use of memory, all they have to have a pointer to the next node
Here Insert Picture Description
See here, you should have a little understanding of the list it, so here we come to define a list of it.

1. Define list

 typedef struct list
 {
  int x;
  struct list * next;
 }list;

Think about the definition of the list we have the equivalent of a small chain of each section of the mold, then, we should make a small chain and rope.

2. The head node initialization

list * InitListHead() //初始化 
{
 list *Phead = (list *)malloc(sizeof(list));//创建头结点
 Phead->x = 0;  //该变量可存放该链表长度
 Phead->next = NULL;
 return Phead;       //创建后返回该头指针
}

Data can be stored, such as the head node chain length and so on, then, we just need to create duplicate nodes, this is the list, here to note that the one-way linked list including the tail and head into the insert, the following will explain separately.

3. Head interpolation

void HeadList(list * L,int e)//头插法
{
 list * Pbady = (list *)malloc(sizeof(list));//创建一个节点
 Pbady->x = e;
 Pbady->next = NULL;
 if (L->next == NULL)
 {
  L->next=Pbady;
 }
 else
 {
  Pbady->next = L->next;
  L->next = Pbady;
 }
 L->x++;
}
}

If you look at the code a little stiff, then we use diagram to illustrate.
Here Insert Picture Description
Easy to see, using the first interpolation data is in reverse order, but later the top surface, one of which is the difference between head and tail of the plug is inserted.

4. Tail interpolation

void TrailList(list * LA,list ** LB,int e)//尾插法
{
  list *Pbady = (list*)malloc(sizeof(list));
  Pbady->x = e;
  if (LA->next == NULL)
  {
   LA->next = Pbady;
  }
  else
  {
   (*LB)->next = Pbady;
  }
  Pbady->next = NULL;
  *LB = Pbady;
  LA->x++;
}

End plug head wears interpolation method for one more pointer, if the code looking ignorant, that we are still on the drawing is.
Here Insert Picture Description
Unlike head tail interpolation interpolation method, just at the beginning of the insertion can be, but the end is inserted need to traverse the entire list and find the last, if the list is long enough to traverse it again every time, obviously unscientific, so we expect to use a pointer keep records of the location of the last node, and points to the last node, so that whenever we have a new node, you can just plug in the back of the pointer. And update the pointer as to why the use of secondary pointer to view this article.
Parameter pointers are some of the problems of
the following basic operations look at the list.

5. Find content by serial number

int NumFindList(list *L,int e)//按序号查找
{
 if (e <= 0)
 {
  return -1;
 }
 int i = 0;
 int j = L->x;
 list * P = L;
 while (P != NULL&&i <= j)
 {
  if (i == e)
  {
   printf("该内容为%d\n", P->x);
   break;
  }
  P = P->next;
  i++;
 }
}

6. Find by value

void ValueFindList(list * L,int e)//按值查找
{
 int i = 0;
 int j = L->x;
 list * P = L;
 while (P != NULL&&i <= j)
 {
  if (P->x == e)
  {
   printf("已找到\n");
  }
  P = P->next;
  i++;
 }
}

7. seeking chain length

int LengthList(list * L)//链表长度
{
 return L->x;//这里我们只需要访问头节点中的数据即可
}

8. insert

void InsertList(list * L,int i,int e)//插入操作
{
 if (i > L->x)
 {
  printf("插入位置不合法\n");
  return ;
 }
 list * Pbady = (list *)malloc(sizeof(list));
 list *P = L->next;
 int a = 1;
 Pbady->x = e;
 while (P != NULL)
 {
  if (a == i-1)
  {
   Pbady->next = P->next;
   P->next = Pbady;
   L->x++;
   break;
  }
  P = P->next;
  a++;
 }
}

9. deletion

void DelList(list * L,int i)//删除操作
{
 if (i > L->x)
 {
  printf("插入位置不合法\n");
  return;
 }
 list * P = L->next;
 list * P_2 = NULL;
 int a = 1;
 while (P != NULL)
 {
  if (a == i-1)
  {
   P_2 = P->next;
   P->next = P->next->next;
   P_2->next = NULL;
   free(P_2);
   L->x--;
   break;
  }
  P = P->next;
  a++;
 }
}

The display list existing content

void ShowList(list * L)//显示链表
{
 list * P = L;
 printf("该链表共有%d个元素\n", P->x);
 P = L->next;
 while (P!= NULL)
 {
  printf("%d\n", P->x);
  P = P->next;
 }
}

11. Release Memory

void FreeList(list * L)
{
 list * P;
 if (L == NULL)
 {
  return ;
 }
 while (L)
 {
  P = L->next;
  free(L);
  L = P;
 }
}

Well, one way to explain it to the list here. If there is something wrong, please point out.
Each text sentence: Do not always use to impress to retain tears, and sometimes your tears counterproductive, smile, be sure to smile with confidence to face all!

Published 15 original articles · won praise 41 · views 6140

Guess you like

Origin blog.csdn.net/Fdog_/article/details/102932871