数据结构部分

 

#include<iostream>
#include<cstdlib>
#include <stdio.h>

using namespace std;
typedef struct Node *PNode;
typedef struct Node
{
    int reg;
    int date;
    PNode previous;
    PNode next;
}Node;
Node *head;
typedef struct
{
    PNode head;
    PNode tail;
    int size;
}DList;
DList *a;
int init_list();
int insert_follow_reg(int reg,int value);
int delete_follow_reg(int reg);
int reload_follow_reg(int reg,int value);
void print_list();
int main()
{
    init_list();
    insert_follow_reg(9,0);
    //delete_follow_reg(8);
    reload_follow_reg(9,3);
    print_list();
}
int init_list()
{
    head=new(Node);
    head->next=NULL;
    head->previous=NULL;
    a=new(DList);
    a->head=head;
    a->tail=NULL;
    a->size=0;
}
int insert_follow_reg(int reg,int value)
{
    Node *p;
    p=new(Node);
    p->date=value;
    p->reg=reg;
    if(head->next==NULL)
    {
        head->next=p;
        p->next=NULL;
        p->previous=head;
        a->tail=p;
        a->size++;
    }
    else
    {
        if(p->reg<head->next->reg)
        {
            p->next=head->next;
            head->next->previous=p;
            head->next=p;
            p->previous=head;
        }
        else
        {
            int cnt=0;
            for(Node *q=head->next;q!=a->tail;q=q->next)
            {
                if(p->reg>q->reg && p->reg<q->next->reg)
                {
                        p->next=q->next;
                        q->next->previous=p;
                        q->next=p;
                        p->previous=q;
                        cnt=1;
                        a->size++;
                        break;
                }
            }
            if(cnt==0)
            {
                if(a->tail->reg < p->reg)
                {
                    p->next=a->tail->next;
                a->tail->next=p;
                p->previous=a->tail;
                a->tail=p;
                a->size++;
                }
                
            }
        }
        
    }
}
int delete_follow_reg(int reg)
{
    int cnt=0;
    for(Node *q=head->next;q!=NULL;q=q->next)
    {
        if(q->reg==reg)
        {
            cnt=1;
            q->previous->next=q->next;
            q->next->previous=q->previous;
        }
    }
    if(cnt==0) 
    {
        printf("没有此序号\n");
    }
}
int reload_follow_reg(int reg,int value)
{
    int cnt=0;
    for(Node *q=head->next;q!=NULL;q=q->next)
    {
        if(q->reg==reg)
        {
            cnt=1;
            q->date=value;
        }
    }
    if(cnt==0) cout<<"没有此序号"<<endl;
}
void print_list()
{
    for(Node *q=head->next;q!=NULL;q=q->next)
    {
        cout<<q->reg<<" "<<q->date<<endl;
        //cout<<a->tail->reg<<endl;
        
    }
    printf("a->size = %d\n",a->size);
}

 

posted @ 2016-08-16 22:19  mrsandstorm  阅读(135)  评论(0编辑  收藏  举报