栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

顺序表(C语言实现)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

顺序表(C语言实现)

seq_list.c

#include "seq_list.h"

seq_list* seq_list_create(int capacity)
{
	seq_list *result=0;
	result = malloc( sizeof(seq_list) );
	if (result ==0)
	{
		printf("malloc errorrn");
		return 0;
	}
	
	
	result->capacity = capacity;
	result->len = 0;
	result->node = malloc(sizeof(seq_list_node) * capacity);
	if (result->node == 0)
	{
		free(result);
		printf("malloc errorrn");
		return 0;
	}
	return result;

}

void seq_list_destroy(seq_list *list)
{
	free(list);
}

void seq_list_clear(seq_list *list)
{
	list->len = 0;
}
int seq_list_length(seq_list *list)
{
	return list->len;
}

int seq_list_capacity(seq_list *list)
{
	return list->capacity;
}

int seq_list_insert(seq_list *list,seq_list_node *node,int pos)
{
	int  i;
	
	if (list->capacity <(list->len+1))
	{
		printf("seq_list_insert -1,pos:%d,node:0x%xn",pos,(unsigned int)node);
		return -1;
	}
	
	if (list->len <= pos)
	{
		pos = list->len;
	}
	
	for (i=list->len; i>pos; i--)
	{
		list->node[i] = list->node[i-1];
	}

	list->node[pos] = (seq_list_node)node;//里面装的是地址 
	
	list->len++;
	
	return i;
	
}

seq_list_node *seq_list_get(seq_list *list,int pos)
{
	seq_list_node *result=0;
	
	if (pos >= list->capacity)
		return 0;
	
	if (pos >= list->len)
		return 0;
	
	
	result = (seq_list_node*)list->node[pos];
	return result;
}

seq_list_node *seq_list_delete(seq_list *list,int pos)
{
	int i=0;
	if (pos >= list->capacity)
		return 0;
	
	if (pos >= list->len)
		return 0;
	
	if (list->len == 0)
		return 0;
	
	for (i=pos; i<(list->len-1); i++)// 4  0 1 2 3
	{
		list->node[i] = list->node[i+1];
	}
	list->len--;
	
	
	return 0;
}

void seq_list_printf(seq_list *list)
{
	int i;
	seq_list_node tmp;
	seq_list_node *pdata;
	printf("capacity:%d-len:%d-n",list->capacity,list->len);
	for (i=0; ilen; i++)
	{
		tmp = list->node[i];
		pdata = (seq_list_node*)tmp;
		printf("0x%x-%dn",tmp,pdata[0]);
	}
	printf("n");
	
}

seq_list.h

#ifndef _SEQLIST_H_
#define _SEQLIST_H_

#include "stdio.h"
#include "stdlib.h"


typedef  unsigned int seq_list_node;
typedef struct _tag_seq_list seq_list;

struct _tag_seq_list
{
	int capacity;
	int len;
	seq_list_node *node;
};


seq_list* seq_list_create(int capacity);
void seq_list_destroy(seq_list *list);
void seq_list_clear(seq_list *list);
int seq_list_length(seq_list *list);
int seq_list_capacity(seq_list *list);

int seq_list_insert(seq_list *list,seq_list_node *node,int pos);

seq_list_node *seq_list_get(seq_list *list,int pos);

seq_list_node *seq_list_delete(seq_list *list,int pos);


void seq_list_printf(seq_list *list);

#endif

测试代码


#include "seq_list.h"
#include "stdio.h"
#include "stdint.h"


int main(void)
{

	uint8_t i;
	int tmp[11];
	seq_list *head;
    printf("hello  rn");
	
	head = seq_list_create(10);
	printf("len:%d,capacity:%dn",head->len,head->capacity);
	
	
	for (i=0; i<11; i++)
	{
		tmp[i] = i+1;
	}
	seq_list_insert(head,(seq_list_node*)&tmp[0],0);
	seq_list_insert(head,(seq_list_node*)&tmp[1],0);
	seq_list_insert(head,(seq_list_node*)&tmp[2],0);
	seq_list_insert(head,(seq_list_node*)&tmp[3],0);
	seq_list_insert(head,(seq_list_node*)&tmp[4],0);
	
	seq_list_insert(head,(seq_list_node*)&tmp[5],0);
	seq_list_insert(head,(seq_list_node*)&tmp[6],0);
	seq_list_insert(head,(seq_list_node*)&tmp[7],0);
	seq_list_insert(head,(seq_list_node*)&tmp[8],0);
	seq_list_insert(head,(seq_list_node*)&tmp[9],0);
//	
	seq_list_printf(head);
	printf("seq list capacity:%d-len:%dn",seq_list_capacity(head),seq_list_length(head));
	
	
	printf("delete pos:%dn",8);
	seq_list_delete(head,8);
	seq_list_printf(head);
	
	printf("delete pos:%dn",0);
	seq_list_delete(head,0);
	seq_list_printf(head);
	printf("seq list capacity:%d-len:%dn",seq_list_capacity(head),seq_list_length(head));
	
	while(1)
	{
	
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997823.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号