c语言代码实现
#include<stdio.h>
#include<stdlib.h>
//学生结构体
typedef struct
{
int age;
int height;
double weight;
}Student;
//单链表节点类型
typedef struct node
{
Student stu;
struct node* next;
}Node;
//节点的拷贝函数
void CopyNodeValue(Student *stu1, Student *stu2)
{
stu1->age = stu2->age;
stu1->height = stu2->height ;
stu1->weight = stu2->weight ;
}
//创建节点函数
Node* CreateNode(Student *stu)
{
//申请一个内存空间
Node *p=(Node*)malloc(sizeof(Node));
//判断是否申请成功
if (!p)
{
printf("内存不足"); return NULL;
}
//初始化节点内容,学生信息
CopyNodeValue(&p->stu, stu);
p->next = NULL;
//返回创建的节点
return p;
}
//输入节点信息函数
void InputValue(Student* stu)
{
if (!stu)return;
printf("学生的年龄,身高,体重\r\n");
scanf_s("%d%d%lf", &stu->age, &stu->height, &stu->weight);
}
//创建链表函数
Node* CreateLink()
{
//创建头节点
Student stu{ 0,0,0 };
Node* head = CreateNode(&stu);
if (!head)exit(-1);
//创建其他节点
int tag = 0;
Node *tail = head;
Node* pnew;
printf("是否添加其他节点,是输入1,否输入0\r\n");
scanf_s("%d", &tag);
while (tag !=0)
{
InputValue(&stu);
pnew=CreateNode(&stu);
if (!pnew)break;
tail->next = pnew;
tail = pnew;
printf("是否添加其他节点,是输入1,否输入0\r\n");
scanf_s("%d", &tag);
}
//返回链表
return head;
}
//链表的销毁函数
void FreeLink(Node *head)
{
//判断链表是否为空
if (head == NULL)return;
//如果非空,逐个释放
Node * p, * q;
p = head;
while (p->next != NULL)
{
q = p->next;
p->next = q->next;
free(q);
}
free(head);
}
//节点信息显示函数
void DisplayNode(Student* stu)
{
//判断节点是否为空
if (stu == NULL)return;
//打印信息
printf("age=%5d,height=%5d,weight=%5lf\r\n",stu->age,stu->height,stu->weight);
}
//遍历单链表且打印信息函数
void DisplayLink(Node* head)
{
//判断链表是否为空
if (head == NULL)return;
//如果非空,逐个打印
Node* p = head->next ;
while (p != NULL)
{
DisplayNode(&p->stu);
p = p->next;
}
}
//在指定位置插入节点函数
void InsertNode(Node *head,Node* pnew, int add)
{
//判断是否为空
if (head == NULL)return;
//判断插入的节点是否为空
if (pnew == NULL)return;
//遍历,找到插入位置
Node* p = head;
int i = 0;
for (i = 0; (i < add) && (p->next != NULL); i++)
{
p = p->next;
}
if (p->next == NULL) { printf("插入新节点后位置错误\r\n"); return; }
//插入新节点
pnew->next = p->next;
p->next = pnew;
}
//在指定位置删除节点函数
void DeleteNode(Node* head, int add)
{
//判断是否为空
if (head == NULL)return;
//遍历,找到删除位置
Node *p = head ;
Node *q;
for (int i = 0;( i<add)&&(p->next !=NULL);i++)
{
p = p->next;
}
if (p->next == NULL) { printf("删除节点后位置错误\r\n"); return; }
q = p->next;
p->next = q->next;
free(q);
//删除节点
}
//节点内容比较函数
int Comp(Student* stu1, Student* stu2)
{
if (stu1 == NULL || stu2 == NULL)return -1;
if (stu1->age == stu2->age && stu1->height == stu2->height && stu1->weight == stu2->weight)return 1;
return 0;
}
//根据指定条件查找节点函数
int CheckNode(Node *head, Node *node)
{
//链表不能为空
if (head == NULL || node==NULL)return -1;
//遍历链表,找到相同的节点
Node* p = head->next ;
int m = 0;
while (p != NULL)
{
m=Comp(&p->stu,&node->stu);
if (m == 1) { printf("找到该节点\r\n"); DisplayNode(&p->stu); return 1;}
p = p->next;
}
return 0;
}
int main()
{
//创建链表
Node* head = CreateLink();
//显示链表所有节点信息
DisplayLink(head);
//在链表指定位置插入节点
printf("插入新节点的信息\r\n");
Student stu;
InputValue(&stu);
Node* pnew = CreateNode(&stu);
InsertNode(head, pnew, 2);
printf("插入新节点后的链表\r\n");
DisplayLink(head);
//在链表指定位置删除节点
DeleteNode(head, 1);
printf("删除节点后的链表\r\n");
DisplayLink(head);
//根据指定条件查找节点
if (CheckNode(head, pnew) == 1) printf("找到该节点\r\n");
else printf("没有找到该节点\r\n");
//释放链表空间
FreeLink(head);
return 0;
}
总结:
本文只用于学习和记录。