C/C++,java 数据结构---线性链表存储数据:获取数据慢,增删快

首先是C语言的实现 然后是C++和JAVA
 
  

LinkList.h
#ifndef _LINK_LIST
#define _LINK_LIST
typedef void LinkList;
typedef struct _linklist {
	struct _linklist*  next;
}LinkListNode;

LinkList* create();
int insert(LinkList* list,LinkListNode* node, int pos);
LinkListNode* deleteNode(LinkList* list,int pos);
void destory(LinkList* list);
void clear(LinkList* list);
LinkList* get(LinkList* list,int pos);
int getLength(LinkList* list);


#endif

LinkList.c
#include 
#include 
#include"LinkList.h"

typedef struct _tag_list {
	LinkListNode heard;//链表的节点
	int length;//链表的长度
}LinkedList;

LinkList* create() {
	//给链表分配内存
	LinkedList *ret = (LinkedList*)malloc(sizeof(LinkedList));
	if (ret==NULL)
	{
		return NULL;
	}
	//让链表的头结点指向NULL
	ret->heard.next = NULL;
	ret->length = 0;
	return ret;
}
int insert(LinkList* list,LinkListNode* node, int pos) {
	int i = 0;
	LinkedList* listList = NULL;
	LinkListNode* current = NULL;
	if (list==NULL||node==NULL||pos<0)
	{
		return -1;
	}
	listList =(LinkedList*) list;
	current = &listList->heard;
	for (i = 0; i < pos&¤t->next!=NULL; i++)
	{
		current = current->next;
	}
	//改变链表节点指针指向
	node->next= current->next;
	current->next = node;
	listList->length++;
	return 0;
}
//删除指定位置的元素
LinkListNode* deleteNode(LinkList* list, int pos) {
	LinkedList* linkList = NULL;
	LinkListNode* current = NULL;
	LinkListNode* ret = NULL;
	linkList = (LinkedList*)list;
	int i = 0;
	if (list == NULL||pos>0||poslength) {
		return;
	}
	//让辅助指针指向头
	current = &linkList->heard;
	//遍历直到指定位置
	for (i = 0; i < pos; i++)
	{
		//改变辅助指针的指向
		current = current->next;
	}
	//
	ret = current->next;
	//改变链表节点指针指向
	 current->next= ret->next;
	linkList->length--;

	return ret;
}
void destory(LinkList* list) {
	LinkedList* linenk = NULL;
	if (list==NULL)
	{
		return;
	}
	linenk = (LinkedList*)list;
	free(linenk);
	linenk = NULL;
}
void clear(LinkList* list) {

}
LinkList* get(LinkList* list, int pos) {
	LinkedList* linkedList = NULL;
	LinkListNode* current = NULL;
	LinkListNode* ret = NULL;
	if (list==NULL)
	{
		return NULL;
	}
	linkedList = (LinkedList*)list;
	current = &linkedList->heard;
	int i = 0;
	for ( i = 0; i < pos; i++)
	{
		current = current->next;
	}
	ret = current->next;
	return ret;
}
int getLength(LinkList* list) {
	if (list==NULL)
	{
		return -1;
	}
	LinkedList* listlist = (LinkedList*)list;
	return listlist->length;
}

main.c

#include
#include "stdlib.h"
#include "string.h"
#include "LinkList.h"

typedef struct Teacher
{
	char name[64];
	int age;
}Teacher;

int main()
{
	Teacher		t1, t2, t3;
	int			length, i = 0;

	LinkList		*list = NULL;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;


	list = create();

	length = getLength(list);

	insert(list, (LinkListNode *)&t1, 0);
	insert(list, (LinkListNode *)&t2, 0);
	insert(list, (LinkListNode *)&t3,0);

	//遍历链表 
	for (i = 0; iage);
		}
	}
	deleteNode(list,0);
	printf("\n=============================\n");
	//遍历链表 
	for (i = 0; iage);
		}
	}

	destory(list);
	system("pause");
	return 0;
}


再来看C++的


linkedList2.h


#pragma once
template
 struct Node {
	Node* next;
	T data;
};


 template
class linkedList2
{
	
public:
	linkedList2();
	~linkedList2();
	bool insert(T &t);
	bool insert(T &t,int pos);
	T* get(int pos);
	int getLen();
	bool deleNode(int pos);
private:
	Node *head;
	int len;
};

linkedList2.cpp
#include "linkedList2.h"


template
linkedList2::linkedList2()
{
	head =nullptr;
	len =0;
}

template
linkedList2::~linkedList2()
{
	
}

