Static concept, and code list

A sequence table with different static chain of
1, the physical structure of the sequence table and the static list (i.e., storage structure) are the same, stored in an array in computer memory in linear form, is a set of consecutive addresses of memory cells linear structure sequentially stored data elements, but both data structure (logical structure) are different.
2, the sequence table: one-dimensional array to focus the entire array, dynamic allocation, still by means of the pointer data manipulation, described as follows:

typedef struct
{
int *elem;
int length;
int listsize;
}Sqlist;

When insertions and deletions linear table, you need to move the pointer element.

3, static tables: pointer is not used list structure is used, a component of array elements used to store data, the other used as a "cursor" indicates the relative position of the next node in the array, although the data is stored using a dimensional array stored in the computer, but still not always inherited point list next node point thereof, described as follows:

typedef struct
{
int data;
int cur;
}Component, StaticLinkList[MAXSIZE];

Such a storage structure, still need pre-allocate a large space, but without moving element as insert and delete operations of the linear table, only modified pointer (cursor), it still has the main advantage of the chain memory structure.
Second, the list of static features
to facilitate use of the list structure in the high level programming language not of a pointer type
III shows the code
1, header files

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

2, declares a static list

#define maxsize 100
typedef struct Component
{
	int data;
	int cur;             //cur指向下一个结点
}StaticLinkList[maxsize];//结构体数组

3, the function code

//初始化
void initList(StaticLinkList space)
{
	for(int i=0;i<maxsize;i++)
	{
		space[i].cur=-1;
	}
}
//插入
void Zinsert(StaticLinkList space,int value,int pos)    //value是插入的值,pos是下一个值得位置(注:从零开始)
{
	//判满
	int count=0;
	for(int i=0;i<maxsize;i++)
	{
		if(space[i].cur==-1)
		{
			count++;
		}
	}
	if(count==1)                       //不是一整个循环的前提下。如果是一个循环count=0
	{
		printf("已满,无法进行插入操作!");
		return;
	}
	//插入
	for(int i=0;i<maxsize;i++)
	{
		if(space[i].cur==-1)
		{
			space[i].data=value;   
			space[i].cur=pos;        
			break;
		}
	}  
}
//值删除
void Valuedelete(StaticLinkList space,int value)
{
	//获取value在结构体数组中的位置和value所指向的下一个结点位置
	int pos1,pos2;//pos1为value在结构体数组中的位置,pos2为value所指向的下一个结点位置
	for(int i=0;i<maxsize;i++)
	{
		if(space[i].data==value&&space[i].cur!=-1)
		{
			pos1=i;
			pos2=space[i].cur;
			space[i].cur=-1;
			for(int j=0;j<maxsize;j++)
			{
				if(space[j].cur==pos1)
				{
					space[j].cur=pos2;
				}
			}
		}
	}
}
//打印
void myprint(StaticLinkList space,int start,int end)   //start为开始的位置 ,结束时cur的值
{
	//判空
	int count=0;
	for(int i=0;i<maxsize;i++)
	{
		if(space[i].cur==-1)
		{
			count++;
		}
	}
	if(count==maxsize)
	{
		printf("空,无法进行打印操作!");
		return;
	}
	//打印
	    int i=start;	
		while(1)
		{
			printf("%d",space[i].data);
		    i=space[i].cur;
			if(space[i].cur==end)
			{
				printf("%d\n",space[i].data);
				break;
			}
		}	
}

4, the main function

int main()
{
	StaticLinkList space;
	initList(space);
	//插入
	Zinsert(space,1,1);
	Zinsert(space,2,3);
	Zinsert(space,4,4);
	Zinsert(space,3,2);
	Zinsert(space,5,5);
	Zinsert(space,6,0);
	//打印
	myprint(space,0,0);
	//删除值为3的结点
	Valuedelete(space,3);
	//打印
	myprint(space,0,0);
}

5, operating results
Here Insert Picture Description

Published 13 original articles · won praise 3 · Views 626

Guess you like

Origin blog.csdn.net/weixin_43873172/article/details/104127331