# [数据结构]链表（中）

### 双链表

#include<stdio.h>
#include<malloc.h>

typedef struct node
{
char data;
struct node *prior;
struct node *next;
}node;

node *create(int n)
{
char a='A';
node *s;
int i=1;
if(n!=0)
{
while(i<=n)
{
s=malloc(sizeof(node));
s->data=a;
p->next=s;
s->prior=p;
p=s;
a++;
i++;
}
return p->next;
}
}
node *next(node *p,int n)
{
int s;
if(n<0)
{
for(s=0;s>n;s--)
{
p=p->next;
}
return p;
}
else
{
for(s=0;s<n;s++)
{
p=p->prior;
}
return p;
}
}
int main()
{
node *p=create(26);
int sa;
int s;
printf("输入一个整数（正数向右移，负数向左移。）\n");
while(1)
{
if(scanf("%d",&s)!=1&&(char)s=='s')
break;
else
{
p=next(p,s);
for(sa=0;sa<26;sa++)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
}
}


Python版


# -*- coding: cp936 -*-
__metaclass__=type

class Node:
def __init__(self):
self.data=0
self.prior=self
self.next=self

def setdata(self,data):
self.data=data

def getdata(self):
print self.data

def nextnode(node,num):
for i in range(num-2*num):
node=node.next

for i in range(26):
print chr(node.data),
node=node.next
print ' '
return node

def priornode(node,num):
for i in range(num):
node=node.prior

for i in range(26):
print chr(node.data),
node=node.next
print ' '
return node

if __name__=='__main__':
p=Node()
for i in range(65,91):
s=Node()
s.data=i
p.next=s
s.prior=p
p=s
for i in range(26):
print ' '
t=True
while(t):
i=raw_input('请输入一个数字\n')
if i=='end':
t=False
elif int(i)>=0 :