zoukankan      html  css  js  c++  java
  • 数据结构实验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("	%d",p->data);
            p=p->prior;
        }
    }
    void Print_List(List head)
    {
        List p=head->next;
        while(p!=head)
        {
            printf("	%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("测试数据:
    ");
        Print_List(L);
        printf("
    ");
        printf("改变后,采用反向遍历:
    ");
        singleToDubleCireList(L);
        Print_ListR(L);
        printf("
    ");
    }
  • 相关阅读:
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K近邻算法及应用
    实验一 感知器及其应用
    实验三 面向对象分析与设计
    实验二 结构化分析与设计
    实验一 软件的开发文档与工具的安装与使用
    ATM管理系统
    举例分析流程图与活动图的区别与联系
    四则运算
  • 原文地址:https://www.cnblogs.com/wuyibb/p/6930554.html
Copyright © 2011-2022 走看看