注:有头结点
#!/usr/bin/python3.5
# _*_coding:utf-8_*_
class Node:
def __init__(self, value):
self.data = value
self.next = None
self.prev = None
class DoubleLinkList:
def __init__(self):
self._head = Node(None)
self._head.next = None
def insert(self, index, value):
if index <= 0:
print('插入位置有误')
return
n = Node(value)
cur = self._head
for i in range(index - 1):
cur = cur.next
if cur == None:
print('插入位置有误')
return
n.next = cur.next
if cur.next != None:
cur.next.prev = n
cur.next = n
n.prev = cur
def remove(self, index):
if self.empty():
print('链表是空的')
cur = self._head
for i in range(index - 1):
cur = cur.next
if cur == None:
print('删除位置有误')
return
temp = cur.next
if cur.next.next != None:
cur.next.next.prev = cur
cur.next = cur.next.next
del temp
def empty(self):
return self._head.next == None
def length(self):
cur = self._head.next
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def appendleft(self, value):
n = Node(value)
if self._head.next != None:
self._head.next.prev = n
n.next = self._head.next
self._head.next = n
n.prev = self._head
def appendright(self, value):
cur = self._head
while cur.next != None:
cur = cur.next
n = Node(value)
n.next = cur.next
cur.next = n
n.prev = cur
def clear(self):
cur = self._head.next
while cur != None:
temp = cur
cur = cur.next
del temp
self._head.next = None
def travel(self):
if self._head.next == None:
print('链表为空')
return
cur = self._head.next
print('正向输出:')
while cur.next != None:
print(cur.data)
cur = cur.next
print(cur.data)
print('逆向输出:')
while cur != self._head:
print(cur.data)
cur = cur.prev
def search(self, value):
cur = self._head.next
index = 1
while cur != None and cur.data != value:
cur = cur.next
index += 1
if cur == None:
print('没有该元素')
return
print('index:%d,value:%d' % (index, value))
if __name__ == '__main__':
link = DoubleLinkList()
C语言:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
struct node* prev;
} Node;
Node* init (Node* head) {
head = (Node*)malloc(sizeof(Node));
head -> next = NULL;
head -> prev = NULL;
return head;
}
void insert_head (Node* head, int value) {
Node* p;
p = (Node*)malloc(sizeof(Node));
p -> data = value;
p -> next = head -> next;
if (head -> next != NULL) {
head -> next -> prev = p;
}
p -> prev = head;
head -> next = p;
}
void insert_tail (Node* head, int value) {//尾插
Node* p;
p = (Node*)malloc(sizeof(Node));
p -> data = value;
p -> next = NULL;
Node* temp = head;
while (temp -> next != NULL) {
temp = temp -> next;
}
temp -> next = p;
p -> prev = temp;
}
int length (Node* head) {//获取长度
Node* temp = head -> next;
int sum = 0;
while (temp != NULL) {
sum++;
temp = temp -> next;
}
return sum;
}
bool insert(Node* head, int index, int value) {//插入
if (index < 1 || index > length(head) + 1) {
printf("index error!");
return false;
}
else {
Node* p;
p = (Node*)malloc(sizeof(Node));
p -> data = value;
Node* temp = head;;
for (int i=1; i<index; i++) {
temp = temp -> next;
}
p -> next = temp -> next;
if (temp -> next != NULL) {
temp -> next -> prev = p;
}
temp -> next = p;
p -> prev = temp;
return true;
}
}
bool delete_node (Node* head, int index) {//删除节点
if (index < 1 || index > length(head)) {
printf("index error!");
return false;
}
else {
Node* temp = head;
Node* del;
for (int i=1; i<index; i++) {
temp = temp -> next;
}
del = temp -> next;
temp -> next = temp -> next -> next;
if (temp -> next != NULL) {
temp -> next -> prev = temp;
}
free(del);
return true;
}
}
int locate (Node* head, int value) {//元素定位
Node* temp = head -> next;
int site = 1;
while (temp != NULL) {
if (temp -> data == value) {
return site;
}
else {
temp = temp -> next;
site++;
}
}
return -1;
}
bool isEmpty (Node* head) {//判空
if (head -> next == NULL) {
return true;
}
else {
return false;
}
}
bool getElem (Node* head, int index) {//获取元素
if (isEmpty(head)) {
printf("NULL");
return false;
}
if (index < 1 || index > length(head)) {
printf("index error!");
return false;
}
else {
Node* temp = head -> next;
for (int i=1; i<index; i++) {
temp = temp -> next;
}
printf("%d\n", temp -> data);
return true;
}
}
void clear (Node* head) {//清空链表
Node* temp = head -> next;
Node* prev;
while (temp != NULL) {
prev = temp;
temp = temp -> next;
free(prev);
}
head -> next = NULL;
printf("cleared!");
}
void print (Node* head) {
Node* temp = head -> next;
while (temp != NULL) {
printf("%d ", temp -> data);
temp = temp -> next;
}
}
int main (int argc, char **argv) {
Node* head;
head = init(head);
insert_head(head, 3); insert_head(head, 2); insert_head(head, 1);
print(head); printf("\n");
insert_tail(head, 4); insert_tail(head, 5); insert_tail(head, 6);
print(head); printf("\n");
insert(head, 7, 0);
print(head); printf("\n");
delete_node(head, 7); delete_node(head, 1); delete_node(head, 3); delete_node(head, 5);
print(head); printf("\n");
printf("%d\n", locate(head, 2)); printf("%d\n", locate(head, 5)); printf("%d\n", locate(head, 0));
getElem(head, 3);
printf("%d\n", isEmpty(head));
clear(head);
printf("%d", isEmpty(head));
return 0;
}