1. 链表结构和操作定义
ChainList.h
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node{
DATA data;
struct Node *next;
}ChainListType;
/*--------------------------函数原型声明 start----------------------*/
ChainListType *ChainListCreateEnd(int n); //功能:尾插法建立链表
ChainListType *ChainListCreateFirst(int n); //功能:头插法建立链表
ChainListType *ChainListAddEnd(ChainListType *head, DATA data); //功能:在链表末尾添加节点
ChainListType *ChainListAddFirst(ChainListType *head, DATA data); //功能:在链表头部添加节点
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data); //功能:在链表指定位置插入节点
ChainListType *ChainListFind(ChainListType *head, char *key); //功能:按关键字在链表中查找内容
int ChainListDelete(ChainListType *head, char *key); //功能:删除指定关键字的节点
int ChainListLength(ChainListType *head); //功能:获取链表节点数量
/*--------------------------函数原型声明 end----------------------*/
/*--------------------------函数定义 start----------------------*/
//功能:尾插法建立链表
ChainListType *ChainListCreateEnd(int n){
ChainListType *head, *node, *h;
DATA data;
if(!(head = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!\n");
return NULL;
}
head->next = NULL;
h = head;
for(int i=0; i<n; i++){
if(!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!\n");
return NULL;
}
printf("请输入添加的节点(学号 姓名 年龄):");
fflush(stdin);
//scanf("%s %s %d", &data.key, &data.name, &data.age);
scanf("%s %s %d", data.key, data.name, &data.age);
node->data = data;
node->next = NULL;
h->next = node;
h = h->next;
}
return head;
}
//功能:头插法建立链表
ChainListType *ChainListCreateFirst(int n){
ChainListType *head, *node, *h;
DATA data;
if(!(head = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!\n");
return NULL;
}
head->next = NULL;
h = head;
for(int i=0; i<n; i++){
if(!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!\n");
return NULL;
}
printf("请输入添加的节点(学号 姓名 年龄):");
fflush(stdin);
//scanf("%s %s %d", &data.key, &data.name, &data.age);
scanf("%s %s %d", data.key, data.name, &data.age);
node->data = data;
node->next = h->next;
h->next = node;
}
head = h; //切记不能少
return head;
}
//功能:在链表末尾添加节点
ChainListType *ChainListAddEnd(ChainListType *head, DATA data){
ChainListType *node, *h;
if(!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!\n");
return NULL;
}
node->data = data;
node->next = NULL;
if(head->next == NULL){
head->next = node;
return head;
}
h = head;
while(h->next != NULL)
h = h->next;
h->next = node;
return head;
}
//功能:在链表头部添加节点
ChainListType *ChainListAddFirst(ChainListType *head, DATA data){
ChainListType *node;
if(!(node = (ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!!");
return NULL;
}
node->data = data;
node->next = head->next;
head->next = node;
return head;
}
//功能:在链表指定位置插入节点
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data){
ChainListType *node, *p;
if(!(node=(ChainListType *)malloc(sizeof(ChainListType)))){
printf("申请内存失败!!!");
return NULL;
}
node->data = data;
p = ChainListFind(head, findkey);
if(p){
node->next = p->next;
p->next = node;
}
else{
free(node);
printf("未找到插入位置!");
}
return head;
}
//功能:按关键字在链表中查找内容
ChainListType *ChainListFind(ChainListType *head, char *key){
ChainListType *h;
h = head->next;
while(h){
if(strcmp(h->data.key, key)==0)
return h;
h = h->next;
}
return NULL;
}
//功能:删除指定关键字的节点
int ChainListDelete(ChainListType *head, char *key){
ChainListType *pre, *h;
pre = head;
h = head->next;
while(h){
if(strcmp(h->data.key, key)==0){
pre->next = h->next;
free(h);
return 1;
}
else{
pre = h;
h = h->next;
}
}
return 0;
}
//功能:获取链表节点数量
int ChainListLength(ChainListType *head){
ChainListType *h;
int i = 0;
h = head->next;
while(h){
++i;
h = h->next;
}
return i;
}
/*--------------------------函数定义 end----------------------*/
2. 链表操作测试
ChainListText.cpp
#include<stdio.h>
//功能:定义链表结构中的数据类型DATA
typedef struct {
char key[15];
char name[20];
int age;
}DATA;
#include "ChianList.h"
//功能:遍历链表各节点
void ChainListAll(ChainListType *head){
ChainListType *h;
DATA data;
h = head->next;
printf("链表所有数据如下:\n");
while(h){
data = h->data;
printf("(%s,%s,%d)\n", data.key, data.name, data.age);
h = h->next;
}
}
//功能:测试链表所有操作的主函数
int main(){
int select;
int len, num;
char key[15];
DATA data, *data1;
ChainListType *head, *tmp;
do{
printf("---------------------------\n");
printf("1.表尾法创建链表 2.表头法创建链表\n");
printf("3.在链表末尾添加节点 4.在链表表头添加节点\n");
printf("5.在链表指定位置插入节点 6.删除指定关键字的节点\n");
printf("7.按关键字在链表中查找内容 8.获取链表节点数量\n");
printf("9.遍历链表内容 0.退出\n");
printf("请选择执行的操作序号:");
fflush(stdin);
scanf("%d", &select);
switch(select){
case 1:
printf("请输入要创建的列表的节点数:");
fflush(stdin);
scanf("%d", &num);
head = ChainListCreateEnd(num);
break;
case 2:
printf("请输入要创建的列表的节点数:");
fflush(stdin);
scanf("%d", &num);
head = ChainListCreateFirst(num);
break;
case 3:
printf("在链表末尾添加节点\n"); //注意在空链表中添加情况分析
printf("请输入添加的节点(学号 姓名 年龄):");
fflush(stdin);
scanf("%s %s %d", &data.key, &data.name, &data.age);
ChainListAddEnd(head, data);
printf("链表已建完\n");
break;
case 4:
printf("在链表头部添加节点\n");
printf("请输入添加的节点(学号 姓名 年龄):");
fflush(stdin);
scanf("%s %s %d", &data.key, &data.name, &data.age);
ChainListAddFirst(head, data);
printf("链表已建完\n");
break;
case 5:
printf("请输入插入位置节点关键字:");
fflush(stdin);
scanf("%s", key);
printf("请输入添加的节点(学号 姓名 年龄):");
fflush(stdin);
scanf("%s %s %d", &data.key, &data.name, &data.age);
ChainListInsert(head, key, data);
break;
case 6:
printf("请输入删除节点的关键字:");
fflush(stdin);
scanf("%s", key);
ChainListDelete(head, key);
break;
case 7:
printf("请输入查找节点的关键字:");
fflush(stdin);
scanf("%s", key);
tmp = ChainListFind(head, key);
if(tmp)
printf("找到的节点信息为:(%s,%s,%d)\n", (tmp->data).key, (tmp->data).name, (tmp->data).age);
break;
case 8:
printf("链表长度为:");
num = ChainListLength(head);
printf("%d\n", num);
break;
case 9:
printf("链表遍历结果为:\n");
ChainListAll(head);
break;
}
}while(select != 0);
system("pause");
return 0;
}