Linux顺序表实现,通过选择实现插入、查找、排序等功能

1.头文件

#ifndef _MYLIST_H
#define _MYLIST_H

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

typedef int DATA;
typedef struct mylist
{
	DATA *phead;
	int size;
	int count;
}MYLIST;

MYLIST *create(int size);
int destroy(MYLIST **pplist);
void input(MYLIST *plist);
void output(MYLIST *plist);
void insret(MYLIST *plist);
void change(MYLIST *plist);
void deletef(MYLIST *plist);
void find(MYLIST *plist);
void sort(MYLIST *plist);

#endif

2.主函数

#include <stdio.h>
#include "mylist.h"

int main(int argc, const char *argv[])
{
	MYLIST *plist = NULL;
	int size = 100;
	plist = create(size);
	if(NULL==plist || NULL==plist->phead)
	{
		puts("create fail!");
		return -1;
	}

	int OP = 0;


	while (1)
	{
		printf("0----quit\n");
		printf("1----input\n");
		printf("2----insret\n");
		printf("3----change\n");
		printf("4----delete\n");
		printf("5----find\n");
		printf("6----sort\n");
		printf("input operation:\n");
		printf("***********************\n");
		scanf("%d",&OP);

		switch (OP)
		{
			case 0:
				printf("Quit!\n");
				return 0;
			case 1:
				input(plist);
				break;
			case 2:
				insret(plist);
				break;
			case 3:
				change(plist);
				break;
			case 4:
				deletef(plist);
				break;
			case 5:
				find(plist);
				break;
			case 6:
				sort(plist);
				break;
			default:
				printf("input operation error!\n");
				break;
		}
	}

	destory(&plist);
	return 0;
}

3.其他子函数

#include <stdio.h>
#include "mylist.h"

MYLIST *create(int size)
{
	MYLIST *plist = NULL;
	plist = (MYLIST *)malloc(sizeof(MYLIST));
	if (NULL == plist)
	{
		puts("create mylist struct fail!");
		return NULL;
	}

	plist->phead = (DATA *)malloc(sizeof(DATA)*size);
	if (NULL == plist->phead)
	{
		puts("create mylist fail!");
		return NULL;
	}
	plist->count = 0;

	return plist;
}

int destory(MYLIST **pplist)
{
	free((*pplist)->phead);
	free(*pplist);

	return 0;
}

void input(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("input five number:");
		plist->count = 5;
		int i = 0;
		for (i=0;i<5;i++)
		{
			scanf("%d",&plist->phead[i]);
		}
		
		output(plist);
	}
	else
	{
		puts("count != 0,input other operation:\n");
	}
}

void output(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!\n");
		return;
	}
	int i = 0;
	for (i=0;i<plist->count;i++)
	{
		printf("%d->",plist->phead[i]);
	}
	printf("NULL\n");
	printf("plist->count = %d\n",plist->count);
}

void insret(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!");
		return;
	}
	printf("mylist count is %d\n",plist->count);
	int i = 0;
	int insid = 0;
	DATA content = 0;
	printf("input insid and content:\n");
	scanf("%d%d",&insid,&content);
	if (insid<0 || insid>plist->count)
	{
		puts("insid out of range!");
		return;
	}

	if (insid == plist->count)
	{
		plist->phead[insid] = content;
	}
	if (insid<plist->count)
	{
		i = plist->count-1;
		for (;i>=insid;i--)
		{
			plist->phead[i+1] = plist->phead[i];
		}
		plist->phead[insid] = content;
	}
	plist->count += 1;
	output(plist);
}

void change(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!\n");
		return;
	}
	int insid = 0;
	int content = 0;
	printf("input insid and content:\n");
	scanf("%d%d",&insid,&content);
	if (insid<0 || insid>=plist->count)
	{
		puts("insid out of range!");
		return;
	}
	plist->phead[insid] = content;
	output(plist);
}

void deletef(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!\n");
		return;
	}
	int insid = 0;
	printf("input insid:\n");
	scanf("%d",&insid);
	if (insid<0 || insid>=plist->count)
	{
		puts("insid out of range!");
		return;
	}
	int i = insid;
	for (;i<plist->count;i++)
	{
		plist->phead[i] = plist->phead[i+1];
	}
	plist->count -=1;
	output(plist);
}

void find(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!\n");
		return;
	}
	int insid = 0;
	int num = 0;
	printf("input insid\n");
	scanf("%d",&insid);
	if (insid<0 || insid>=plist->count)
	{
		puts("insid out of range!");
		return;
	}
	num = plist->phead[insid];
	printf("plist->phead[%d] = %d\n",insid,num);
}

void sort(MYLIST *plist)
{
	if (plist->count == 0)
	{
		puts("no content!\n");
		return;
	}
	int i = 0;
	int j = 0;
	DATA temp = 0;
	int OP = plist->count;
	for (i=0;i<OP-1;i++)
	{
		for(j=0;j<OP-1-i;j++)
		{
			if (plist->phead[j] > plist->phead[j+1])
			{
				temp = plist->phead[j];
				plist->phead[j] = plist->phead[j+1];
				plist->phead[j+1] = temp;
			}
		}
	}
	output(plist);
}

4.程序运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值