单链表的基本功能---C语言

单链表的基本功能---C语言_第1张图片

//整个工程所要可能会用到的头文件
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include
#include
#include
#include
#include
#define DataType int
#endif
TextMain.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
int main()
{
	SList mylist;
	SListInit(&mylist);
	int select = 1;
	int flag = 0;
	int pos = 0;
	DataType item = 0;
	DataType x = 0;
	DataType key = 0;
	SListNode *p = NULL;
	while (select)
	{
		printf("*****************************************\n");
		printf("* [1] push_back         [2] push_front  *\n");
		printf("* [3] show_list         [0] quit_system *\n");
		printf("* [4] pop_back          [5] pop_front   *\n");
		printf("* [6] insert_val        [7] delete_val *\n");
		printf("* [8] find_val          [9] sort        *\n");
		printf("* [10] reverse          [11] length     *\n");
		printf("* [12] clear                            *\n");
		printf("*****************************************\n");
		printf("请选择:>");
		scanf("%d", &select);
		if (select == 0)
			break;
		switch (select)
		{
		case 1:
			printf("请输入要插入的数据<-1结束>:>");
			while (scanf("%d", &item), item != -1)
			{
			//尾插
				SListPushBack(&mylist, item);
			}
			break;
		case 2:
			printf("请输入要插入的数据<-1结束>:>");
			while (scanf("%d", &item), item != -1)
			{
			//头插
				SListPushFront(&mylist, item);
			}
			break;
		case 3:
		//显示
			SListShowList(&mylist);
			break;
		case 4:
		//尾删
			SListPopBack(&mylist);
			break;
		case 5:
		//头删
			SListPopFront(&mylist);
			break;
		case 6:
			    	printf("请输入要插入的值:>\n");
				    scanf("%d", &item);
				    //按值插入
					SListInsertVal(&mylist, item);
			break;
		case 7:
			printf("请输入需要删除的值:>\n");
			scanf("%d", &item);
			//按值删除
			SListDeleteVal(&mylist, item);
			break;
		case 8:
			printf("请输入要查找的数据:>\n");
			scanf("%d", &key);
			//按值查找
			p = SListFindVal(&mylist, key);
			if (p == NULL)
				printf("查找的数据不存在.\n");
			else
				printf("查找的数据的地址为:>%p\n", p);
			break;
		case 9:
		//排序
			SListSort(&mylist);
			break;
		case 10:
		//逆置
			SListReverse(&mylist);
			break;
		case 11:
		//长度
			SListLength(&mylist);
			break;
		case 12:
		//清除
			SListClear(&mylist);
			break;
		default:
			printf("输入错误,请重新输入.........\n");
			break;
		}
		system("pause");
		system("cls");
	}
	return 0;
}
SList.h
#ifndef _SLIST_H_
#define _SLIST_H_
#include"common.h"
typedef struct SListNode
{
	DataType data;
	struct SListNode *next;
}SListNode;
typedef struct SList
{
	SListNode *first;
	SListNode *last;
	size_t          size;
}SList;
SListNode * _Buynode(DataType x)
{
	SListNode *s = (SListNode*)malloc(sizeof(SListNode));
	if (s == NULL)
		return NULL;
	s->next = NULL;
	s->data = x;
	return s;
}
void SListInit(SList *plist);
void SListShowList(SList *plist);
bool SListPushBack(SList *plist, DataType x);
bool SListPushFront(SList *plist, DataType x);
bool SListPopBack(SList *plist);
bool SListPopFront(SList *plist);
void SListInsertVal(SList *plist, DataType x);
bool SListDeleteVal(SList *plist, DataType x);
SListNode* SListFindVal(SList *plist, int pos);
void SListSort(SList *plist);
void SListReverse(SList *plist);
size_t SListLength(SList *plist);
void SListClear(SList *plist);
//上面是函数的声明
////////////////////////////////////////////////////////////////
//下面事函数的实现
void SListShowList(SList *plist)
{
	SListNode *p = plist->first->next;
	while (p != NULL)
	{
		printf("%d-->", p->data);
		p = p->next;
	}
	printf("over.\n");
}
void SListInit(SList *plist)
{
	SListNode *s = _Buynode(0);
	plist->first = plist->last = s;
	plist->size = 0;
}
bool SListPushBack(SList *plist, DataType x)
{
	SListNode *s = _Buynode(x);
	if (s == NULL)
		return false;
	plist->last->next = s;
	plist->last = s;
	plist->size++;
	return true;
}
bool SListPushFront(SList *plist, DataType x)
{
	SListNode *s = _Buynode(x);
	if (s == NULL)
		return false;
	s->next = plist->first->next;
	plist->first->next = s;
	if (plist->size == 0)
		plist->last = s;
	plist->size++;
	return true;
}
bool SListPopBack(SList *plist)
{
	SListNode *p = NULL;
	if (plist->size == 0)
		return false;
	p = plist->first;
	while (p->next != plist->last)
		p = p->next;
	p->next = NULL;
	free(plist->last);
	plist->last = p;
	plist->size--;
	return true;
}
bool SListPopFront(SList *plist)
{
	SListNode *p = NULL;
	if (plist->size == 0)
		return false;
	p = plist->first->next;
	plist->first->next = p->next;
	free(p);
	plist->size--;
	if (plist->size == 0)
		plist->last = plist->first;
	return true;
}
void SListInsertVal(SList *plist, DataType x)
{
	SListNode *p = plist->first;
	while (p->next != NULL && x > p->next->data)
		p = p->next;
	SListNode *s = _Buynode(x);
	if (p->next == NULL)
	{
		p->next = s;
		plist->last = s;
	}
	else
	{
		s->next = p->next;
		p->next = s;
	}
	plist->size++;
}
bool SListDeleteVal(SList *plist, DataType x)
{
	SListNode *q;
	SListNode *p = plist->first;
	while (p->next != NULL && p->next->data != x)
		p = p->next;
	if (p->next == NULL)
		return false;
	q = p->next;
	if (p->next == plist->last)
		plist->last = p;
	p->next = q->next;
	free(q);
	plist->size--;
	return true;
}
SListNode* SListFindVal(SList *plist, int key)
{
	SListNode *p = plist->first->next;
	while (p != NULL && p->data != key)
		p = p->next;
	return p;
}
void SListSort(SList *plist)
{
	if (plist->size > 1)
	{
		SListNode *prev;
		SListNode *p = plist->first->next;
		SListNode *q = p->next;
		plist->last = p;
		plist->last->next = NULL;

		p = q;
		while (p != NULL)
		{
			q = q->next;
			prev = plist->first;
			while (prev->next != NULL && p->data>prev->next->data)
				prev = prev->next;

			if (prev->next == NULL)
			{
				prev->next = p;
				plist->last = p;
				p->next = NULL;
			}
			else
			{
				p->next = prev->next;
				prev->next = p;
			}
			p = q;
		}
	}
}
void SListReverse(SList *plist)
{

		SListNode *p = plist->first->next;
		SListNode *q = p->next;
		plist->last = p;
		plist->last->next = NULL;
		p = q;
		while (p != NULL)
		{
			q = p->next;
			p->next = plist->first->next;
			plist->first->next = p;
			p = q;
		}
}
size_t SListLength(SList *plist)
{
	return plist->size;
}
void SListClear(SList *plist)
{
	SListNode *p = plist->first->next;
	while (p != NULL)
	{
		plist->first->next = p->next;
		free(p);
			p = plist->first->next;
	}
	plist->last = plist->first;
	plist->size--;
}
#endif

如有大佬有更好的建议,联系方式1846074273

你可能感兴趣的:(单链表的基本功能---C语言)