两种单链表的写法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum {FLASS,TRUE} Bool;

typedef struct Data
{
	int id;
	char name[15];
}Data;

typedef struct Node
{
	struct Data data;
	struct Node *next;
}Node;


typedef struct list
{
	Node *head;
}list;


list *creat()
{
	list *ls = (list *)malloc(sizeof(list)/sizeof(char));
	if (ls == NULL)
		return;
	ls->head = (Node*)malloc(sizeof(Node)/sizeof(char));
	if (ls->head == NULL)
		return;
	ls->head->next = NULL;
	return ls;
}

Bool insertlast(list *ls,Data date)
{
	if (ls == NULL)
		return FLASS;
	Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
		return FLASS;
	node->data.id = date.id;
	strcpy(node->data.name,date.name);
	node->next = NULL;
	Node *tmp = ls->head;
	while (tmp->next)
	{
		tmp = tmp->next;
	}
	tmp->next = node; 
	return TRUE;
}


Bool inserthead(list *ls,Data date)
{
	if (ls == NULL)
		return FLASS;
	Node *node = (Node *)malloc(sizeof(Node)/sizeof(char));
	if (node == NULL)
		return FLASS;
	node->data.id = date.id;
	strcpy(node->data.name,date.name);
	node->next = ls->head->next;
	ls->head->next = node;
	return TRUE;
}

Bool display(list *ls)
{
	if (ls == NULL)
		return FLASS;
	Node *tmp = ls->head;
	while(tmp->next)
	{
		printf ("%d,%s\n",tmp->next->data.id,tmp->next->data.name);
		tmp = tmp->next;
	}
	return TRUE;
}

Bool delete(list *ls)
{
	int i;
	if (ls == NULL)
		return FLASS;
	Node *tmp = ls->head;
	scanf ("%d",&i);
	while (tmp->next)
	{
		if (tmp->next->data.id == i)
		{
			Node *p = tmp->next;
			tmp->next = p->next;
			free(p);
			return TRUE;
		}
		tmp = tmp->next;
	}
	
}

int main()
{
	list *ls = creat();
	Data data1;
	char a[]="hello";
	char b[]="world";
	data1.id = 1;
	strcpy(data1.name,a);
	/* insertlast(ls,data1); */
	inserthead(ls,data1);
	Data data2;
	data2.id = 2;
	strcpy (data2.name,b);
	/* insertlast(ls,data2); */
	inserthead(ls,data2);
	display(ls);
	printf ("----------\n");
	
	delete(ls);
	display(ls);
	return 0;
}

第二种

//链表代码总结
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

//数据:
struct Data
{
	int id;
	//char name[15];
	char *name;	//此时需要注意什么?
};

//结点:
struct Node
{
	struct Data data;
	struct Node *next;
};

//头结点的创建及初始化
struct Node* CreateHead(struct Node **head)
{
	//入口参数检查
	if (*head == NULL)
		return;
	//申请空间
	*head = (struct Node *)malloc(sizeof(struct Node)/sizeof(char));
	(*head)->data.name = (char *)malloc(sizeof(char)*15);
	//申请内存的初始化
	(*head)->data.id = 0;
	(*head)->data.name = NULL; 
	(*head)->next = NULL;
	//返回内存的地址
	return *head;
} 

//链表的尾插
int Insert_tail(struct Node *head, struct Data data)
{
	//入口参数检查
	if (NULL == head)
		return 0;
	//申请新结点的内存
	struct Node *node = (struct Node *)malloc(sizeof(struct Node)/sizeof(char));
	//为内存赋值初始化
	node->data.name = (char *)malloc(sizeof(char)*15);
	
	node->data.id = data.id;
	strcpy(node->data.name,data.name);
	node->next = NULL;
	//遍历链表-将指针定位到链表尾部
	while (head->next)
	{
		head = head->next;
	}
	//改变结点指向,使其插入到链表尾部
	head->next = node;
	//成功返回1
	return 1;
}

//删除结点
int Delete(struct Node *head, struct Data data)
{
	//入口参数检查
	if (head == NULL)
		return 0;
	//遍历-定位到要删除的结点
	//while(){ if()}
	//struct Node *tmp = head->next;
	while (head->next)
	{
		if (head->next->data.id == data.id)
		{
			struct Node *p = head->next;
			head->next = p->next;
			free(p);
			return 1;
		}
		head = head->next;
	}
	
	
	//保存要删除结点的地址!!!
	
	//改变结点指向,是要删除的结点脱离链表
	
	//释放被删除结点的内存!!!
	
	//指向该删除结点的指针置空
	
	//成功返回1 失败返回-1
}

//修改结点信息
int Updata(struct Node *head, struct Data data)
{
	//入口参数检查
	
	//遍历-找到要修改的结点
	
	//进行赋值修改操作
	
	//修改成功 返回1 失败或找不到 返回 -1
}

void PrintData(struct Data data)
{
	printf("%d,%s\n",data.id,data.name);
}

//链表的遍历打印
void Display(struct Node *head)
{
	//入口参数检查
	if (head == NULL)
		return;
	//遍历链表、打印结点
	//遍历:while(...)		for(i = 0; i < Length(head); ++i)
	//打印结点:print()各个参数    PrintData(data)	自己封装一个打印结构体的函数
	//struct Node *tmp = head->next;
	// while (tmp != NULL)
	// {
	// 		PrintData(tmp->data);
	// 		tmp = tmp->next;
	// }
	
	while (head->next)
	{
		PrintData(head->next->data);
		head = head->next;
	}

}

//链表的长度
int Length(struct Node *head)
{
	//入口参数检查
	
	//定义count变量,遍历链表进行计数
	
	//返回count,若链表为空,返回0
}

int main()
{
	struct Node *head;
	CreateHead(&head);
	
	
	struct Data data1;
	data1.name = (char *)malloc(sizeof(char)*15);
	data1.id = 1;
	strcpy(data1.name, "ajef");
	
	
	struct Data data2 = {2,"fajsf"};
	struct Data data3 = {3,"JFJEF"};
	struct Data data4 = {4,"afsjfa"};
	struct Data data5 = {5,"fajfk"};
	
	Insert_tail(head,data1);
	
	Insert_tail(head,data2);
	Insert_tail(head,data3);
	Insert_tail(head,data4);
	Insert_tail(head,data5);
	
	Display(head);
	Delete(head,data1);
	Display(head);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43667336/article/details/85009592