#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum{FALSE, TRUE, ERROR} BOOL;
typedef struct _Data
{
int id;
char *name;
}Data;
typedef struct _Node
{
Data data;
struct _Node *next;
}Node;
Node *CreateHead(Node **node)
{
if(NULL == node)
{
return NULL;
}
Node *head = (Node *)malloc(sizeof(Node)/sizeof(char));
if(NULL == head)
{
//分配头结点head的空间失败
return NULL;
}
head->data.name = (char *)malloc(sizeof(char)*15);
if(head->data.name == NULL)
{
//分配指针name的空间失败
free(head);
return NULL;
}
head->data.id = 0;
strcpy(head->data.name,"head");
head->next = NULL;
*node = head;
return head;
}
BOOL Insert_tail(Node *head, Data data)
{
if(NULL == head)
{
return ERROR;
}
Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
if(NULL == node)
{
return ERROR;
}
node->data.name = (char *)malloc(sizeof(char)*15);
node->data.id = data.id;
stpcpy(node->data.name, data.name);
node->next = NULL;
Node *tmp = head;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = node;
return TRUE;
}
void Print_Data(Data data)
{
printf("id = %-4d,name = %s\n", data.id, data.name);
}
void Display(Node *head)
{
if(NULL == head)
return ;
Node *tmp = head->next;
while(tmp != NULL)
{
Print_Data(tmp->data);
tmp = tmp->next;
}
}
BOOL Delete_Data(Node *head, Data data)
{
if(NULL == head)
return ERROR;
Node *tmp = head;
while(tmp->next != NULL)
{
if(strcmp(tmp->next->data.name, data.name) == 0)
{
Node *p = tmp->next;
tmp->next = p->next;
free(p->data.name);
free(p);
return FALSE;
}
tmp = tmp->next;
}
return TRUE;
}
BOOL Updata_Data(Node *head, Data data)
{
if(NULL == head)
{
return ERROR;
}
Node *tmp = head;
while(tmp->next != NULL)
{
if(strcmp(tmp->next->data.name, data.name) == 0)
{
printf("名字修改为:");
scanf("%s", tmp->next->data.name);
printf("修改成功\n");
return TRUE;
}
tmp = tmp->next;
}
}
int Length(Node *head)
{
if(NULL == head)
{
return -1;
}
int length = 0;
Node *tmp = head;
while(tmp->next != NULL)
{
length++;
tmp = tmp->next;
}
return length;
}
void Reverse(Node *head)
{
if(NULL == head || NULL == head->next || NULL == head->next->next)
{
return ;
}
Node *pre = head->next;
Node *cur = pre->next;
Node *tmp = cur->next;
while(cur != NULL)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
head->next->next = NULL;
head->next = pre;
}
int main()
{
Node *head2 = NULL;
Node *head1 = CreateHead(&head2);
if(NULL == head1)
{
printf("head1初始化失败\n");
}
if(NULL == head2)
{
printf("head2初始化失败\n");
}
Data data1;
data1.id = 1;
data1.name = (char *)malloc(sizeof(char)*15);
strcpy(data1.name, "zoujie");
Data data2;
data2.id = 2;
char name2[15] = "luzhiwei";
data2.name = name2;
Insert_tail(head1, data1);
Display(head1);
printf("1--------------------------\n");
Insert_tail(head1, data2);
Display(head1);
printf("length = %d\n", Length(head1));
printf("2--------------------------\n");
Reverse(head1);
Display(head1);
printf("3--------------------------\n");
Delete_Data(head1, data1);
Display(head1);
printf("4--------------------------\n");
Updata_Data(head1, data2);
Display(head1);
printf("length = %d\n", Length(head1));
printf("5--------------------------\n");
return 0;
}
又写了一遍单链表
最新推荐文章于 2022-11-08 15:45:00 发布