动态链表增删改查及排序功能

主要功能的实现:

#include "SeqList.h"
void InitSeqList(SeqList * pSeq)//初始化
{
	assert(pSeq);
	pSeq->array = (DataType*)malloc(sizeof(DataType)*DEFAULT_CAPICITY);
	pSeq->size = 0;
	pSeq->capicity = DEFAULT_CAPICITY;
}
void PrintSeqList(SeqList* pSeq)//打印
{
	assert(pSeq);
	size_t i = 0;
	for (; i < pSeq->size; i++)
	{
		printf("%d", pSeq->array[i]);
	}
	printf("\n");
}

void CheckExpandCapicity(SeqList* pSeq)//检查容量
{
	assert(pSeq);
	if (pSeq->size == pSeq->capicity)
	{
		DataType *tmp = (DataType *)malloc(pSeq->capicity * 2 * sizeof(DataType));
		memcpy(tmp, pSeq->array, sizeof(DataType)*pSeq->size);
		free(pSeq->array);
		pSeq->array = tmp;
		pSeq->capicity = pSeq->capicity * 2;
	}
}
void PushFront(SeqList* pSeq, DataType x)//头插
{
	int i = 0;
	assert(pSeq);
	CheckExpandCapicity(pSeq);
	for (i = pSeq->size; i >= 1; i--)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}
	pSeq->array[0] = x;
	pSeq->size++;
}

