双向循环链表的练习

//(1)编写算法,创建一个带头结点的双向循环链表,其结点结构如下:
//(2)编写算法,删除链表上的最后一个结点。
//(3)编写算法,遍历链表,输出所有结点的信息。
/*#include<stdio.h>
#include<stdlib.h>
typedef int elemtype;
typedef struct node
{
    struct node *prior;
    elemtype data;
    struct node *next;
}NODE;
NODE *save()      //创建头结点
{
    NODE *newhead;
    newhead=(NODE *)malloc(sizeof(NODE));
    newhead->data=0;
    newhead->prior=newhead->next=NULL;
    return newhead;
}

void creat(NODE *head)
{
    int n;
    NODE *new_one;
    printf("请创建链表:\n");
    printf("请输入节点的个数:");
    scanf("%d",&n);
    fflush(stdin);
    printf("请输入各个接点的值:");
    for(int i=1;i<=n;i++)        //把值传入链表中
    {
        if(i=1)       //第一个结点的前驱后继都是头结点
        {
            new_one=(NODE *)malloc(sizeof(NODE));
            scanf("%d",&new_one->data);
            new_one->next=head;
            head->next=new_one;
            new_one->prior=head;
            head->prior=new_one;
            continue;         //创建第一个后回去
        }
        new_one=(NODE *)malloc(sizeof(NODE));           //第二个及大于第二个的时候做下面的,连串起来
        scanf("%d",&new_one->data);
        head->next->prior=new_one;
        new_one->next=head->next;
        head->next=new_one;
        new_one->prior=head;
    }
}

void prn(NODE *head)        //遍历链表
{
    NODE *View;
    printf("反序输出\n");         //使用了头插法所以反序输出
    for(View=head->next;View!=head;View=View->next)
        printf("%d\t",View->data);
}

void DEL_last(NODE *head)       //删除最后一个结点
{
    NODE *_Del;
    _Del=head->prior;
    head->prior=_Del->prior;
    _Del->prior->next=head;
    free(_Del);
    printf("删除完毕!\n");
}

void main()
{
    NODE *head;
    head=save();
    creat(head);
    prn(head);
    DEL_last(head);
    printf("删除之后的链表为:\n");
    prn(head);
}*/

 

posted on 2013-04-23 21:53  绕指柔yy  阅读(141)  评论(0编辑  收藏  举报

导航