C language to lead the list of linked nodes to create find insert delete operations

  • 2020-04-02 02:45:57
  • OfStack

The example of this article describes the C language to achieve the lead node linked list creation, search, insert, delete operations. Is the base operation for the linked list part of the data structure. Share with you for your reference. Specific methods are as follows:


#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
  int data;
  struct node* next;//Notice the rules for defining structural variables here
} Node, *PNode;

Node* createLinklist(int length)
{
  int i = 0;
  PNode pHeader = NULL;
  PNode pTail = NULL;
  PNode pTemp = NULL;
  printf("createn");

  pHeader = (PNode)malloc(sizeof(Node));//Application header
  if (!pHeader)
  {
    exit(-1);
  }
  pHeader->next = NULL;

  for (i = 0; i < length; i++)
  {
    pTemp = (PNode)malloc(sizeof(Node));//Use malloc to include header files
    if (!pTemp)
    {
      exit(-1);
    }
    pTemp->data = i*10;
    pTemp->next = NULL;
    if (!pHeader->next)
    {
      //If the first node is empty, connect to the first node first
      pHeader->next = pTemp;
    }
    else
    {
      pTail->next = pTemp;
    }
    pTail = pTemp;
  }
  return pHeader;
}

Node* search(PNode pHeader, int k)
{
  PNode p = pHeader->next;
  int i = 1;
  printf("searchn");
  while(p && (i < k))
  {
    p = p->next;
    i++;
  }
  if (p && (i == k)) //I == k is necessary for this step,
  //Because if at the beginning I was the same as gt; = k and pHeader-> The next step is not NULL yet, and the value of the first element is returned
  {
    return p;
  }
  return NULL;
}

int insert(PNode pHeader, PNode pNew, int k)
{
  PNode p = NULL;
  printf("insertn");
  if ( 1 == k )
  {
    p = pHeader;
  }
  else
  {
    printf("==>");
    p = search(pHeader, k-1);
  }
  if (p)
  {
    //One of the main differences between a lead node and a non-lead node is this
    //If the node is not headed, then the operation to insert the node at the first position should be
    // pNew->next = p;
    // p = pNew;
    //The operation of the lead node is as follows
    pNew->next = p->next;
    p->next = pNew;
    return 1;
  }
  return 0;
}

int deleteNode(PNode pHeader, int k)
{
  PNode p = NULL;
  printf("deleteNoden");
  if (1 == k)
  {
    p = pHeader->next;
  }
  else
  {
    printf("==>");
    p = search(pHeader, k-1);
  }
  if (p && p->next)
  {
    //The operation that removes the first node when the node is not headed
    // Node* temp = p;
    // p = p->next;
    // free(temp);
    //The operation of the lead node is as follows
    PNode temp = p->next;
    p->next = temp->next;
    free(temp);
    return 1;
  }
  else
  {
    printf("Not Foundn");
    return 0;
  }
}

void print(PNode pHeader)
{
  PNode p = pHeader->next;
  printf("printn ");
  while(p)
  {
    printf("%4d ", p->data);
    p = p->next;
  }
  putchar('n');
}

void freeList(PNode pH)
{
  PNode p = NULL;
  printf("freeListn");
  while(NULL != pH)
  {
    p = pH;
    pH = pH->next;
    printf("%4d be freedn", p->data);
    free(p);
  }
}

int main(void)
{
  PNode pHeader = NULL;//NULL macros (all uppercase) are used in C and C++ to determine whether a pointer is NULL
  PNode pNew = NULL;
  PNode result = NULL;
  pHeader = createLinklist(10);
  print(pHeader);
  result = search(pHeader, 5);
  if ( result )
  {
    printf("%dn", result->data);
  }
  else
  {
    printf("Not Foundn");
  }
  pNew = (PNode)malloc(sizeof(Node));
  if (!pNew)
  {
    exit(-1);
  }
  pNew->data = 100;
  pNew->next = NULL;
  insert(pHeader, pNew, 5);
  print(pHeader);
  deleteNode(pHeader, 12);
  print(pHeader);
  freeList(pHeader);
  return 0;
}

The above examples are well annotated and are not difficult to understand. Hope that this article is helpful to the C program data structure and algorithm design.


Related articles: