408 King's Road Data Structure After-Class Code Exercises (8)

Get into the habit of writing together! This is the 12th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

I plan to update the implementation of all after-school code exercises in the 23 King's Road data structure. Although the exams are generally written in pseudocode, I have implemented all of them for obsessive-compulsive disorder. The warehouse is here.

  • Linear table
  • linked list

Singly linked list structure

#define ElemType int

typedef struct LNode{
  ElemType data;
  struct LNode* next;
}LNode, *LinkList; 
复制代码

Some of the methods I have defined myself are easy to run, try to use malloc()and free()for memory management (exam requirements):

// 创建一个不带头结点的单链表
LinkList createList(vector<int> data) {
  if (data.size() == 0) return NULL;
  
  LNode* head = (LinkList)malloc(sizeof(LNode));
  head->data = data[0];
  head->next = NULL;
  
  LNode* p = head;
  for (int i = 1; i < data.size(); i++) {
    LNode* q = (LNode*)malloc(sizeof(LNode));
    q->data = data[i];
    q->next = NULL;
    p->next = q;
    p = q;
  }
  return head;
}

// 创建一个带头结点的单链表
LinkList createHeadList(vector<int> data) {
  if (data.size() == 0) return NULL;

  LNode* head = (LinkList)malloc(sizeof(LNode));
  head->next = NULL;

  LNode* p = head;
  for (int i = 0; i < data.size(); i++) {
    LNode* q = (LNode*)malloc(sizeof(LNode));
    q->data = data[i];
    q->next = NULL;
    p->next = q;
    p = q;
  }
  return head;
}

// 打印链表
void printList(LinkList L) {
  while (L != NULL) {
    cout << L->data << " ";
    L = L->next;
  }
  puts("");
}
复制代码

2.3.7, 1

image.png

  • The linked list has natural recursion, each node can be regarded as a small linked list, and they can be combined with each other to form a new linked list
  • Recursion is when a function directly or indirectly calls the function itself
  • Implementing a recursive function only requires figuring out two things: the recurrence relation and the recursive exit
  • The recursive relationship in this question is: delete the current node if it is x, and process the next node if it is not x
  • The recursive exit is: the current node is empty
  • Because the recursion depth is O(n), the time complexity is O(n) and the space complexity is O(n)
void delX(LinkList &L, int x) {
  if (L == NULL) return;      // 1.递归出口
  LNode* p;
  if (L->data == x) {         // 2.L结点的值为x,删除L结点
    p = L;
    L = L->next;
    free(p);
    delX(L, x);
  } else {                    // 3.L结点的值不为x,递归处理L结点的下一个结点
    delX(L->next, x);
  }
}
复制代码

2.3.7, 2

image.png

  • The only difference from the first question is the leading node. The recursion of the first question can also be used, and the function does not need any changes at all.
void delX(LinkList &L, int x) {
  if (L == NULL) return;      // 1.递归出口
  LNode* p;
  if (L->data == x) {         // 2.L结点的值为x,删除L结点
    p = L;
    L = L->next;
    free(p);
    delX(L, x);
  } else {                    // 3.L结点的值不为x,递归处理L结点的下一个结点
    delX(L->next, x);
  }
}

// 调用
delX(head->next, 3);
复制代码
  • Classic double pointer, p scans the entire linked list from beginning to end, and pre points to the previous node of p for easy deletion
  • Time complexity O(n), space complexity O(1)
void delX2(LinkList &L, int x) {
  LNode *pre = L, *p = L->next, *q;
  while (p != NULL) {
    if (p->data == x) {
      q = p;
      p = p->next;
      pre->next = p;
      free(q);
    } else {
      pre = p;
      p = p->next;
    }
  }
}
复制代码

Guess you like

Origin juejin.im/post/7086442631051345934