数据结构实验2

题目:建立一个循环单链表,其节点有 prior,data 和 next 三个域,其中 data 为数 据域,存放元素的有效信息,next 域为指针域,指向后继节点,prior 为指针域,它的 值为 NULL。编写一个算法将此表改为循环双链表。

test.h

#ifndef LIST
#define LIST
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
    elemtype data;
    struct node *prior;
    struct node *next;
}*List,node;

List Init_List()
{
//    List *p;
    List p=(List )malloc(sizeof(List));
    p->prior=NULL;
    p->next=p;
    return p;
}

void INSERTBEFORE(List p,elemtype x)
{
    List s;
    s=(List )malloc(sizeof(node));
    s->data=x;
    s->next=p->next;
    p->next=s;
}



void Print_ListR(List head)
{
    List p=head->prior;
    while(p!=head)
    {
        printf("\t%d",p->data);
        p=p->prior;
    }
}
void Print_List(List head)
{
    List p=head->next;
    while(p!=head)
    {
        printf("\t%d",p->data);
        p=p->next;
    }
}
#endif

test.c

typedef int elemtype;
#include"test.h"

void  singleToDubleCireList(List first)
{
    List p,q;
    p=first;
    q=p->next;
    while(q!=first)
    {
        q->prior=p;
        p=q;
        q=q->next;
    }
    q->prior=p;
}
int main()
{

    List L=Init_List();
    int a[10]={4,4,5,5,6,6,7,7,8,8},i;
    for(i=9;i>=0;i--)
        INSERTBEFORE(L,a[i]);
    printf("测试数据:\n");
    Print_List(L);
    printf("\n");
    printf("改变后,采用反向遍历:\n");
    singleToDubleCireList(L);
    Print_ListR(L);
    printf("\n");
}

 

posted @ 2017-06-01 20:02  五邑bb  阅读(1708)  评论(0编辑  收藏  举报