数据结构学习~05.双链表的操作,手写LinkedList集合
本文是上一篇文章的后续,详情点击该链接~
先写一个普通的双链表插入和遍历~
#include
#include
typedef struct no_de {
int data;
struct no_de* pre;
struct no_de* next;
}Node;
Node* getHead() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = NULL;
head->next = NULL;
head->pre = NULL;
return head;
}
void Insert(Node *L,int data) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = data;
p->next = L->next;
p->pre = L;
L->next = p;
}
void Print(Node *L) {
Node* p = L->next;
if (p == NULL) {
printf("[]");
}
while (p != NULL) {
if (p ->next != NULL) {
printf("%d->",p->data);
}
else {
printf("%d",p->data);
}
p = p->next;
}
}
int main(int argc,char * argv[]) {
Node* list = getHead();
Insert(list,1); Insert(list, 2); Insert(list, 3); Insert(list, 4);
Print(list);
getchar();
return 0;
}
删除结点的算法
q = p->next;
p->next = q->next;
q->next->pre = p;
free(q);
查找结点的算法
Node* findNode(Node *C, int x) {
Node* p = C->next;
while (p != NULL) {
if (p ->data == x) {
break;
}
p = p->next;
}
return p;
}
奉上完整代码
#include
#include
typedef struct no_de {
int data;
struct no_de* pre;
struct no_de* next;
}Node;
Node* getHead() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = NULL;
head->next = NULL;
head->pre = NULL;
return head;
}
void Insert(Node *L,int data) {
Node* p = (Node*)malloc(sizeof(Node));
p->data = data;
p->next = L->next;
p->pre = L;
L->next = p;
}
int Delete(Node* C, int s) {
Node* p, * q;
p = C;
while (p->next != NULL) {
if (p->next->data == s) {
break;
}
p = p->next;
}
if (p->next == NULL) {
return 0;
}
else {
q = p->next;
p->next = q->next;
q->next->pre = p;
free(q);
return 1;
}
}
void Print(Node *L) {
Node* p = L->next;
if (p == NULL) {
printf("[]");
}
while (p != NULL) {
if (p ->next != NULL) {
printf("%d->",p->data);
}
else {
printf("%d",p->data);
}
p = p->next;
}
printf("\n");
}
Node* findNode(Node *C, int x) {
Node* p = C->next;
while (p != NULL) {
if (p ->data == x) {
break;
}
p = p->next;
}
return p;
}
int main(int argc,char * argv[]) {
Node* list = getHead();
Insert(list,1); Insert(list, 2); Insert(list, 3); Insert(list, 4);
Delete(list,2);
Print(list);
Node* p = findNode(list,3);
printf("%d",p->data);
getchar();
return 0;
}
采用尾插法建立单链表?
void InsertLast(Node*& L, int a[], int n) {
Node* s, * r;
int i;
L = (Node*)malloc(sizeof(Node));
L->pre = NULL;
L->next = NULL;
r = L;
for (i = 0; i < n; ++i) {
s = (Node*)malloc(sizeof(Node));
s->data = a[i];
r->next = s;
s->pre = r;
r = s;
}
r->next = NULL;
}
Java中的LinkedList
定义List接口
public interface List <E>{
int size();
boolean isEmpty();
boolean contains(E element);
void add(E element);
E get(int index);
E set(int index,E element);
void add(int index,E element);
E remove(int index);
int indexOf(E element);
void clear();
String toString();
}
接口实现
public class LinkedList<E> implements List<E>{
private int size;
private Node first;
private Node last;
@Override
public int size() {
return size;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(E element) {
return indexOf(element) > -1;
}
@Override
public void add(E element) {
add(size,element);
}
private Node<E> node(int index){
Node N = first;
if(index > (size >> 1)){
N = last;
for(int i = size-1; i > index; i--){
N = N.pre;
}
}else{
for(int i = 0; i < index; i++){
N = N.next;
}
}
return N;
}
@Override
public E get(int index) {
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("index: " + index + " size: " + size);
}
return node(index).element;
}
@Override
public E set(int index, E element) {
Node<E> node = node(index);
E oldElement = node.element;
node.element = element;
return oldElement;
}
@Override
public void add(int index, E element) {
if(index == size) {
Node l = last;
Node newNode = new Node(l,null,element);
last = newNode;
if(l == null){
first = newNode;
}else {
l.next = newNode;
}
}else{
Node<E> node = node(index);
Node<E> pre = node.pre;
Node<E> newNode = new Node(pre, node, element);
pre.next = newNode;
if (pre == null) {
first = newNode;
} else {
node.pre = newNode;
}
}
size++;
}
@Override
public E remove(int index) {
if(index < 0 || index >= size){
throw new IndexOutOfBoundsException("index: " + index + " size: " + size);
}
Node<E>node = node(index);
Node<E> pre = node.pre;
Node<E> next = node.next;
if(pre == null){
first = next;
next.pre = null;
}else{
pre.next = next;
}
if(next == null){
last = pre;
}else{
next.pre = pre;
}
size--;
return node.element;
}
@Override
public int indexOf(E element) {
Node N = first;
int index = 0;
for(Node i = N; i != null; i = i.next){
if(element == i.element){
return index;
}
index++;
}
return -1;
}
@Override
public void clear() {
size = 0;
first = null;
last = null;
}
public String toString(){
Node N = first;
StringBuilder stringBuilder = new StringBuilder("[");
boolean flag = false;
for(Node i = N; i != null; i = i.next){
if(i.next == null) {
flag = true;
}
if(flag == false){
stringBuilder.append(i.element + ",");
}else{
stringBuilder.append(i.element);
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
private static class Node<E>{
Node<E> pre;
Node<E> next;
E element;
public Node(Node next,E element){
this.next = next;
this.element = element;
}
public Node(Node pre,Node next,E element){
this.pre = pre;
this.next = next;
this.element = element;
}
public Node() {
}
}
}