template
T* linkedList2::get(int index)
{
	if (index < 0 || index > len)
	{
		return NULL;
	}
	Node * temp = head;
	int count = 0;
	while (count <= len)
	{
		if (count == index)
		{
			return &temp->data;
		}
		temp = temp->next;
		count++;
	}
	
	return NULL;
}
template
bool linkedList2::insert(T & t)//尾部插入
{
	Node *tmp = new Node();
	tmp->data = t;
	tmp->next = nullptr;
	if (head==nullptr)
	{
		head = tmp;
	}
	else {
		Node *curren = head;
		while (curren->next!=nullptr) {
			curren = curren->next;
		
		}
		curren->next = tmp;
	}
	len++;
	return true;
}

template
bool linkedList2::insert(T & t, int index)//在index位置插入 t数据
{
	try {
		if (index == len)
		{
			insert(t);
			return true;
		}
		else if (index<0 || index>len)
		{
			return false;
		}
		Node *temp = head;
		Node *node = new Node();
		int i = 0;
		while (temp->next!=nullptr&&inext;
			i++;
		}
		
		node->data = t;
	
		if (temp==head)
		{
			head = node;
			node->next = temp;
		}
		else {
			node->next = temp->next;
			temp->next = node;
		}
		++len;
		return true;
	}
	catch (...) {
		return false;
	}
	
}


template
int linkedList2::getLen()//获取链表长度
{
	return len;
}
template
bool linkedList2::deleNode(int pos)
{
	if (pos>len||pos<0)
	{
		return false;
	}

	Node *temp=head;
	if (pos==0)
	{
		head = temp->next;
		len--;
		return true;
	}
	int j =1;
	while (temp->next&&jnext;
		j++;
	}
	Node *tmp2 = temp->next;
		temp->next = tmp2->next;
	delete tmp2;//释放内存
	len--;

	return true;
}

最后来看看JAVA的实现:

LinkedList.java

package com.xielinhua.link;

public class LinkedList {
	private Node heard = null;// 头节点
	private int len;

	@SuppressWarnings("hiding")
	private class Node {
		public T data;// 数据
		public Node next;

	}

	/**
	 * 添加到前面
	 * 
	 * @param data
	 * @return
	 */
	public void addFirst(T data) {
		Node newNode = new Node<>();
		newNode.data = data;
		newNode.next = null;
		newNode.next = heard;
		heard = newNode;
		len++;
	}

	/**
	 * 添加到尾部
	 * 
	 * @param data
	 * @return
	 */
	public boolean add(T data) {
		Node newNode = new Node<>();
		newNode.data = data;
		newNode.next = null;
		if (heard == null)
			heard = newNode;
		else {
			Node p = heard;
			while (p.next != null) {
				p = p.next;
			}
			p.next = newNode;

		}

		len++;
		return true;
	}

	/**
	 * 在指定位置添加
	 * 
	 * @param data
	 * @param index
	 * @return
	 */
	public boolean add(T data, int index) {
		if (index >= len) {
			add(data);
			return true;
		}
		Node newNode = new Node();
		newNode.data = data;
		newNode.next = null;
		if (index == 0) {
			newNode.next = heard;
			heard = newNode;
		} else {
			Node p = heard;
			for (int i = 0; i < index; i++) {
				p = p.next;
			}
			newNode.next = p.next;
			p = newNode;
		}
		len++;
		return true;
	}

	/**
	 * 删除一个节点
	 * 
	 * @param index
	 * @return
	 */
	public boolean remove(int index) {
		Node p = heard;
		Node temp = null;
		if (index == 0) {
			heard = heard.next;
		} else {
			for (int i = 1; i < index; i++) {
				p = p.next;
			}
			temp = p.next;
			p.next = temp.next;

		}
		len--;

		return false;
	}

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

	/**
	 * 获取指定位置的节点
	 * 
	 * @param index
	 * @return
	 */
	public T getNode(int index) {
		Node temp = heard;
		for (int i = 0; i < index; i++) {
			temp = temp.next;
		}
		return temp.data;
	}

}



测试文件 main.cpp

#include
using namespace std;
#include"linkedList2.cpp"

class Teacher
{
public:
	Teacher() {};
	~Teacher() {};

public:
	int age;
};




void main() {
	Teacher t1, t2, t3;
	t1.age = 23;
	t2.age = 24;
	t3.age = 25;
	linkedList2 list;
	list.insert(t1,0);
	list.insert(t2,0);
	list.insert(t3,0);
	for (int i = 0; i < list.getLen(); i++)
	{
		Teacher* t=list.get(i);
		cout << t ->age<< endl;
	}
	
	Teacher* t = list.get(2);

	cout << t->age << "================="<< endl;
	list.deleNode(2);
	for (int i = 0; i < list.getLen(); i++)
	{
		Teacher* t = list.get(i);
		cout << t->age << endl;
	}

	cin.get();
}

测试文件

Test.java

package com.xielinhua.link;

public class Test {

	public static void main(String[] args) {
		LinkedList list = new LinkedList<>();
		list.addFirst(12);
		list.addFirst(13);
		list.addFirst(14);
		list.addFirst(15);

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

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

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

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

	}

}





你可能感兴趣的:(C/C++)