# Data structure single chain table

Posted by wvwisokee on Fri, 07 Feb 2020 15:59:06 +0100

# Data structure - single chain table

Initlist (LinkList & L) parameter: single chain table l function: initialization time complexity O(1)
ListLength(LinkList L) parameter: single chain table l function: time complexity of obtaining single chain table length O(n)
Listinsert (LinkList & L, int i, ElemType e) parameters: single chain table L, location I, element e functions: location I post time complexity O(n) [add lookup]
If the pointer p is known, insert O(1)
ListDelete (LinkList & L, int i) parameter: single chain table L, location I function: delete location I element time complexity O(n) [add lookup]
If the deletion of p pointer is known, it is better to O(1), because the data field can be exchanged with the successor node, and then the successor node can be deleted.
The worst is O(n), which is to find the node before p from the beginning, and then delete the node indicated by p
LocateElem(LinkList L,ElemType e) parameter: single chain table L, element E function: find the first element equal to e, return pointer time complexity O(n)

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
#define Status int
#define ElemType int
//Single link table node data structure
typedef struct LNode
{
ElemType data;//Data domain
struct LNode *next;//Pointer domain
//**************************Basic operation function***************************//
//Initialization function
{
L = new LNode;//Generate the header node so that the deletion and other operations do not have to be divided into the first node and others
L->next = NULL;
return 1;
}
//There is no data in the header node to obtain the length of single chain table
{
int sum=0;
while(p)
{
sum++;
p=p->next;
}
}
//Insert function -- post insert into position I (1 < = I < = length + 1), i.e. position I need not be distinguished after i-1
{
LNode* s;
int j=0;
while(p&&(j<i-1))//j refers to the position i-1 or p has jumped out at the end
{
p=p->next;
++j;
}
if(!p||j>i-1)//When I < 1 or I > ListLength (L) + 1, if the insertion position is invalid, ListLength will not be called to improve efficiency
{
printf("Invalid insertion position!!!\n");
return false;
}
s=new LNode;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
//Delete function delete node at position I, i.e. delete node after i-1
{
LNode* s;
int j=0;
while(p&&(j<i-1))//j refers to position i-1
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1)//Invalid delete position when i<1 or i>ListLength (L)
{
printf("Invalid delete location!!!\n");
return false;
}
q=p->next;
p->next=q->next;
free(q);//Release space
return true;
}
//The search function finds the first node equal to e by value and returns the node pointer, otherwise NULL
{
LNode *p=L;
while(p&&(p->data!=e))
{
p=p->next;
}
return p;
}
//**************************Function implementation function**************************//
//Ergodic output function
{
if(ListLength(L))
{
printf("All elements of the current single chain table:");
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
else
{
printf("Current single chain table is empty!\n");
}
}
//Insert function call ListInsert to insert
{
int place;
ElemType e;
bool flag;
printf("Please enter the location to insert(Starting from 1)And elements:\n");
scanf("%d%d",&place,&e);
flag=ListInsert(L,place,e);
if(flag)
{
printf("Insert successfully!!!\n");
PrintList(L);
}
}
//Delete function call ListDelete delete
{
int place;
bool flag;
printf("Please enter the location to delete(Starting from 1):\n");
scanf("%d",&place);
flag=ListDelete(L,place);
if(flag)
{
printf("Delete successfully!!!\n");
PrintList(L);
}
}
//Find function call LocateElem to find
{
ElemType e;
LNode *q;
printf("Please enter the value to find:\n");
scanf("%d",&e);
q=LocateElem(L,e);
if(q)
{
printf("Find the element!\n");
}
else
}
{
printf("********1.Post interpolation    Two.delete*********\n");
printf("********3.lookup    Four.output*********\n");
printf("********5.Sign out          *********\n");
}
//Main function
int main()
{
int choice;
InitList(L);
while(1)
{
scanf("%d",&choice);
if(choice==5) break;
switch(choice)
{
case 1:
Insert(L);
break;
case 2:
Delete(L);
break;
case 3:
Search(L);
break;
case 4:
PrintList(L);
break;
default:
printf("Input error!!!\n");
}
}
return 0;
}

```
Published 23 original articles, won praise 2, visited 264