数据结构中,我们经常用到了一些操作,比如删除,插入等等,有API函数直接调用,当然是比较好的,但是我们可以尝试一下自己写一下代码。
以简单的录入个人信息为例,首先来看一下这些函数。
#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);
现在我们就来一步一步的实现每一个函数
1.遍历链表
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.链表元素个数
int ChainListLength(ChainListType *head)
{
ChainListType *h;
int i=0;
h=head;
while(h){
i++;
h=h->next;//遍历计数
}
return i;
}
3.删除某个节点
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.插入某个元素
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.查找某个元素
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.添加头节点
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.添加尾节点
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;
}
我们可以来写一个函数来测试一下
#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;
}