In the data structure, we often use some operations, such as deletion, insertion, etc., of course, it is better to directly call API functions, but we can try to write the code ourselves.
Take the simple entry of personal information as an example, let's first look at these functions.
#include <stdlib.h>
typedef struct Node{
DATA data;
struct Node *next;
}ChainListType;
ChainListType *ChainListAddEnd(ChainListType *head,DATA data);
ChainListType *ChainListAddFirst(ChainListType *head,DATA data);
ChainListType *ChainListFind(ChainListType *head,char *key);
ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data);
int ChainListDelete(ChainListType *head,char *key);
int ChainListLength(ChainListType *head);
void ChainlistPrintAll(ChainListType *head);
Now let's implement each function step by step
1. Traverse the linked list
void ChainlistPrintAll(ChainListType *head)
{
ChainListType *h;
DATA data;
h=head;
printf("list as fllowing:\n");
while(h){//处理每一个节点
data=h->data;
printf("(%s,%s,%d)\n",data.key,data.name,data.age);
h=h->next;//准备处理下一个节点
}
return ;
}
2. The number of linked list elements
int ChainListLength(ChainListType *head)
{
ChainListType *h;
int i=0;
h=head;
while(h){
i++;
h=h->next;//遍历计数
}
return i;
}
3. Delete a node
int ChainListDelete(ChainListType *head,char *key)
{
ChainListType *node,*h;
node=h=head;
while(h){
if(strcmp(h->data.key,key)==0){//找关键字
node->next=h->next;
free(h);//释放内存
return 1;
}else{
node=h;
h=h->next;//没找到就接着找
}
}
return 0;
}
4. Insert an element
ChainListType *ChainListInsert(ChainListType *head,char *findkey,DATA data)
{
ChainListType *node,*node1;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("node's application fail!\n");
return 0;
}
node->data=data;
node1=ChainListFind(head,findkey);
if(node1){
node->next=node1->next;
node1->next=node;
}else{
free(node);
printf("do not find the place!\n");
}
}
5. Find an element
ChainListType *ChainListFind(ChainListType *head,char *key)
{
ChainListType *h;
h=head;
while(h){//节点有效
if(strcmp(h->data.key,key)==0)
return h;
h=h->next;
}
return NULL;
}
6. Add head node
ChainListType *ChainListAddFirst(ChainListType *head,DATA data)
{
ChainListType *node,*h;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("node's application fail!\n");
return NULL;
}
node->data=data;
node->next=head;
head=node;
return head;
}
7. Add tail node
ChainListType *ChainListAddEnd(ChainListType *head,DATA data)
{
ChainListType *node,*h;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType))))
{
printf("node's application fail!\n");
return NULL;
}
node->data=data;
node->next=NULL;
if(head==NULL){
head=NULL;
return head;
}
h=head;
while(h->next!=NULL)
h=h->next;
h-next=node;
return head;
}
We can write a function to test it
#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
ChainListType *node,*head=NULL;
DATA data;
char key[15],findkey[15];
printf("Enter the data in the linked list, including keywords, names, age:\n");
do{
fflush(stdin);
scanf("%s",data.key);
if(strcmp(data.key,"0")==0) break;
scanf("%%d",data.name,data.age);
head=ChainListAddEnd(head,data);
}while(1);
printf("The number of nodes in the linked list is:%d\n",ChainListLength(head));
ChainlistPrintAll(head);
printf("\nEnter the insertion node's keyword:\n");
scanf("%s",&findkey);
printf("Enter the data in the linked list, including keywords, names, age:\n");
scanf("%s%s%d",data.key,data.name,data.age);
head=ChainListInsert(head,findkey,data);
ChainlistPrintAll(head);
printf("Enter search keywords:\n");
fflush(stdin);
scanf("%s",key);
node=ChainListFind(head,key);
if(node){
data=node->data;
printf("The keyword %s corresponding node is:(%s,%s,%d)",key,data.key,date.name,date.age);
}else
printf("No corresponding node found!\n");
printf("Enter keywords to delete:\n");
fflush(stdin);
scanf("%s",key);
ChainListDelete(head,key);
ChainlistPrintAll(head);
return 0;
}