Day 18 数据结构--循环链表与双向循环链表

循环链表

#include <stdio.h>

#include <stdlib.h>

#define T 1

#define F 0

 

typedef int Elementype;

typedef int Status;

 

struct node

{

    Elementype value;

    struct node* next;

};

typedef struct node* Node;

 

Status init(Node* p);

Status print(Node p);

int length(Node p);

Status insert_head(Node p,Elementype value); //

Status insert_tail(Node p,Elementype value); //

Status insert_index(Node p,int index,Elementype value); //

Status delete_index(Node p,int index); //

Status delete_value(Node p,Elementype value); //

Status update_index(Node p,int index,Elementype value); //

Status update_value(Node p,Elementype old_value,Elementype new_value); //

Status query_index(Node p,int index); //

Status query_value(Node p,Elementype value); //

 

 

int main()

{

    Node head;

    init(&head);

 

    int i;

    for(i = 0;i < 10;i++)

    {

        insert_head(head,i + 1);

    }

    print(head);

 

    for(i = 0;i < 10;i++)

    {

        insert_tail(head,i + 1);

    }

    print(head);

 

    insert_index(head,2,99);

    print(head);

 

    delete_index(head,5);

    print(head);

 

    delete_value(head,1);

    print(head);

 

    update_index(head,5,55);

    print(head);

 

    update_value(head,8,100);

    print(head);

 

    query_index(head,15);

 

    query_value(head,99);

    query_value(head,100);

 

    return 0;

}

 

Status init(Node* p)

{

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->next = newnode;//

    *p = newnode

    return T;

}

 

Status print(Node p)

{

    Node head = p;

    while(head != p->next)

    {

        printf("%d ",p->next->value);

        p = p->next;

    }

    printf("\n");

}

 

int length(Node p)

{

    Node head = p;

    int len = 0;

    while(head != p->next)

    {

        len++;

        p = p->next;

    }

    return len;

}

 

Status insert_head(Node p,Elementype value)

{

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->value = value;

    newnode->next = p->next;

    p->next = newnode;

    return T;

}

 

Status insert_tail(Node p,Elementype value)

{

    Node head = p;

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->value = value;

    while(head != p->next)

    {

        p = p->next;

    }

    newnode->next = p->next;

    p->next = newnode;

 

    return T;

}

 

Status insert_index(Node p,int index,Elementype value)

{

    if(index < 0 || index > length(p) - 1) //0 <= index <= length - 1

    {

        printf("index ie error\n");

        return F;

    }

 

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->value = value;

 

    int i;

    for(i = 0;i < index;i++)

    {

        p = p->next;

    } //pindex

 

    newnode->next = p->next;

    p->next = newnode;

    return T;

}

 

Status delete_index(Node p,int index)

{

    if(index < 0 || index > length(p) - 1)

    {

        printf("index is error\n");

        return F;

    }

    int i;

    for(i = 0;i < index;i++)

    {

        p = p->next;

    }

 

    Node temp = p->next;

    p->next = temp->next; //p->next->next;

    free(temp);

 

    return T;

}

 

Status delete_value(Node p,Elementype value)

{

    int i = 0;

    Node head = p; //

    while(head != p->next)

    {

        if(value == p->next->value)

        {

            delete_index(head,i);

        }

        else

        {

            i++;

            p = p->next;

        }

    }

    return T;

}

 

Status update_index(Node p,int index,Elementype value)

{

    if(index < 0 || index > length(p) - 1)

    {

        printf("index is error\n");

        return F;

    }

    int i;

    for(i = 0;i < index;i++)

    {

        p = p->next;

    }

    p->next->value = value;

    return T;

}

 

Status update_value(Node p,Elementype old_value,Elementype new_value)

{

    Node head = p;

    while(p->next != head)

    {

        if(old_value == p->next->value)

        {

            p->next->value = new_value;

        }

        p = p->next;

    }

}

 

Status query_index(Node p,int index)

{

    if(index < 0 || index > length(p) - 1)

    {

        printf("index is error\n");

        return F;

    }

 

    int i;

    for(i = 0;i < index;i++)

    {

        p = p->next;

    }

 

    printf("query_index(%d) : %d\n",index,p->next->value);

    return T;

}

 

Status query_value(Node p,Elementype value)

{

    printf("the index of query_value(%d) are : ",value);

    int i = 0;

    int flag = 0;

    Node head = p;

    while(p->next != head)

    {

        if(p->next->value == value)

        {

            flag++;

            printf("%d ",i);

        }

        else

        {

            i++;

            p=p->next;

 

        }

       

    }

    if(flag == 0)

    {

        printf("NOT FOUND\n");

        return F;

    }

    printf("\n");

    return T;

}

 

 

 

 

 

双向循环链表

#include <stdio.h>

#include <stdlib.h>

#define T 1

#define F 0

 

typedef int Elementype;

typedef int Status;

 

struct node//

{

    struct node* prior;

    Elementype value;

    struct node* next;

};

typedef struct node* Node;

 

Status init(Node* p);

Status insert_head(Node p,Elementype value);

void printN(Node p);

void printP(Node p);

int length(Node p);

Status insert_tail(Node p,Elementype value);

Status insert_index(Node p,int index,Elementype value);

Status delete_index(Node p,int index);

Status delte_value(Node p,Elementype value);

 

 

int main()

{

    Node head;

    init(&head);

 

    int i;

    for(i = 0;i < 10;i++)

    {

        insert_head(head,i+1);

    }

    printN(head);

    printP(head);

 

    for(i = 0;i < 10;i++)

    {

        insert_tail(head,i+1);

    }

    printN(head);

    printP(head);

 

 

 

    return 0;

}

 

Status init(Node* p)

{

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->next = newnode;

    newnode->prior = newnode;

    *p = newnode;

    return T;

}

 

Status insert_head(Node p,Elementype value)

{

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->value = value;

    newnode->next = p->next;

    p->next = newnode;

    newnode->prior = p;

    p->next->next->prior = newnode;

    return T;

}

 

void printN(Node p)

{

    Node head = p;

    while(head != p->next)

    {

        printf("%d ",p->next->value);

        p = p->next;

    }

    printf("\n");

}

 

void printP(Node p)

{

    Node head = p;

    while(head != p->prior)

    {

        printf("%d ",p->prior->value);

        p = p->prior;

    }

    printf("\n");

}

 

int length(Node p)

{

    Node head = p;

    int len = 0;

    while(head != p->next)

    {

        p = p->next;

        len++;

    }

    return len;

}

 

Status insert_tail(Node p,Elementype value)

{

    Node newnode = (Node)malloc(sizeof(struct node));

    if(NULL == newnode)

    {

        return F;

    }

    newnode->value = value;

    newnode->next = p;

    p->prior->next = newnode;

    newnode->prior = p->prior;

    p->prior = newnode;

    return T;

}

 

 

 

猜你喜欢

转载自blog.csdn.net/j_xianyu/article/details/81435209