不管是什么语言大致都一样的 ,先来看看用C来实现:
linkenList.h
#ifndef _LINKED_LIST #define _LINKED_LIST typedef void Tem; typedef struct No { struct Node* next; struct Node* pre; Tem* data; }Node; typedef struct _No { struct Node* head; int len; } LinkedList; LinkedList* initLinked(); int addFirst(LinkedList* list, Tem* t); int addEnd(LinkedList* list,Tem* t); int add(LinkedList* list, Tem* t,int pos); Node* getNode(LinkedList* list,int pos); int getLen(LinkedList* list); int remove(LinkedList* list,int pos); int destory(LinkedList* list); #endif
#include "linkenList.h" #include<stdlib.h> LinkedList* initLinked() { LinkedList* list = (LinkedList*)malloc(sizeof(LinkedList));//分配内存 list->len = 0; return list; } /*在头部加入元素*/ int addFirst(LinkedList* list, Tem* t) {//在头部加入 if (list == NULL || t==NULL) { return -1; } Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = t; newNode->next = NULL; if (list->len==NULL) { list->head = newNode; newNode->pre = NULL; } else { newNode->next = list-> head; Node* head=list->head; head->pre = newNode; list->head = newNode; } list->len++; Node* last = getNode(list, getLen(list) - 1); last->next = list->head;//让最后一个元素的下一个节点指向头节点形成循环 return 0; } /*在尾部加入元素*/ int addEnd(LinkedList* list,Tem* t) { if (list == NULL || t==NULL) { return -1; } Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = t; newNode->next = NULL; newNode->pre = NULL; if (list->len == 0) { list->head = newNode; } else { Node* temp = list-> head; int i =1; while (temp&&i<list->len) { temp = temp->next; i++; } newNode->pre = temp;//改变新加入的上一个指针的指向 temp->next = newNode; newNode->next = list->head;//让新加入的节点指向头结点 } list->len++; return 0; } /*在指定位置加入元素*/ int add(LinkedList* list, Tem* t, int pos) { if (list == NULL || pos<0||t==NULL) { return -1; } if (pos==0) { addFirst(list,t); } else if(pos>=list->len){ addEnd(list, t); } else { Node *newNode = (Node*)malloc(sizeof(Node)); newNode->data = t; Node *p = list->head; int i = 1; while (p&&i<pos) { p = p->next; i++; } newNode->pre = p;//改变新加入的上一个指针的指向 newNode->next = p->next; p->next=newNode; list->len++; } return 0; } /*获取指定位置元素*/ Node* getNode(LinkedList* list, int pos) { if (list == NULL || pos<0) { return -1; } Node* p = list->head; int i = 0; for (i = 0; i < pos;i++) { p = p->next; } return p; } /*获取链表长度*/ int getLen(LinkedList* list) { if (list == NULL) { return -1; } return list->len; } /*删除指定位置元素*/ int remove(LinkedList* list, int pos) { if (list==NULL||pos<0) { return -1; } Node *temp; if (pos == 0) { temp=list->head; list->head = temp->next; } else { Node *p = list->head; int i = 1; while (p&&i < pos) { p = p->next; i++; } temp = p->next; p->next = temp->next; free(temp);//释放要删除的内存 } list->len--; return 0; } /*销毁链表*/ int destory(LinkedList* list) { if (list == NULL) { return -1; } free(list); return 0; }
#include<stdlib.h> #include<stdio.h> #include"linkenList.h" void main() { LinkedList* list=initLinked(); add(list,21,0); add(list, 22,0); add(list, 23, 0); int len=getLen(list); int i = 0; for (int i = 0; i < len*4;i++) { Node* node=getNode(list,i); printf("%d\n",node->data); } remove(list, 1); printf("====================\n"); for (int i = 0; i < getLen(list); i++) { Node* node = getNode(list, i); printf("%d\n", node->data); } destory(list); system("pause"); }
再来看看用java来实现
package com.xielinhua.link; public class LinkedList<T> { private Node<T> head = null;// 头节点 private int len; @SuppressWarnings("hiding") public class Node<T> { public T data;// 数据 public Node<T> next;// 下一个节点 public Node<T> pre;// 上一个节点 } /** * 在头部加入 * * @param t * @return */ public boolean addFirst(T t) { Node<T> p = new Node<T>(); p.data = t; p.pre = null;// 由于是在头部加入所以新加入的节点的上一个节点都为NULL if (head == null) { head = p; } else { head.pre = p;// 动态将原始的头节点的上一个节点指向新的节点 p.next = head; head = p; } len++; Node<T> last = getNode(len - 1);// 获取最后一个元素的节点 然后让他的下一个节点指向头部 形成循环 last.next = head; return true; } public Node<T> getNode(int index) { Node<T> p = head; for (int i = 0; i < index; i++) { p = p.next; } return p; } public int getLen() { return len; } /** * 在尾部加入 * * @param t * @return */ public boolean add(T t) { Node<T> p = new Node<T>(); p.data = t; if (head == null) { head = p; } else { Node<T> current = head; int i = 1; while (current.next != null && i < len) { current = current.next; i++; } p.pre = current;// 动态改变将要插入的上一个节点指向 current.next = p; } p.next = head;// 让他的下一个节点指向头部 形成循环 len++; return true; } /** * 在指定位置加入 * * @param t * @param index * @return */ public boolean add(T t, int index) { if (index >= len) { add(t); } else { if (index == 0) { addFirst(t); } else { Node<T> newNode = new Node<T>(); newNode.data = t; newNode.next = null; Node<T> p = head; int i = 1; while (p.next != null && i < index) { p = p.next; i++; } newNode.pre = p;// 动态改变将要插入的上一个节点指向 newNode.next = p.next; p.next = newNode; len++; Node<T> last = getNode(len - 1); last.next = head; return true; } } return false; } /** * 删除指定位置的元素 * * @param index * @return */ public boolean remove(int index) { if (index == 0) { head = head.next; } else { Node<T> p = head; Node<T> tmp; int i = 1; while (p.next != null && i < index) { p = p.next; i++; } tmp = p.next; p.next = tmp.next; } len--; return true; } }
package com.xielinhua.link; public class Test { public static void main(String[] args) { 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() * 2; i++) { System.out.println("数据==" + list.getNode(i).data); } System.out.println("==============="); list.remove(1); for (int i = 0; i < list.getLen(); i++) { System.out.println("数据2==" + list.getNode(i).data); } } }
#pragma once template<typename T> struct Node { Node* next; T data; }; template<typename T> class linkedList2 { public: linkedList2(); ~linkedList2(); bool add(T &t); bool add(T &t,int pos); bool addFirst(T &t); Node<T>* getNode(int pos); int getLen(); bool remove(int pos); private: Node<T> *head; int len; };
#include "linkedList2.h" template<typename T> linkedList2<T>::linkedList2() { head =nullptr; len =0; } template<typename T> linkedList2<T>::~linkedList2() { } template<typename T> Node<T>* linkedList2<T>::getNode(int index)//获取指定位置的元素 { Node<T> *p = head; for (int i = 0; i < index;i++) { p = p->next; } return p; } template<typename T> bool linkedList2<T>::add(T & t)//在尾部加入 { Node<T> *newNode = new Node<T>(); newNode->data = t; newNode->next = head; if (head==nullptr) { head = newNode; } else { Node<T> *p = head; int i = 1; while (p&&i<len) { p = p->next; i++; } p->next = newNode; } len++; return true; } template<typename T> bool linkedList2<T>::addFirst(T & t)//在头部加入 { Node<T> *newNode = new Node<T>(); newNode->data = t; if (head ==nullptr) { head = newNode; } else { newNode->next = head; head = newNode; } newNode->next = head; len++; return false; } template<typename T> bool linkedList2<T>::add(T & t, int index)//在指定位置加入 { if (index>=len) { add(t); } else { if (index==0) { addFirst(t); } else { Node<T> *newNode = new Node<T>(); Node<T> *current = head; int i = 1; while (current!=nullptr&&i<index) { current = current->next; i++; } newNode->next = current->next; current->next = newNode; len++; Node<T> *last = getNode(len-1); last->next = head; return true; } } return false; } template<typename T> int linkedList2<T>::getLen()// { return len; } template<typename T> bool linkedList2<T>::remove(int pos)//删除指定位置元素 { if (pos==0) { head->next = head; } else { Node<T> *p = head; Node<T> *tem; int i = 1; while (p!=nullptr&&i<pos) { p = p->next; i++; } tem = p->next; p->next = tem->next; delete tem;//释放内存 } len--; return true; }