[Data structure] C language, realize address book through dynamic sequence table, source code included

Directory

  • Implement the underlying code sequence table
    • seqlist.h
    • seqlist.c
  • Then implement the contact sequence table
    • contact.h
    • contact.c
  • main function
    • test.c

Implement the underlying code sequence table

(I wrote this in my previous article) I use it directly here.

seqlist.h

#pragma once

#include <stdio.h>
#include "contact.h"

typedef struct contactinfo datatype;//Change the data type to the address book type

//Create a sequence list
typedef struct seqlist
{<!-- -->
datatype* a; //Data to be stored
int size;//Number of valid data
int capacity;//space capacity
}SL;

//initialization
void init(SL* L);//All three data in the table must be changed


//destroy
void destroy(SL* L);//Destruction is a data space in the table, and the type of this data space is the structure type of the table

//Head plug
void push_front(SL* L, datatype x);

//Tail insertion
void push_back(SL *L,datatype x);

//Delete header
void dele_front(SL *L);

//Delete tail
void dele_back(SL *L);

//Print
void display(SL *L);

//Short call
int empty(SL* L);

//Insert before the specified position
void insert_pos(SL *L,int position,datatype x);

//Delete the specified location
void dele_pos(SL* L, int position);

//Expansion
void check_capacity(SL* L);

//Find
int find(SL* L, datatype x);

seqlist.c

void init(SL* L)
{<!-- -->
/*L->a = NULL;
L->size = 0;
L->capacity = 0;*/
L->a = (datatype*)malloc(sizeof(datatype) * 4);
if (L->a == NULL)
{<!-- -->
printf("Initialization failed!");
exit(-1);
}
L->size = 0;
L->capacity = 4;
}

void destroy(SL* L)
{<!-- -->
if(L->a)
free(L->a);//Delete the space
L->a = NULL;//Prevent wild pointers
L->capacity = L->size = 0;

}

