单链表 增删改查

/*************************************************************************
    > File Name: 3.c
    > Author: zz
    > Mail: zzzkkk@outlook.com 
    > Created Time: 2017年05月20日 星期六 14时40分05秒
 ************************************************************************/

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

#define T 1
#define F -1

typedef int Status;
typedef int ElementType;

struct Node
{
	ElementType value;
	struct Node* next;
};

Status init(struct Node** head);
Status insert_head(struct Node* head, ElementType value);
Status insert_tail(struct Node* head, ElementType value);
Status insert_index(struct Node*head, ElementType value, int index);
Status delete_index(struct Node* head, int index);
Status delete_value(struct Node* head, ElementType value);
Status update_index(struct Node*head, int index, ElementType value);
void update_value(struct Node*head, ElementType old_value, ElementType new_value);
void query_value(struct Node* head, ElementType value);
Status query_index(struct Node* head, int index);
int length(struct Node* head);
void print(struct Node* head);

int main()
{
	int i;
	int ret = 0;
    
	struct Node* head = NULL;
	ret = init(&head);
	if (F == ret)
	{
		return 1;
	}

    for (i = 0; i < 10; i++)
	{
		insert_head(head, i);
	}
    for (i = 0; i < 10; i++)
	{
		insert_tail(head, i);
	}

	print(head);

	printf("length = %d\n", length(head));

	delete_index(head, length(head) - 1);
	delete_index(head, 0);
	delete_index(head, 5);
	print(head);

	insert_index(head, 99, 5);
	insert_index(head, 99, 0);
	insert_index(head, 99, length(head));
	print(head);

	delete_value(head, 0);
	print(head);

	update_index(head, 4, -1);
	print(head);
	
	update_value(head, 99, 100);
	print(head);
	
	query_value(head, 100);
	query_value(head, 99);

	query_index(head, 10);

	return 0;
}

Status init(struct Node** head)
{
	struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
	if (NULL == newnode)
	{
		return F;
	}
	newnode->value = 0;
	newnode->next = NULL;
    (*head) = newnode;
	
	return T;
}


Status insert_head(struct Node* head, ElementType value)
{
	struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
	if (NULL == newnode)
	{
		return F;
	}
    
	newnode->value = value;
    newnode->next = head->next;
	head->next = newnode;

	return T;
}

Status insert_tail(struct Node* head, ElementType value)
{
	struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
	if (NULL == newnode)
	{
		return F;
	}
    
	newnode->value = value;
	newnode->next = NULL;

	while (head->next != NULL)
	{
		head = head->next;
	}

	head->next = newnode;
	return T;
}

Status insert_index(struct Node*head, ElementType value, int index)
{
	if (index < 0 || index > length(head))
	{
		printf("out of range\n");
		return F;
	}

	int i;
	for (i = 0; i < index; i++)
	{
		head = head->next;
	}

	struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
	if (NULL == newnode)
	{
		return F;
	}

	newnode->value = value;
	newnode->next = head->next;
	head->next = newnode;

	return T;
}

Status delete_index(struct Node* head, int index)
{
	if (index < 0 || index >= length(head))
	{
		printf("out of range\n");
		return F;
	}
	int i;
	for (i = 0; i < index; i++)
	{
		head = head->next;
	}

	struct Node* temp = head->next->next;
	free(head->next);
	head->next = temp;
	return T;
}

Status delete_value(struct Node* head, ElementType value)
{
    int len = length(head);
	int i;
	for (i = 0; i < len; i++)
	{
		printf("i = %d value = %d\n", i, head->next->value);
		if (head->next->value == value)
		{
			struct Node* temp = head->next->next;
			free(head->next);
			head->next = temp;
		}
		else
		{
			head = head->next;
	
		}
	}
	return T;
}

Status update_index(struct Node*head, int index, ElementType value)
{
	if (index < 0 || index >= length(head))
	{
		printf("out of rangr\n");
		return F;
	}
	int i;
	for (i = 0; i <= index; i++)
	{
		head = head->next;
	}
	head->value = value;
	return T;
}


void update_value(struct Node*head, ElementType old_value, ElementType new_value)
{
	int i;
	while (head->next != NULL)
	{
		if (head->next->value == old_value)
		{
			head->next->value = new_value;
		}
		head = head->next;
	}
}

void query_value(struct Node* head, ElementType value)
{
	int count = 0;
	int index = 0;
	while (head->next != NULL)
	{
		if (head->next->value == value)
		{
			count++;
			printf("find %d : %d\n", value, index);
		}	
		head = head->next;
		index++;
	}
	if (0 == count)
	{
		printf("not found %d\n", value);
	}
}

Status query_index(struct Node* head, int index)
{
	if (index < 0 || index >= length(head))
	{
		printf("out of rangr\n");
		return F;
	}
	int i;
	for (i = 0; i <= index; i++)
	{
		head = head->next;
	}

	printf("index = %d value =%d\n", index, head->value);
	return T;
}

int length(struct Node* head)
{
	int count = 0;
	while (head->next != NULL)
	{
		count++;
		head = head->next;
	}
	return count;
}

void print(struct Node* head)
{
	while (head->next != NULL)
	{
		printf("%d ", head->next->value);
		head = head->next;
	}
	printf("\n");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答这个问题。 首先,我们可以定义一个火车售票的节点类,包含以下属性: - 车次号 - 出发站点 - 到达站点 - 出发时间 - 到达时间 - 座位类型 - 票价 - 下一节点指针 接下来,我们可以定义一个火车售票单链表类,实现以下方法: 1. 增加节点:在链表末尾添加节点 2. 删除节点:根据车次号删除指定节点 3. 修节点:根据车次号修指定节点 4. 查询节点:根据车次号查询指定节点信息 代码实现如下: ```python class TrainTicketNode: def __init__(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): self.train_num = train_num self.start_station = start_station self.end_station = end_station self.start_time = start_time self.end_time = end_time self.seat_type = seat_type self.ticket_price = ticket_price self.next = None class TrainTicketLinkedList: def __init__(self): self.head = None # 添加节点 def add_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): new_node = TrainTicketNode(train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price) if self.head is None: self.head = new_node else: current_node = self.head while current_node.next is not None: current_node = current_node.next current_node.next = new_node # 删除节点 def delete_node(self, train_num): if self.head is None: print("链表为空") else: if self.head.train_num == train_num: self.head = self.head.next else: current_node = self.head while current_node.next is not None and current_node.next.train_num != train_num: current_node = current_node.next if current_node.next is None: print("未找到该车次信息") else: current_node.next = current_node.next.next # 修节点 def modify_node(self, train_num, start_station, end_station, start_time, end_time, seat_type, ticket_price): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: current_node.start_station = start_station current_node.end_station = end_station current_node.start_time = start_time current_node.end_time = end_time current_node.seat_type = seat_type current_node.ticket_price = ticket_price # 查询节点 def search_node(self, train_num): if self.head is None: print("链表为空") else: current_node = self.head while current_node is not None and current_node.train_num != train_num: current_node = current_node.next if current_node is None: print("未找到该车次信息") else: print("车次号:{}\n出发站点:{}\n到达站点:{}\n出发时间:{}\n到达时间:{}\n座位类型:{}\n票价:{}".format( current_node.train_num, current_node.start_station, current_node.end_station, current_node.start_time, current_node.end_time, current_node.seat_type, current_node.ticket_price)) ``` 这样,我们就实现了火车售票单链表增删改查功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值