// test13.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
#define ElemType int
typedef struct DuLNode{
ElemType data;
DuLNode* next;
DuLNode* prior;
}DuLNode, *DuLinkList;
/**
ListInsert(L, pos, d)
p=L[pos-1]
node->next=p->next
p->next=node
node->prior=p;
node->next->prior=node
*/
int ListInsert(DuLinkList& L, int pos, ElemType d){
DuLNode* p=L; //p用于遍历List
if(pos<=0) return -1; //只能在头结点(pos=0)之后插入
for(int i=0;i<pos-1;i++) //寻找插入位置的前一个结点
p=p->next;
DuLNode* node=(DuLNode*)malloc(sizeof(DuLNode));
node->data=d;
node->next=p->next;
p->next=node;
node->prior=p;
node->next->prior=node;
return 1;
}
ostream& operator<<(ostream& out, DuLinkList& L){ //重载<<便于输出DuLinkList
DuLNode* p=L->next; //如果要往前遍历,就p=L->prior
while(p && p!=L){
out<<p->data<<"<-->";
p=p->next; //如果要往前遍历,就p=p->prior
}
out<<"NULL";
return out;
}
int main(){
DuLinkList list=(DuLNode*)malloc(sizeof(DuLNode));
list->data=-1; //head结点,默认为-1
list->next=list;
list->prior=list;
for(int i=0;i<10;i++){
ListInsert(list,i+1, i*i);
}
cout<<list;
system("pause");
}