欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

double

程序员文章站 2024-03-01 10:19:58
...
#include<stdio.h>
#include<stdlib.h>


typedef struct DOUBLE_LIST
{
    int data;
    struct DOUBLE_LIST *prev;
    struct DOUBLE_LIST *next;
}double_list;

double_list *createlist()       //创建有n个元素的双向链表 并输入元素
{
    double_list *head, *p, *q;
    int n,x;
	int i;
    head = (double_list *)malloc(sizeof(double_list));
    head->prev = head;
    head->next = head;
    p = head;
    printf("Enter the number of elements to create a double linked list:\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &x);
        q = (double_list *)malloc(sizeof(double_list));
        q->data = x;
        p->next = q;
        head->prev = q;
        q->prev = p;
        q->next = head;
        p = q;
    }
    return head;
}
//遍历并且输出这些元素
void printlist(double_list *head)
{
    double_list *p;
    p = head;
    p = p->next;
    while(p!=head)
    {
        printf("%d  ", p->data);
        p = p->next;
    }
    printf("\n");
}
//得到现在双向链表中的元素的个数
int lengthlist(double_list *head)
{
    double_list *p;
    p = head;
    p = p->next;
    int coun = 0;
    while(p!=head)
    {
        coun++;
        p = p->next;
    }
    return coun;
}
//在第i个元素之前插入数据data
void insertlist_f(double_list *head, int i, int data)
{
    double_list *p = head, *q;
    p = p->next;
    i--;
    while(i--)
        p = p->next;
    q = (double_list *)malloc(sizeof(double_list));
    q->data = data;
    (p->prev)->next = q;
    q->prev = p->prev;
    q->next = p;
    p->prev = q;
}
//删除第i个位置的元素
void deletelist_i(double_list *head, int i)
{
    double_list *p = head;
    p = p->next;
    i--;
    while(i--)
        p = p->next;
    (p->prev)->next = p->next;
    (p->next)->prev = p->prev;
    free(p);
}
//删除值为x的元素
void deletelist_x(double_list *head, int x)
{
    double_list *p = head, *q;
    p = p->next;
    while(p!=head)
        if(p->data == x)
        {
            q = p->next;
            (p->prev)->next = p->next;
            (p->next)->prev = p->prev;
            free(p);
            p = q;
        }
        else
            p = p->next;
}
//对双向链表进行排序
void sortlist(double_list *head)  //升序
{
    double_list *p = head, *q, *t;
    p = p->next;
    for(;p!=head;p=p->next)
        for(t = p->next;t!=head;t=t->next)
        {
            if(p->data > t->data)
            {
                int a = p->data;
                p->data = t->data;
                t->data = a;
            }
        }
}
int main()
{
    double_list *head;
    head = createlist();
	printf("printlist=============\n");
    printlist(head);

	printf("deletelist_x===2==========\n");

	deletelist_x(head, 2);
    //sortlist(head);
	printf("printlist=============\n");
    printlist(head);
	printf("insertlist_f===2===2=======\n");
    insertlist_f(head, 2, 2);
	printf("printlist=============\n");
    printlist(head);
 
    return 0;
}


/*
[email protected]:~/lin/ms$ ./a.out                
Enter the number of elements to create a double linked list:
6
1
2
3
4
5
6
printlist=============
1  2  3  4  5  6  
deletelist_x===2==========
printlist=============
1  3  4  5  6  
insertlist_f===2===2=======
printlist=============
1  2  3  4  5  6  
[email protected]:~/lin/ms$ 

*/