//Author: Guo R.H
//Date:06.12.23
//USTC
#include
#include
#define LEN sizeof(DList)
typedef struct DList
{
int data;
struct DList *prior, *next;
}DList;
void InitList(DList **head)
{
*head = (DList *)malloc(LEN);
if(*head)
(*head)->next = (*head)->prior = *head;
else
exit(0);
}
int ListLength(DList *head)
{
if(!head)
{
printf("empty list!");
return 0;
}
DList *p = head->next;
int i=0;
while(p != head)
{
i++;
p = p->next;
}
return i;
}
DList *GetP(DList *head, int i)
{
if(i<0 || i>ListLength(head))
return NULL;
DList *p=head;
int j;
for(j=0; j p = p->next;
return p;
}
void Insert(DList *head, int i, int t)
{
if(i<1 || i>ListLength(head)+1)
{
printf("Insert error\n");
exit(0);
}
int j=0;
DList *p, *s = GetP(head, i-1);
if(!s)
{
printf("error\n");
exit(0);
}
p=(DList *)malloc(LEN);
if(!p)
{
printf("error\n");
exit(0);
}
p->data = t;
p->prior = s;
p->next = s->next;
s->next->prior = p;
s->next = p;
}
void print1(DList *head)
{
DList *p = head->next;
while(p != head)
{
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
void print2(DList *head)
{
DList *p = head->prior;
while(p != head)
{
printf("%d ",p->data);
p = p->prior;
}
printf("\n");
}
int main()
{
DList *head;
int i;
InitList(&head);
for(i=0; i<5; i++)
Insert(head, i+1, i+1);
print1(head);
print2(head);
return 0;
}
阅读(1269) | 评论(0) | 转发(0) |