C-------------------------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
int data; //数据域
struct list *next; //指针域 后继指针
struct list *pre; //指针域 前驱指针
}list;
//初始化链表
int initList(list **head);
//队尾添加一个元素
int appendElem(list **head, int e);
//打印链表
void printList(list *L);
//反向打印链表
void trav_printList(list *L);
//获得一个元素
int getElem(list *head, int i, int *e);
//查找链表中元素值为e的节点是否存在
int searchElem(list *head, int e);
//添加一个元素
int addElem(list **head, int i, int e);
//删除一个元素
int deleteElem(list **head, int i, int *e);
//返回单链表的长度
int listLength(list *head){
return head->data;
}
int main(){
list *head; //声明一个头节点
if(!initList(&head)){
printf("初始化链表失败\n");
}else{
//第一个元素下表为0
appendElem(&head, 1);
appendElem(&head, 4);
addElem(&head, 0,-2);
printf("正向打印链表\n");
printList(head);
//反向打印链表
printf("反向打印链表\n");
trav_printList(head);
int e;
deleteElem(&head, 2, &e);
printf("e:%d\n", e);
printList(head);
getElem(head, 1, &e);
printf("e:%d\n", e);
printf("下标:%d\n", searchElem(head, -2));
}
return 0;
}
int initList(list **head){
(*head) = (list *)malloc(sizeof(list)); //头结点分配储存空间
if(!(*head)) return 0;
(*head)->data = 0; //数据域
(*head)->next = NULL;
(*head)->pre = NULL;
return 1;
}
int appendElem(list **head, int e){
list *node = (list *)malloc(sizeof(list)); //生成一个新的节点
if(!node) return 0;
node->data = e;
list *p; //遍历整个链表,一直到结尾
p = (*head);
p->data += 1;
while(p->next != NULL){
p = p->next;
}
node->next = p->next;
p->next = node;
node->pre = p;
return 1;
}
void printList(list *L){
list *p = L;
while(p != NULL){
printf("%-3d", p->data);
p = p->next;
}
printf("\n");
}
//对双向链表来说十分简单,但是对单链表则十分困难
void trav_printList(list *L){
list *p = L;
while(p->next != NULL){
p = p->next;
}
while(p != L){
printf("%-3d", p->data);
p = p->pre;
}
printf("%-3d\n", p->data);
}
int getElem(list *head, int i, int *e){
list *p = head->next; //不计算头节点
//第i个元素是否存在
while(i--&&p != NULL){
p = p->next;
}
//p!=NULL存在
if(p != NULL){
*e = p->data;
return 1;
}else{ //p=NULL不存在
return 0;
}
}
int searchElem(list *head, int e){
list *p = head->next;
//查找元素
int i = 0;
while(p != NULL && p->data != e){
p = p->next;
i++;
}
//一直查找到尾也没有找到,返回-1
if(p == NULL){
return -1;
}else{ //找到返回该元素的下标
return i;
}
}
int addElem(list **head, int i, int e){
list *p = *head;
//找到第i个节点前面的节点,如果i超过节点数,就把节点放置到最后一个
while(i-- && p->next != NULL){
p = p->next;
}
list *node = (list *)malloc(sizeof(list));
if(!node) return 0;
node->data = e;
list *nowNode = p->next;
// 这种方法更好理解一点
node->next = nowNode;
p->next = node;
if(nowNode != NULL)
nowNode->pre = node;
node->pre = p;
// 这是另一种稍微复杂的一点的方法
// p->next->pre = node;
// node->pre = p;
// node->next = p->next;
// p->next = node;
(*head)->data++;
return 1;
}
int deleteElem(list **head, int i, int *e){
list *p = *head;
if(i >= p->data){
printf("索引超出范围\n");
return 0;
}
//获得被删除元素前面一个元素
while(i--){
p = p->next;
}
//该元素前面的元素指针指向该元素后面的元素
list *delnode = p->next;
list *nextnode = delnode->next;
*e = delnode->data;
// 这是一种方法
// if(delnode->next != NULL){
// delnode->next->pre = p;
// }
// p->next = delnode->next;
// 这是另一种方法
p->next = nextnode;
if(nextnode != NULL){
nextnode->pre = p;
}
free(delnode);
(*head)->data--;
return 1;
}
c++ ---------------------------------------------
#include <iostream>
#include <stdio.h>
#include <cstdlib>
using namespace std;
typedef struct Node{
int data; //数据域
struct Node *next; //指针域
struct Node *pre; //指针域
}Node;
class myList{
private:
Node *head;
public:
//初始化链表
int initList();
//队尾添加一个元素
int appendElem(int e);
//打印链表
void printList();
//反向打印链表
void trav_printList();
//获得一个元素
int getElem(int i, int *e);
//查找链表中元素值为e的节点是否存在
int searchElem(int e);
//添加一个元素
int addElem(int i, int e);
//删除一个元素
int deleteElem(int i, int *e);
//返回单链表的长度
int listLength(){
return head->data;
}
};
int myList::initList(){
head = new Node;
if(!head) return 0;
head->next = NULL;
head->pre = NULL;
head->data = 0;
return 1;
}
int myList::appendElem(int e){
Node *p = head;
while(p->next != NULL){
p = p->next;
}
Node *node = new Node;
if(!node) return 0;
node->data = e;
node->next = NULL;
p->next = node;
node->pre = p;
head->data++;
return 1;
}
void myList::printList(){
Node *p = head;
while(p != NULL){
printf("%-3d", p->data);
p = p->next;
}
}
void myList::trav_printList(){
Node *p = head;
while(p->next != NULL){
p = p->next;
}
while(p != head){
printf("%-3d", p->data);
p = p->pre;
}
printf("%-3d\n", p->data);
}
int myList::getElem(int i, int *e){
Node *p = head->next;
if(i >= head->data){ //下标在0开始
return 0;
}
while(i-- && p != NULL){
p = p->next;
}
*e = p->data;
return 1;
}
int myList::searchElem(int e){
Node *p = head->next;
int i = 0; //下标索引
while(p != NULL && p->data != e){
p = p->next;
i++;
}
if(p == NULL){
return -1;
}else{
return 0;
}
}
int myList::addElem(int i, int e){
if(i > head->data){
return 0;
}
Node *p = head;
while(i-- && p!= NULL){
p = p->next;
}
Node *node = new Node;
node->data = e;
Node *nownode = p->next;
p->next = node;
node->pre = p;
nownode->pre = node;
node->next = nownode;
head->data++;
return 1;
}
int myList::deleteElem(int i, int *e){
if(i > head->data){
return 0;
}
Node *p = head;
while(i-- && p!= NULL){
p = p->next;
}
Node *delnode = p->next;
*e = delnode->data;
Node *nextnode = delnode->next;
p->next = nextnode;
if(nextnode != NULL){
nextnode->pre = p;
}
delete delnode;
head->data--;
return 1;
}
int main(){
//下标从0开始
myList List;
List.initList();
List.appendElem(1);
List.appendElem(4);
List.addElem(0, -2);
cout<<"正向输出链表"<<endl;
List.printList();
cout<<"\n反向输出链表"<<endl;
List.trav_printList();
int e;
List.deleteElem(2, &e);
printf("e:%d\n", e);
List.printList();
List.getElem(1, &e);
printf("e:%d\n", e);
printf("下标:%d\n", List.searchElem(-2));
return 0;
}
python-------------------------------------
class Node(): def __init__(self, value, next=None, pre = None): self.value = value self.next = next self.pre = pre def initList(): head = Node(0) return head def appendElem(head:Node, e:int): p = head while p.next != None: p = p.next node = Node(e) p.next = node node.pre = p head.value += 1 def printList(head:Node): p = head while p != None: print("%d "%p.value, end="") p = p.next def tra_printList(head:Node): p = head while p.next != None: p = p.next while p != head: print("%d "%p.value, end="") p = p.pre print("%d"%p.value) def getElem(head:Node, i:int): p = head.next while i != 0: i -= 1 p = p.next if p != None: return p.value else: return "error" def searchElem(head:Node, e:int): p = head.next i = 0 while p != None: if p.value == e: return i i += 1 return -1 def addElem(head:Node, i:int, e:int): if i > head.value: return "error" p = head while i !=0: p = p.next i -= 1 node = Node(e) nownode = p.next p.next = node node.pre = p node.next = nownode nownode.pre = node head.value += 1 def deleteElem(head:Node, i:int): if i > head.value: return "error" p = head while i !=0: p = p.next i -= 1 delnode = p.next e = delnode.value nextnode = delnode.next p.next = nextnode if nextnode != None: nextnode.pre = p head.value-=1 return e if __name__ == '__main__': # 不算头节点的第一个节点下标为1 head = initList() appendElem(head, 1); appendElem(head,4); addElem(head, 0,-2) print("正向输出链表") printList(head) print("反向输出链表") tra_printList(head) e = deleteElem(head, 2) print("e的值:%d"%e) printList(head) e = getElem(head, 1) print("下标为1的值:%d" % e) print("下标:%d\n"%searchElem(head, -2))