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

主要功能的实现:

#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;
}
时间: 2024-08-09 02:20:29

动态链表增删改查及排序功能的相关文章

java servlet开发购物车功能,实现增删改查结算等功能。

原文:java servlet开发购物车功能,实现增删改查结算等功能. 源代码下载地址:http://www.zuidaima.com/share/1550463494130688.htm 购物车功能:增删改查,结算等功能,主要技术为:servlet对数据库的访问... 源代码截图:

Mybatis实现部门表增删改查以及排序

废话不说,直接开门见山! 需要在WebContent下的lib下导入两个包 mybatis-3.2.5.jar ojdbc6.jar 1 package com.xdl.entity; 2 3 import java.io.Serializable; 4 5 public class Dept implements Serializable{ 6 private Integer deptno;//类型和名称与表保持一致 7 private String dname; 8 private Stri

java实现单链表增删改查

package 数据结构算法.链表; /* *定义节点 * 链表由节点构成 */ public class Node<E> { private E e; //数据data private Node<E> next; //指向下一个节点 public Node() { } public Node(E e) { this.e = e; } public Node<E> getNext() { return next; } public void setNext(Node&l

单向链表增删改查的实现

/*实现单向链表的增删改查 */#include <malloc.h>#include <stdio.h>#include<stdlib.h>#define LEN sizeof(node) typedef struct node{ int num; struct node *next;}node,*pnode; /*在链表头进行插入,新节点成为头节点,原头节点成为新节点的下一个节点,头节点指针后伊一位*/void insert_head(pnode *phead,in

python3-list列表增删改查合并排序

# Auther: Aaron Fan names = ["aaron", "alex", "james", "meihengfan"]names2 = [1,2,3,4,5]print(names) #查#print(names) #列出列表的内容print(names[3]) #访问列表中第4个值print(names[1:3]) #访问列表中从第2个到第3个的值print(names[-1]) #访问列表中的最后一个值p

基于双向链表的增删改查和排序(C++实现)

双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.一般我们都构造双向循环链表. 由于双向链表可以方便地实现正序和逆序两个方向的插入.查找等功能,在很多算法中经常被使用, 这里用C++构造了一个双向链表,提供了对双向链表的插入.查找.删除节点.排序等功能,其中排序提供了插入排序和冒泡排序两种方式 1 #include<iostream> 2 3 using namespac

[python] 用pickle模块实现“增删改查”的简易功能

#!/usr/bin/env python2  #coding:utf-8  """ pickle的作用:  1:pickle.dump(dict,file)把字典转为二进制存入文件. 2:pickle.load(file)把文件二进制内容转为字典.  """ import pickle # 增  def adds():      users = {"name":"yangbin", "age&q

使用cglib和JAVA反射实现AOP操作数据库增删改查的简单功能

对mybatis理解的还不是特别深刻,只会简单的使用,实现这个功能跟平常使用spring+mybatis时的DAO操作有点类似,spring+mybatis具体内部实现还不清楚,后续要继续学习 代码: 1. 实体类: 1 package com.mrlu.concurrency.domain; 2 3 /** 4 * Created by stefan on 15-12-19. 5 */ 6 public class Goods { 7 private Integer id; 8 private

JavaScript数组:增删改查、排序等

直接上代码 // 数组应用 var peoples = ["Jack","Tom","William","Tod","Cart","Jhson"]; console.log('原始:'+'length('+ peoples.length +')==' + peoples); // push(元素),从尾部添加 peoples.push("Smith","Wo