void push_front(SL* L, datatype x)
{<!-- -->
assert(L);
//Is there enough space? , expansion
if (L->size == L->capacity)
{<!-- -->
datatype* temp = (datatype*)realloc(L->a, L->capacity * sizeof(datatype) * 2);//The space for storing data is doubled
if (temp == NULL)
{<!-- -->
printf("realloc fail!\\
");
return;
}
L->a = temp;//make a point to the first address of the expanded space
L->capacity = L->capacity * 2;
}


//Insert
int end = L->size - 1;//points to the position of the last number
while (end >= 0)//When equal to 0, move back further
{<!-- -->
L->a[end + 1] = L->a[end];
end--;
}
L->a[0] = x;
L->size + + ;
}


void push_back(SL* L, datatype x)
{<!-- -->
if (L == NULL)
{<!-- -->
printf("The table was not found!"); exit(-1);
}
//Is there enough space? Not enough and needs expansion
if (L->size == L->capacity)
{<!-- -->
/*int newcapcity = L->capacity == 0 ? 4 : 2 * L->capacity;*/
datatype* temp=(datatype *)realloc(L->a, L->capacity * sizeof(datatype) * 2);//The space for storing data is doubled
if (temp == NULL)
{<!-- -->
printf("realloc fail!\\
");
return;
}
L->a = temp;//make a point to the first address of the expanded space
L->capacity = L->capacity * 2;
}

//Insert
L->a[L->size] = x;
L->size + + ;
}


void dele_front(SL* L)//You only need to move the following numbers forward until the last element is also moved
{<!-- -->
int start = 0;
while (start < L->size - 1)
{<!-- -->
L->a[start] = L->a[start + 1];
start + + ;
}
L->size--;
}

void dele_back(SL* L)//Tail deletion does not require expansion
{<!-- -->
L->size--;
}

int empty(SL* L)
{<!-- -->
if (L->size == 0)
{<!-- -->
return 1;
}
else return 0;
}
         
void check_capacity(SL *L)
{<!-- -->
if (L->size == L->capacity)
{<!-- -->
/*int newcapcity = L->capacity == 0 ? 4 : 2 * L->capacity;*/
datatype* temp = (datatype*)realloc(L->a, L->capacity * sizeof(datatype) * 2);//The space for storing data is doubled
if (temp == NULL)
{<!-- -->
printf("realloc fail!\\
");
return;
}
L->a = temp;//make a point to the first address of the expanded space
L->capacity = L->capacity * 2;
}
}


void insert_pos(SL* L, int position, datatype x)
{<!-- -->
assert(L);
check_capacity(L);
assert(position >= 0 & amp; & amp; position <= L->size);


int end = L->size - 1;
for (end; end >= position; end--)
{<!-- -->
L->a[end + 1] = L->a[end];
}
L->a[position] = x;
L->size + + ;
}


void dele_pos(SL* L, int position)
{<!-- -->
assert(L);
if (L == NULL)
{<!-- -->
return;
}
for (position; position < L->size - 1; position + + )
{<!-- -->
L->a[position] = L->a[position + 1];
}
L->size--;
}

int find(SL* L,datatype x)
{<!-- -->
assert(L);
int i = 0;
/*for (i = 0; i <= L->size - 1; i + + )
{
if()
{
printf("Found!");
return i;
}
}*/
if (i == L->size)
{<!-- -->
printf("Not found!"); return -1;
}
}

Reimplement the contact sequence table

contact.h

//The address book does not store a single type, but a composite type structure.
typedef struct contactinfo//This is to define a data type that needs to be stored, not a table
{<!-- -->
char name[100];
char sex[10];
int age;
char tel[15];
char addr[100];
}cinfo;

//The bottom layer of the address book is implemented by a sequence table
typedef struct seqlist contact;//It’s just a statement, used in contact.c



//initialization
void init_contact(contact *L);//What is passed here is the sequence table

//destroy
void destroy_contact(contact* L);

//Add contacts
void contactADD(contact *L);

//delete contact
void contactDele(contact* L);

//Modify contact
void contactModify(contact* L);

//View address book
void contactShow(contact* L);

//Find contacts
int contactFindByName(contact* L,char name[]);

void contactFind(contact* L);

contact.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "contact.h"
#include "seqlist.h"
#include <string.h>


void init_contact(contact* L)
{<!-- -->
init(L);

}


void destroy_contact(contact* L)
{<!-- -->
destroy(L);
}

void contactADD(contact* L)
{<!-- -->
cinfo people;///Create a structure variable to receive data
printf("Please enter the contact name:\\
");
scanf("%s", people.name);
printf("Please enter the contact's gender:\\
");
scanf("%s", people.sex);
printf("Please enter the age of the contact:\\
");
scanf("%d", & amp;people.age);//
printf("Please enter the contact number:\\
");
scanf("%s", people.tel);
printf("Please enter the contact address: \\
");
scanf("%s", people.addr);
push_back(L, people);
}


void contactDele(contact* L)
{<!-- -->
printf("Please enter the user name to be deleted:\\
");
char name[100];
scanf("%s", name);
int position = contactFindByName(L,name);
if (position < 0)
{<!-- -->
printf("The contact does not exist!\\
");
return;
}
dele_pos(L, position);
}

void contactModify(contact* L)
{<!-- -->
printf("Please enter the user name to be modified:\\
");
char name[100];
scanf("%s", name);
int position = contactFindByName(L,name);
if (position < 0)
{<!-- -->
printf("The user to be modified does not exist!\\
");
return;
}
printf("Please enter the contact name:\\
");
scanf("%s", L->a[position].name);
printf("Please enter the contact's gender:\\
");
scanf("%s", L->a[position].sex);
printf("Please enter the age of the contact:\\
");
scanf("%d", & amp;L->a[position].age);//
printf("Please enter the contact number:\\
");
scanf("%s", L->a[position].tel);
printf("Please enter the contact address: \\
");
scanf("%s", L->a[position].addr);

}

int contactFindByName(contact* L, char name[])
{<!-- -->
for (int i = 0; i < L->size; i + + )
{<!-- -->
if (strcmp(L->a[i].name,name) == 0)
{<!-- -->
return i;
}
}
return -1;
}


void contactShow(contact* L)
{<!-- -->
printf("%-10s %-5s %-5s %-20s %-20s\\
","Name","Gender","Age","Telephone number",\ "address");
for (int i = 0; i < L->size; i + + )
{<!-- -->
printf("%-10s %-5s %-5d %-20s %-20s\\
", L->a[i].name, L->a[i].sex, L->a[ i].age, L->a[i].tel, L->a[i].addr);
}
}

void contactFind(contact* L)
{<!-- -->
printf("Please enter the user name you are looking for:\\
");
char name[100];
scanf("%s", name);
int position = contactFindByName(L,name);
if (position < 0)
{<!-- -->
printf("The contact does not exist!\\
");
return;
}
printf("%-10s %-5s %-5s %-20s %-20s\\
", "Name", "Gender", "Age", "Telephone", \ "address");
printf("%-10s %-5s %-5d %-20s %-20s\\
", L->a[position].name,
L->a[position].sex, L->a[position].age, L->a[position].tel, L->a[position].addr);

}

Main function

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "seqlist.h"
#include "contact.h"

void menu()
{<!-- -->
printf("########## Address Book ##########\\
");
printf("##########1.Add contact##########\\
");
printf("##########2. Delete contact##########\\
");
printf("##########3.Modify contact##########\\
");
printf("##########4.Find Contact##########\\
");
printf("##########5.View address book##########\\
");
printf("########## 0.Exit ##########\\
");
}


int main()
{<!-- -->
int op = -1;
contact L;
init_contact( & amp;L);
do
{<!-- -->
menu();
printf("Please select your operation:\\
");
scanf("%d", & amp;op);
switch(op)
{<!-- -->
case 1:
contactADD( & amp;L);
break;
case 2:
contactDele(&L);
break;
case 3:
contactModify( & amp;L);
break;
case 4:
contactFind( & amp;L);
break;
case 0:
printf("good bye!");
break;
default:printf("Please re-enter:\\
");
}
} while (op != 0);
return 0;
}