void PopFront(SeqList* pSeq)//头删
{
	size_t i = 0;
	assert(pSeq);
	for (; i < pSeq->size - 1; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;

}
void PushBack(SeqList* pSeq, DataType x)//尾插
{
	assert(pSeq);
	CheckExpandCapicity(pSeq);
	pSeq->array[pSeq->size] = x;
	pSeq->size++;
}
void PopBack(SeqList* pSeq)//尾删
{
	assert(pSeq);
	pSeq->size--;
}

void Insert(SeqList* pSeq, size_t index, DataType x)//在index位置插入
{
	size_t i = pSeq->size;
	assert(pSeq);
	assert(index < pSeq->size);
	CheckExpandCapicity(pSeq);
	for (; i >index; i--)
	{
		pSeq->array[i] = pSeq->array[i - 1];
	}
	pSeq->array[index] = x;
	pSeq->size++;
}
void Modify(SeqList* pSeq, size_t index, DataType x)//修改
{
	assert(pSeq);
	assert(index < pSeq->size);
	pSeq->array[index] = x;
}
void Remove(SeqList* pSeq, size_t index)//删除index位置的数
{
	size_t i = index;
	assert(pSeq);
	assert(index < pSeq->size);
	for (; i < pSeq->size - 1; i++)
	{
		pSeq->array[i] = pSeq->array[i + 1];
	}
	pSeq->size--;
}
void Swap(DataType* left, DataType* right)
{
	DataType tmp = *left;
	*left = *right;
	*right = tmp;
}
void BubbleSort(SeqList* pSeq)//冒泡排序
{
	size_t index, end;
	int exchange = 0;
	assert(pSeq);
	for (end = pSeq->size - 1; end > 0; --end)
	{
		exchange = 0;
		for (index = 0; index < end; index++)
		{
			if (pSeq->array[index]>pSeq->array[index + 1])
			{
				Swap(pSeq->array + index, pSeq->array + index + 1);
				exchange = 1;
			}
		}
		if (exchange == 0)
		{
			break;
		}
	}
}
void SelectSort(SeqList* pSeq)//选择排序
{
	size_t MinIndex, index, begin;
	assert(pSeq);
	for (begin = 0; begin < pSeq->size - 1; begin++)
	{
		MinIndex = begin;
		for (index = begin + 1; index < pSeq->size; index++)
		{
			if (pSeq->array[MinIndex]>pSeq->array[index])
			{
				MinIndex = index;
			}
		}
		if (MinIndex != begin)
		{
			Swap(pSeq->array + MinIndex, pSeq->array + begin);
		}
	}
}
FindRet BinarySearch(SeqList* pSeq, DataType x)//二分查找
{
	size_t left=0;
	size_t right = pSeq->size - 1;;
	size_t middle;
	FindRet ret;
	ret.isFind = FALSE;
	assert(pSeq);
	while (left<=right)
	{
		middle = (left + right) / 2;
		if (x == pSeq->array[middle])
		{
			ret.isFind = TRUE;
			ret.index = middle;
			return ret;
		}
		else if (x>pSeq->array[middle])
			left = middle + 1;
		else
			right = middle - 1;
	}
	return ret;
}

头文件:

#pragma once

#define DEFAULT_CAPICITY 3
typedef int DataType;

#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<malloc.h>

typedef struct SeqList
{
	DataType *array;
	size_t size;
	size_t capicity;//当前的容量
}SeqList;

typedef enum Tag
{
	TRUE,	// 真
	FALSE,	// 假
}Tag;

typedef struct FindRet
{
	Tag isFind;		// 是否找到的标示
	size_t index;	// 找到数据的下标
}FindRet;


void InitSeqList(SeqList *pSeq);
void PrintSeqList(SeqList *pSeq);

void CheckExpandCapicity(SeqList* pSeq);

void PushFront(SeqList *pSeq, DataType x);
void PopFront(SeqList *pSeq);

void PushBack(SeqList *pSeq, DataType x);
void PopBack(SeqList *pSeq);

void Insert(SeqList *pSeq, size_t index, DataType x);
void Modify(SeqList *pSeq, size_t index, DataType x);
void Remove(SeqList *pSeq, size_t index);

void Swap(DataType* left, DataType* right);
void BubbleSort(SeqList* pSeq);
void SelectSort(SeqList* pSeq);
FindRet BinarySearch(SeqList* pSep, DataType x);

测试程序部分:

#include "SeqList.h"

void test1()//测试初始化、打印、尾插/尾删
{
	SeqList s;
	InitSeqList(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PrintSeqList(&s);
	PopBack(&s);
	PrintSeqList(&s);

}
void test2()//测试头插、头删
{
	SeqList s;
	InitSeqList(&s);
	PushFront(&s, 3);
	PushFront(&s, 4);
	PushFront(&s, 5);
	PushFront(&s, 6);
	PrintSeqList(&s);
	PopFront(&s);
	PrintSeqList(&s);
}
void test3()//测试在index位置插入、修改index位置的值、删除index位置的值
{
	SeqList s;
	InitSeqList(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PrintSeqList(&s);
	Insert(&s, 2, 8);
	PrintSeqList(&s);
	Modify(&s,2,5);
	PrintSeqList(&s);
	Remove(&s, 2);
	PrintSeqList(&s);
}
void test4()//测试冒泡排序
{
	SeqList s;
	InitSeqList(&s);
	PushBack(&s, 3);
	PushBack(&s, 1);
	PushBack(&s, 5);
	PushBack(&s, 4);
	PushBack(&s, 2);
	PrintSeqList(&s);
	BubbleSort(&s);
	PrintSeqList(&s);
}
void test5()//测试选择排序
{
	SeqList s;
	InitSeqList(&s);
	PushBack(&s, 3);
	PushBack(&s, 1);
	PushBack(&s, 5);
	PushBack(&s, 4);
	PushBack(&s, 2);
	PrintSeqList(&s);
	SelectSort(&s);
	PrintSeqList(&s);
}
void test6()//测试二分搜索
{
	DataType x;
	FindRet ret;
	SeqList s;
	InitSeqList(&s);
	PushBack(&s, 1);
	PushBack(&s, 2);
	PushBack(&s, 3);
	PushBack(&s, 4);
	PushBack(&s, 5);

	x = 4;
	ret = BinarySearch(&s, x);
	if (ret.isFind == TRUE)
	{
		printf("%d %d\n",x,ret.index);
	}
	else
		printf("find failed!\n");
	x = 8;
	ret = BinarySearch(&s, x);
	if (ret.isFind == TRUE)
	{
		printf("%d %d", x, ret.index);
	}
	else
		printf("find failed!\n");
	x = 1;
	ret = BinarySearch(&s, x);
	if (ret.isFind == TRUE)
	{
		printf("%d %d\n", x, ret.index);
	}
	else
		printf("find failed!\n");
	x = 5;
	ret = BinarySearch(&s, x);
	if (ret.isFind == TRUE)
	{
		printf("%d %d\n", x, ret.index);
	}
	else
		printf("find failed!\n");


}
int main()
{
	test1();
	test2();
	//test3();
	test4();
	test5();
	test6();
	getchar();
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的航班管理系统的实现,包括链表的设计、航班的增删改查排序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义航班结构体 typedef struct Flight { char flight_no[20]; // 航班号 char departure[50]; // 出发地 char destination[50]; // 目的地 char date[20]; // 日期 int price; // 价格 struct Flight *next; // 指向下一个节点的指针 } Flight; // 创建一个新的航班节点 Flight *new_flight(char *flight_no, char *departure, char *destination, char *date, int price) { Flight *flight = (Flight *)malloc(sizeof(Flight)); strcpy(flight->flight_no, flight_no); strcpy(flight->departure, departure); strcpy(flight->destination, destination); strcpy(flight->date, date); flight->price = price; flight->next = NULL; return flight; } // 在链表末尾添加一个航班 void add_flight(Flight **head, Flight *flight) { if (*head == NULL) { *head = flight; } else { Flight *cur = *head; while (cur->next != NULL) { cur = cur->next; } cur->next = flight; } } // 删除指定航班号的航班 void delete_flight(Flight **head, char *flight_no) { Flight *cur = *head; Flight *prev = NULL; while (cur != NULL) { if (strcmp(cur->flight_no, flight_no) == 0) { if (prev == NULL) { *head = cur->next; } else { prev->next = cur->next; } free(cur); return; } prev = cur; cur = cur->next; } } // 修改指定航班号的航班信息 void update_flight(Flight *head, char *flight_no) { Flight *cur = head; while (cur != NULL) { if (strcmp(cur->flight_no, flight_no) == 0) { printf("请输入新的出发地: "); scanf("%s", cur->departure); printf("请输入新的目的地: "); scanf("%s", cur->destination); printf("请输入新的日期: "); scanf("%s", cur->date); printf("请输入新的价格: "); scanf("%d", &cur->price); return; } cur = cur->next; } } // 根据价格从小到大对航班进行排序 void sort_flights(Flight **head) { Flight *cur, *cur2; char temp[20]; int temp_price; for (cur = *head; cur != NULL; cur = cur->next) { for (cur2 = cur->next; cur2 != NULL; cur2 = cur2->next) { if (cur->price > cur2->price) { strcpy(temp, cur->flight_no); strcpy(cur->flight_no, cur2->flight_no); strcpy(cur2->flight_no, temp); strcpy(temp, cur->departure); strcpy(cur->departure, cur2->departure); strcpy(cur2->departure, temp); strcpy(temp, cur->destination); strcpy(cur->destination, cur2->destination); strcpy(cur2->destination, temp); strcpy(temp, cur->date); strcpy(cur->date, cur2->date); strcpy(cur2->date, temp); temp_price = cur->price; cur->price = cur2->price; cur2->price = temp_price; } } } } // 输出所有航班信息 void print_flights(Flight *head) { if (head == NULL) { printf("没有航班信息!\n"); } else { Flight *cur = head; printf("航班号\t出发地\t目的地\t日期\t价格\n"); while (cur != NULL) { printf("%s\t%s\t%s\t%s\t%d\n", cur->flight_no, cur->departure, cur->destination, cur->date, cur->price); cur = cur->next; } } } int main() { Flight *head = NULL; int choice; char flight_no[20]; char departure[50]; char destination[50]; char date[20]; int price; while (1) { printf("\n请选择操作:\n"); printf("1. 添加航班\n"); printf("2. 删除航班\n"); printf("3. 修改航班信息\n"); printf("4. 排序航班\n"); printf("5. 查看航班信息\n"); printf("6. 退出程序\n"); printf("请输入操作编号: "); scanf("%d", &choice); switch (choice) { case 1: printf("请输入航班号: "); scanf("%s", flight_no); printf("请输入出发地: "); scanf("%s", departure); printf("请输入目的地: "); scanf("%s", destination); printf("请输入日期: "); scanf("%s", date); printf("请输入价格: "); scanf("%d", &price); add_flight(&head, new_flight(flight_no, departure, destination, date, price)); break; case 2: printf("请输入要删除的航班号: "); scanf("%s", flight_no); delete_flight(&head, flight_no); break; case 3: printf("请输入要修改的航班号: "); scanf("%s", flight_no); update_flight(head, flight_no); break; case 4: sort_flights(&head); break; case 5: print_flights(head); break; case 6: printf("谢谢使用!\n"); return 0; default: printf("无效操作!\n"); break; } } } ``` 这个程序中使用了链表来存储航班信息,每个节点表示一个航班。通过 add_flight 函数可以添加航班,delete_flight 函数可以删除航班,update_flight 函数可以修改航班信息,print_flights 函数可以输出所有航班信息。 另外,sort_flights 函数可以根据价格从小到大对航班进行排序。这个函数使用了冒泡排序算法来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值