加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 06:02:16 所属栏目:安全 来源:网络整理
导读:数据结构 之 单链表的顺序存储实现 闲来无事,回顾下以前学过的数据结构,写个玩玩,理论的东西就不多说了,网上一搜一大堆; 重要的是需要掌握这种数据结构的思想,整个数据结构这门课最重要的也是思想! 下面是代码: //=================================


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

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

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

下面是代码:

//======================================================================  
//  
//        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] = %dn",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("nn");	
	return 0;
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读