#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct List_Photo {
unsigned char Capacity[1024];
struct List_Photo *next;
}Photo;
//创建头节点
Photo * Create_List();
//头部插入节点
int Head_Insert(Photo * head, char * buffer);
//尾部插入节点
int Tail_Insert(Photo * head, char * buffer);
//任意位置插入节点
int Random_Insert(Photo * head, int elem , char *buffer);
//头部删除节点
int Head_Delete(Photo * head);
//尾部删除节点
int Tail_Delete(Photo * head);
//任意位置删除节点
int Random_Delete(Photo * head, int number);
//查找节点(目前是根据链表标号,后期可以根据链表结构体成员进行查找,找到返回成功0并打印其数据,失败提示未找到返回-1)
int Find_Node(Photo *head,int number);
//更新节点数据
int Update_Node(Photo *head,int number,char *buffer);
//遍历节点
void Display(Photo *head);
//销毁链表(不保留头节点)
void Destroy(Photo *head);
//清空链表(保留头节点)
void Clear(Photo *head);
int main() {
char *buffer = "buffer";
char *buffer1 = "buffer1";
char *buffer2 = "buffer2";
Photo *head = Create_List();
Random_Insert(head,1,buffer);
Head_Insert(head,buffer1);
Tail_Insert(head,buffer2);
// Tail_Delete(head);
// Random_Delete(head,1);
Tail_Insert(head,buffer2);
// Head_Delete(head);
// Tail_Delete(head);
Find_Node(head,1);
Update_Node(head,3,buffer2);
Display(head);
Destroy(head);
// Clear(head);
// Display(head);
return 0;
}
//创建头节点
Photo * Create_List() {
Photo * head = (Photo*)malloc(sizeof(Photo));//创建一个头节点
if(head != NULL)
{
head->next = NULL;
}
return head;
}
//头部插入
int Head_Insert(Photo * head, char * buffer)
{
Photo *New_Node = (Photo *)malloc(sizeof(Photo));
if(New_Node == NULL)
{
printf("分配内存失败\r\n");
return -1;
}
New_Node->next = head->next;
head->next = New_Node;
strcpy((char *)New_Node->Capacity,buffer);//暂时不考虑数据过限情况
return 0;
}
//头部删除节点
int Head_Delete(Photo * head)
{
if(head->next == NULL)
{
printf("H只有头节点\r\n");
return -1;
}
Photo *delete_Node;
delete_Node = head->next;
head->next = delete_Node->next;
delete_Node->Capacity = NULL;
free(delete_Node);
return 0;
}
//尾部插入
int Tail_Insert(Photo * head, char * buffer)
{
Photo *New_Node = (Photo *)malloc(sizeof(Photo));
if(New_Node == NULL)
{
printf("分配内存失败\r\n");
return -1;
}
Photo *temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = New_Node;
New_Node->next = NULL;
strcpy((char *)New_Node->Capacity,buffer);//暂时不考虑数据过限情况
return 0;
}
//尾部删除节点
int Tail_Delete(Photo * head)
{
if(head->next == NULL)
{
printf("T只有头节点\r\n");
return -1;
}
int j = 0;
Photo *delete_Node ;
Photo *temp = head;
while (temp->next->next) {
j++;
temp = temp->next;
}
delete_Node = temp ->next;
temp->next = NULL;
free(delete_Node);
return 0;
}
//任意位置插入
int Random_Insert(Photo * head, int number , char *buffer)
{
int j;//用于遍历
Photo *New_Node = (Photo *)malloc(sizeof(Photo));
if(New_Node == NULL)
{
printf("分配内存失败\r\n");
return -1;
}
Photo *temp = head;
for(j = 1;j<number;j++)
{
temp = temp->next;
if(temp == NULL)
{
printf("插入错误\r\n");
return -1;
}
}
New_Node->next = temp->next;
temp->next = New_Node;
strcpy((char *)New_Node->Capacity,buffer);//暂时不考虑数据过限情况
return 0;
}
//任意位置删除
int Random_Delete(Photo * head, int number)
{
if(head->next == NULL)
{
printf("R只有头节点\r\n");
return -1;
}
int j;//用于遍历
Photo *delete_Node;
Photo *temp = head;
for(j = 1;j<number;j++)
{
temp = temp->next;
if(temp->next == NULL)
{
printf("删除错误\r\n");
return -1;
}
}
delete_Node = temp->next;
temp->next = temp->next->next;
free(delete_Node);
return 0;
}
//查找节点(目前是根据链表标号,后期可以根据链表结构体成员进行查找,找到返回成功0并打印其数据,失败提示未找到返回-1)
int Find_Node(Photo *head,int number) {
if(head->next == NULL)
{
printf("只有头节点,无法查找无意义");
return -1;
}
int j = 0;
Photo *temp = head;
for(j = 1;j<number;j++)
{
temp = temp->next;
if(temp->next == NULL)
{
printf("F未找到节点\r\n");
return -1;
}
}
printf("成功找到节点其数据为: %s\r\n ", temp->next->Capacity);
return 0;
}
//更新节点数据
int Update_Node(Photo *head,int number,char *buffer)
{
if(head->next == NULL)
{
printf("只有头节点,修改无意义");
return -1;
}
int j = 0;
Photo *temp = head;
for(j = 1;j<number;j++)
{
temp = temp->next;
if(temp->next == NULL)
{
printf("U未找到节点\r\n");
return -1;
}
}
memset(temp->next->Capacity,0, 1024);
strcpy((char *)temp->next->Capacity,buffer);//暂时不考虑数据过限情况
return 0;
}
//遍历节点
void Display(Photo *head) {
int i = 0;//获取节点个数
Photo* temp = head;
while (temp->next) {
i++;
temp = temp->next;
printf("第%d个节点 %s\r\n ",i,temp->Capacity);
}
printf("节点个数%d\r\n",i);
printf("\n");
}
//销毁链表(下次使用必须重新创建头节点,不然会出现严重错误)
void Destroy(Photo *head)
{
Photo *temp = head;
while(temp != NULL){
head = head->next;
free(temp);
temp = head;
}
}
//清空链表(保留头节点)
void Clear(Photo *head)
{
Photo *temp;
while(head->next != NULL)
{
temp = head->next;
head->next = temp->next;
free(temp);
}
}
单向链表的增删改查
于 2024-08-21 13:41:17 首次发布