【数据结构】顺序存储单链表

前端之家收集整理的这篇文章主要介绍了【数据结构】顺序存储单链表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


数据结构单链表的顺序存储实现

闲来无事,回顾下以前学过的数据结构,写个玩玩,理论的东西就不多说了,网上一搜一大堆;

重要的是需要掌握这种数据结构的思想,整个数据结构这门课最重要的也是思想!

下面是代码

//======================================================================  
//  
//        Copyright (C) 2014-2015 SCOTT      
//        All rights reserved  
//  
//        filename: SeqList.c
//        description: a demo to display SeqList
//  
//        created by SCOTT at  01/26/2015 
//        http://blog.csdn.net/scottly1
//  
//======================================================================  

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

#define TRUE			1
#define FALSE			0
#define MAX_SIZE		1000

typedef int Type;
typedef int Bool;

typedef struct _tag_list
{
	Type *elem;
	int length;			// current length
	int size;			// max length
}LIST,*PLIST;


Bool createList(LIST *list,int len)
{
	if(len > MAX_SIZE || len <= 0 )
	{
		printf("### OVER MAX_LEN!\n");
		return FALSE;
	}

	list->elem = (Type *)malloc(len * sizeof(Type));
	if(NULL == list)
	{
		printf("### createList Create Error!\n");
		return FALSE;
	}
	else
	{		
		list->length = 0;
		list->size = MAX_SIZE;
	}

	printf("### createList Create list Success!\n");
	return TRUE;
}


Bool clearList(LIST *list)
{
	if(NULL == list)
	{
		printf("### clearList List Error!\n");
		return FALSE;
	}

	Type *p = list->elem;

	for(; p < list->elem + list->size; ++p)
	{
		memset(p,sizeof(*p));
	}

	return TRUE;
}


Bool displayList(LIST *list)
{
	if(NULL == list)
	{
		printf("### displayList List Error!\n");
		return FALSE;
	}

	int i = 0;
	Type *p = list->elem;

	for(; p < list->elem + list->length; ++p,i++)
	{
		printf("list->elem[%d] = %d\n",i,*p);
	}

	return TRUE;
}


Bool insertList(LIST *list,int flag,Type elem)
{
	int i = 0;

	if(NULL == list || flag < 0 || flag >= list->size)
	{
		printf("### insertList List Error!\n");
		return FALSE;
	}

	if(list->length + 1 > list->size)
	{
		printf("### List Full!\n");
		return FALSE;
	}

	flag = flag>list->length?list->length:flag;

	for(i=list->length; i>flag; --i)
	{
		list->elem[i] = list->elem[i-1];
	}

	list->elem[i] = elem;
	++list->length;

	return TRUE;
}


Bool deleteList(LIST *list,Type *elem)
{
	int i = 0;

	if(NULL == list || flag < 0 || flag >= list->size)
	{
		printf("### deleteList List Error!\n");
		return FALSE;
	}

	if(list->length - 1 < 0 )
	{
		printf("### List Empty!\n");
		return FALSE;
	}

	flag = flag>list->length?list->length:flag;
	*elem = list->elem[flag];

	for(i=flag; i<list->length; ++i)
	{
		list->elem[i] = list->elem[i+1];
	}
	
	--list->length;

	return TRUE;
}


Bool mergeList(LIST *src1,LIST *src2,LIST *dst)
{
	int i = 0;
	Type *pA,*pB,*pC;
	Type *pA_last,*pB_last,*pC_last;

	if(NULL == src1 || NULL == src2 || NULL == dst)
	{
		printf("### displayList List Error!\n");
		return FALSE;
	}

	pA = src1->elem; pA_last = src1->elem + src1->length;
	pB = src2->elem; pB_last = src2->elem + src2->length;
	pC = dst->elem;

	while(pA < pA_last && pB < pB_last)
	{
		if(*pA >= *pB)
			*pC++ = *pB++;
		else
			*pC++ = *pA++;
		++dst->length;
	}

	while(pA < pA_last)
	{
		*pC++ = *pA++;
		++dst->length;
	}

	while(pB < pB_last)
	{
		*pC++ = *pB++;
		++dst->length;
	}

	return TRUE;
}



int main(int argc,char *argv[])
{
	LIST list1;
	LIST list2;
	LIST dst;
	Type elem1 = 10;
	Type elem2 = 30;

	createList(&list1,5);
	createList(&list2,5);
	createList(&dst,10);

	printf("===================\n");
	insertList(&list1,elem1);
	insertList(&list1,1,elem1+1);
	insertList(&list1,2,elem1+2);
	insertList(&list1,3,elem1+4);
	insertList(&list1,4,100);
	displayList(&list1);

	printf("===================\n");
	insertList(&list2,elem2+3);
	insertList(&list2,elem2+4);
	insertList(&list2,elem2+5);
	insertList(&list2,elem2+6);
	insertList(&list2,elem2+7);
	displayList(&list2);

	printf("===================\n");
	mergeList(&list1,&list2,&dst);
	displayList(&dst);

	printf("\n\n");	
	return 0;
}

注:原创文章,转载请注明出处:http://blog.csdn.net/scottly1/article/details/43154589

原文链接:https://www.f2er.com/datastructure/382716.html

猜你在找的数据结构相关文章