JAVA/C 线性顺序存储结构

首先是JAVA代码:

package com.xielinhua.arraylist;

public class MyArrayList<T> {

	private Object[] data;
	private int cap = 10;// 空间大小
	private int newCap = 10;
	private int len = 0;

	public MyArrayList() {
		data = new Object[cap];// 初始化一个通用数组
	}

	/**
	 * 在头部加入元素
	 * 
	 * @param t
	 * @return
	 */
	public boolean addFir(T t) {
		addCap();
		for (int i = len; i > 0; i--) {
			data[i] = data[i - 1];
		}
		data[0] = t;
		len++;
		return true;
	}

	/**
	 * 判断是否要增加空间
	 */
	private void addCap() {
		if (len == cap) {
			Object[] newData = new Object[cap + newCap];
			for (int i = 0; i < len; i++) {
				newData[i] = data[i];
			}
			data = newData;
		}
	}

	/**
	 * 在尾部加入元素
	 * 
	 * @param t
	 * @return
	 */
	public boolean addEnd(T t) {
		addCap();
		data[len] = t;
		len++;
		return true;

	}

	/**
	 * 在指定位置加入元素
	 * 
	 * @param t
	 * @param pos
	 * @return
	 */
	public boolean add(T t, int pos) {
		addCap();
		if (pos >= len) {

			return addEnd(t);
		}
		int i = 0;
		for (i = len; i > pos; i--) {
			data[i] = data[i - 1];
		}
		data[i] = t;
		len++;

		return false;
	}

	/**
	 * 删除指定位置的元素
	 * 
	 * @param index
	 * @return
	 */
	public boolean remove(int index) {
		for (int i = index + 1; i < len; i++) {
			data[i - 1] = data[i];
		}

		len--;
		return true;
	}

	/**
	 * 获取表的长度
	 * 
	 * @return
	 */
	public int getLen() {
		return len;
	}

	/**
	 * 获取指定位置的元素
	 * 
	 * @param pos
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T getNode(int pos) {
		return (T) data[pos];

	}
}


来测试看看:

package com.xielinhua.link;

import com.xielinhua.arraylist.MyArrayList;

public class Test {

	public static void main(String[] args) {
		MyArrayList<Integer> list = new MyArrayList<>();
		// list.add(10, 0);
		// list.add(11, 3);
		// list.add(12, 2);
		// list.add(13, 0);
		// list.add(23, 2);

		list.addFir(1);
		list.addFir(2);
		list.addFir(3);
		list.addFir(4);
		list.addFir(5);

		// list.addEnd(21);
		// list.addEnd(22);
		// list.addEnd(23);
		// list.addEnd(24);
		// LinkedList<Integer> list = new LinkedList<>();
		// list.add(12, 0);
		// list.add(13, 0);
		// list.add(14, 0);
		// list.add(15, 0);

		for (int i = 0; i < list.getLen(); i++) {

			System.out.println("数据==" + list.getNode(i));
		}
		System.out.println("===============");
		list.remove(0);
		for (int i = 0; i < list.getLen(); i++) {

			System.out.println("数据2==" + list.getNode(i));
		}

	}

}

再来看看C代码:

ArrayList2.h

#ifndef _ARRAY_LIST_
#define _ARRAY_LIST_
typedef void Node;
typedef void ArrayList;
typedef struct List_ {
	int len;
	int cap;
	unsigned int *List;

}List;
List* create();
int add(List* list,Node* node, int pos);
Node* getNode(List* list,int pos);
int desory(List* list);
int remove(List* list,int pos);
#endif

ArrayList2.c

#include"ArrayList2.h"
#include<stdlib.h>
/*创建一个线性表*/
List* create() {
	
	List* list = (List*)malloc(sizeof(List));//分配内存
	list->cap = 10;
	list->List = (unsigned int*)malloc(sizeof(unsigned int)*list->cap);//分配数组内存
	list->len = 0;
	return list;
}
/*在指定位置加入元素*/
int add(List* list, Node* node,int pos) {
	unsigned int* arrayList=list->List;
	if (list->len == 0) {
		arrayList[0] =(unsigned int) node;
		list->len++;
		return 0;
	}
	int i = 0;
	for (i = list->len; i > pos; i--) {
		list->List[i] = list->List[i-1];
	}
	list->List[i] = (unsigned int)node;
	list->len++;
	return 0;
}
/*获取指定位置的元素*/
Node* getNode(List* list, int pos) {
	return (unsigned int*)list->List[pos];
}

/*释放内存*/
int desory(List* list) {
	
	if (list==NULL)
	{
		return -1;
	}
	free(list);
	free(list->List);
	return 0;
}
/*删除指定位置的元素*/
int remove(List* list, int pos) {
	int i = 0;
	for (i = pos - 1; i < list->len;i++) {
	
		list->List[i] = list->List[i+1];
	}
	
	list->len--;
	return 0;

}


测试文件 main.c


#include<stdlib.h>
#include<stdio.h>
#include"ArrayList2.h"
void main() {
	List* list=create();
	int age = 5;
	int age1 = 4;
	int age2 = 3;
	add(list,&age,0);
	add(list, &age1, 0);
	add(list, &age2, 1);
	int i = 0;
	for (i = 0; i < list->len; i++) {
		int* node=(int*)getNode(list, i);
		printf("%d\n",*node);
	
	}
	remove(list,0);
	printf("======================\n");
	for (i = 0; i < list->len; i++) {
		int* node = (int*)getNode(list, i);
		printf("%d\n", *node);

	}
	desory(list);

	system("pause");
}


你可能感兴趣的:(JAVA/C 线性顺序存储结构)