<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="cpp" style="text-align: left;"><span style="font-size:18px;">首先是C语言的实现 然后是C++和JAVA</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">LinkList.h</span>
</pre><pre code_snippet_id="1689626" snippet_file_name="blog_20160519_2_6182414" name="code" class="cpp">#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
<strong><span style="font-size:18px;">LinkList.c</span></strong>
#include <stdlib.h>
#include <stdio.h>
#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||pos<linkList->length) {
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<stdio.h>
#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; i<getLength(list); i++)
{
Teacher *tmp = (Teacher *)get(list, i);
if (tmp != NULL)
{
printf("age:%d ", tmp->age);
}
}
deleteNode(list,0);
printf("\n=============================\n");
//遍历链表
for (i = 0; i<getLength(list); i++)
{
Teacher *tmp = (Teacher *)get(list, i);
if (tmp != NULL)
{
printf("age:%d ", tmp->age);
}
}
destory(list);
system("pause");
return 0;
}
再来看C++的
linkedList2.h
#pragma once
template<typename T>
struct Node {
Node* next;
T data;
};
template<typename T>
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<T> *head;
int len;
};
linkedList2.cpp
#include "linkedList2.h"
template<typename T>
linkedList2<T>::linkedList2()
{
head =nullptr;
len =0;
}
template<typename T>
linkedList2<T>::~linkedList2()
{
}
template<typename T>
T* linkedList2<T>::get(int index)
{
if (index < 0 || index > len)
{
return NULL;
}
Node<T> * temp = head;
int count = 0;
while (count <= len)
{
if (count == index)
{
return &temp->data;
}
temp = temp->next;
count++;
}
return NULL;
}
template<typename T>
bool linkedList2<T>::insert(T & t)//尾部插入
{
Node<T> *tmp = new Node<T>();
tmp->data = t;
tmp->next = nullptr;
if (head==nullptr)
{
head = tmp;
}
else {
Node<T> *curren = head;
while (curren->next!=nullptr) {
curren = curren->next;
}
curren->next = tmp;
}
len++;
return true;
}
template<typename T>
bool linkedList2<T>::insert(T & t, int index)//在index位置插入 t数据
{
try {
if (index == len)
{
insert(t);
return true;
}
else if (index<0 || index>len)
{
return false;
}
Node<T> *temp = head;
Node<T> *node = new Node<T>();
int i = 0;
while (temp->next!=nullptr&&i<index)
{
temp = temp->next;
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<typename T>
int linkedList2<T>::getLen()//获取链表长度
{
return len;
}
template<typename T>
bool linkedList2<T>::deleNode(int pos)
{
if (pos>len||pos<0)
{
return false;
}
Node<T> *temp=head;
if (pos==0)
{
head = temp->next;
len--;
return true;
}
int j =1;
while (temp->next&&j<pos)
{
temp = temp->next;
j++;
}
Node<T> *tmp2 = temp->next;
temp->next = tmp2->next;
delete tmp2;//释放内存
len--;
return true;
}
最后来看看JAVA的实现:
LinkedList.java
package com.xielinhua.link;
public class LinkedList<T> {
private Node<T> heard = null;// 头节点
private int len;
@SuppressWarnings("hiding")
private class Node<T> {
public T data;// 数据
public Node<T> next;
}
/**
* 添加到前面
*
* @param data
* @return
*/
public void addFirst(T data) {
Node<T> newNode = new Node<>();
newNode.data = data;
newNode.next = null;
newNode.next = heard;
heard = newNode;
len++;
}
/**
* 添加到尾部
*
* @param data
* @return
*/
public boolean add(T data) {
Node<T> newNode = new Node<>();
newNode.data = data;
newNode.next = null;
if (heard == null)
heard = newNode;
else {
Node<T> 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<T> newNode = new Node<T>();
newNode.data = data;
newNode.next = null;
if (index == 0) {
newNode.next = heard;
heard = newNode;
} else {
Node<T> 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<T> p = heard;
Node<T> 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<T> temp = heard;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp.data;
}
}
测试文件 main.cpp
#include<iostream>
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<Teacher> 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<Integer> 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));
}
}
}