C + data structure and algorithm

Posted by darrensw on Sun, 01 Dec 2019 15:39:25 +0100

1. Definition of single chain table

Each node includes a data domain and a pointer domain. The addresses of nodes in memory can be discontinuous, but the internal addresses of nodes must be continuous.

2. Structure definition

typedef struct Lnode{
int data;
struct Lnode *next;
}Lnode,*LinkList;

3. Creation of single chain table

1) head insertion

//Establishing single chain table of lead node by head insertion
LinkList create1(LinkList &L){
  Lnode *s;
  int x; 
  L=(LinkList) malloc( sizeof(Lnode));//Create head node
  L->next = NULL; //Initialization
  printf("Add data to linked list,99999 End\n");
  scanf("%",&x);
  while(x!=99999){
      s = (Lnode*)malloc(sizeof(Lnode));//Create a new node
      s->data = x;
      s->next = L->next;
      L->next = s;
      scanf("%d",&x);
       
  }


return L;
}

2) tail insertion

//Establishing single chain table by tail insertion
LinkList create2(LinkList &L){

    int x;
    L=(LinkList) malloc( sizeof(Lnode));//Create a tail node
    Lnode *s,*r = L;//S is the insertion node pointer, r is the tail pointer
printf("Add data to linked list,99999 End\n");
    //scanf("%",&x);
    while(x!=99999){
      s = (Lnode*)malloc(sizeof(Lnode));//Create a new node
      scanf("%d",&x); 
      s->data = x;
      r->next = s;
      r = s; //r points to the new footer
      
    }
    r->next = NULL;//Tail node pointer null
    return L;
}

4. Search of single chain table

1) find by value

//Find the table node by value, return the node bit order, and return - 1 if the search fails
int locateElem(LinkList L, int e){
    Lnode *P = L->next;
    int j=1;
    while (P!=NULL&&P->data!=e){
      P = P->next;
      j++;
    }
     if(P->next == NULL && P->data == e)
    return j;
    else if(P->next != NULL && P->data == e)
    return j;
    else
        return -1;
}
//Look up the table node by value and return the node pointer, which is convenient for chain table operation
Lnode *locateElem2(LinkList L, int e){
 Lnode *P = L->next;
    while (P!=NULL&&P->data!=e){
      P = P->next;
    }
    return P;//Null pointer if failed
}

2) search by serial number

//Look up table nodes by sequence number and return node value
int getElem(LinkList L,int i){
    int j = 1;
    Lnode *P = L->next;
    if(i==0)
        return 0;//If i=0, the value of the return node is returned, but the value of the return node is not stored, so 0 is returned
    if(i<0)
        return -1;//Error sequence number returns - 1
    while(P&&j<i)//P is not empty.
    {
       P= P->next;
       j++;
    }
   return P->data;
}

//Look up the table node according to the serial number, and return the node pointer, which is convenient for chain table operation
Lnode *getElem1(LinkList L, int i){
   int j = 1;
    Lnode *P = L->next;
    if(i==0)
        return L;//If i=0, return to node
    if(i<0)
        return NULL;//Error sequence number returns NULL
    while(P&&j<i)//P is not empty.
    {
       P= P->next;
       j++;
    }
   return P;
}

5. Complete code and operation

#include <stdio.h>
#Include < stdlib. H > / / malloc function header file
//Single chain table definition
typedef struct Lnode{

int data;
struct Lnode *next;
}Lnode,*LinkList;

//Establishing single chain table of lead node by head insertion
LinkList create1(LinkList &L){
  Lnode *s;
  int x; 
  L=(LinkList) malloc( sizeof(Lnode));//Create head node
  L->next = NULL; //Initialization
  printf("Add data to linked list,99999 End\n");
  scanf("%",&x);
  while(x!=99999){
      s = (Lnode*)malloc(sizeof(Lnode));//Create a new node
      s->data = x;
      s->next = L->next;
      L->next = s;
      scanf("%d",&x);
       
  }


return L;
}
  //Establishing single chain table by tail insertion
LinkList create2(LinkList &L){

    int x;
    L=(LinkList) malloc( sizeof(Lnode));//Create a tail node
    Lnode *s,*r = L;//S is the insertion node pointer, r is the tail pointer
printf("Add data to linked list,99999 End\n");
    //scanf("%",&x);
    while(x!=99999){
      s = (Lnode*)malloc(sizeof(Lnode));//Create a new node
      scanf("%d",&x); 
      s->data = x;
      r->next = s;
      r = s; //r points to the new footer
      
    }
    r->next = NULL;//Tail node pointer null
    return L;
}

//Look up table nodes by sequence number and return node value
int getElem(LinkList L,int i){
    int j = 1;
    Lnode *P = L->next;
    if(i==0)
        return 0;//If i=0, the value of the return node is returned, but the value of the return node is not stored, so 0 is returned
    if(i<0)
        return -1;//Error sequence number returns - 1
    while(P&&j<i)//P is not empty.
    {
       P= P->next;
       j++;
    }
   return P->data;
}

//Look up the table node according to the serial number, and return the node pointer, which is convenient for chain table operation
Lnode *getElem1(LinkList L, int i){
   int j = 1;
    Lnode *P = L->next;
    if(i==0)
        return L;//If i=0, return to node
    if(i<0)
        return NULL;//Error sequence number returns NULL
    while(P&&j<i)//P is not empty.
    {
       P= P->next;
       j++;
    }
   return P;
}

//Find the table node by value, return the node bit order, and return - 1 if the search fails
int locateElem(LinkList L, int e){
    Lnode *P = L->next;
    int j=1;
    while (P!=NULL&&P->data!=e){
      P = P->next;
      j++;
    }
     if(P->next == NULL && P->data == e)
    return j;
    else if(P->next != NULL && P->data == e)
    return j;
    else
        return -1;
}
//Look up the table node by value and return the node pointer, which is convenient for chain table operation
Lnode *locateElem2(LinkList L, int e){
 Lnode *P = L->next;
    while (P!=NULL&&P->data!=e){
      P = P->next;
    }
    return P;//Null pointer if failed
}
int main(){
    LinkList L,L1;
    create1(L);
     LinkList temp = L->next;
    while(temp->next != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("Head insertion is the reverse of data storage\n");

    create2(L1);
    LinkList temp2 = L1->next;
     while(temp2 ->next != NULL)
    {
        printf("%d ", temp2->data);
        temp2 = temp2->next;
    }
    printf("Tail interpolation is the same as data storage\n");
     printf("Input value sequence\n");
     int i;
      scanf("%d",&i);
      printf("The first%d Bit value is%d\n",i, getElem(L1,i));

      printf("Enter search value\n");
     int e;
      scanf("%d",&e);
      printf("The value is:%d The sequence is:%d\n",e, locateElem(L1,e));
return 0;
}

6. summary

As the simplest type of linked list, single linked list can get rid of the space constraints of sequence table and support dynamic operation well. But the problem is also obvious, each node can only find its direct successor, but not its previous node. When searching, you need to traverse the table from the header, which will increase the time complexity.

Topics: C