码迷,mamicode.com
首页 > 编程语言 > 详细

数据结构之---c语言实现双向链表操作

时间:2015-05-16 11:58:54      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>   
#include<stdlib.h>   
typedef int ElemType;
typedef struct DoubleLinkedList  
{  
     ElemType data;  
     struct DoubleLinkedList *pre;  
     struct DoubleLinkedList *next;  
}DlinkedList_Node;  
   
//建立链表   
DlinkedList_Node* create_dlink()  
{  
   DlinkedList_Node *head,*p,*s;  
   int x;  
   head = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));  
   p = head;  
   while(1)  
   {  
       printf("请输入要保存的节点, 以0结束输入:\n");  
       scanf("%d",&x);
       if(x != 0)  
       {  
          s = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));  
          s ->data = x;  
          s-> pre = p;  
          p->next = s;  
          p=s;  
       }  
       else  
       {  
          printf("\n数据输入结束\n");  
          break;  
       }  
    }  
    p->next = NULL;  
    head = head ->next;  
    head->pre = NULL;  
    return head;  
}  
    
//顺序、反序打印链表   
void print_dlink(DlinkedList_Node *head)  
{ 
	DlinkedList_Node *p,*s;  
    p = head;  
    printf("正序输出双向链表中的节点:\n");  
    while(p)  
    {  
        printf("%d ",p->data);  
        s = p;  
        p = p->next;  
    }  
    printf("\n 逆序输出双向链表中的节点: \n");  
    while(s)  
    {  
        printf("%d ",s->data);  
        s = s->pre;  
    }  
    printf("\n \n");  
}  
    
//删除所指定的元素的节点   
DlinkedList_Node* delete_dlinkedlist_node(DlinkedList_Node *head,int i)  
{  
    DlinkedList_Node *p;  
    p = head;  
    if(p->data == i)  
    {  
        head = p->next;  
        head->pre = NULL;  
        free(p);  
        return head;  
    }  
    while(p)  
    {  
        if(p->data == i)  
        {  
         	p->pre->next = p->next;  
            p->next->pre = p->pre;  
            free(p);  
            return head;  
         }  
         p = p->next;  
     }  
     printf("抱歉输入数据有误!,没有找到想要删除的数据\n");  
     return head;  
 }

//插入一个结点   
DlinkedList_Node* insert_dlinkedlist_node(DlinkedList_Node *head,int i)  
{  
   
   DlinkedList_Node *p,*temp;  
   p = head;  
   temp = (DlinkedList_Node*)malloc(sizeof(DlinkedList_Node));  
   temp ->data = i;  
   if(i < p->data)//比头结点数据小,插入到链表头部   
   {  
       head = temp;  
       head->next = p;//此处p为原来的head   
       head->pre = NULL;  
       p->pre = head;//此处p为原来的head   
       return head;  
    }

    while(p != NULL && i > p->data)//寻找合适的插入位置   
    {  
       p = p->next;  
    }  
    if(i < p->data)//在链表中间某处找到合适插入位置   
    {  
        temp ->next = p;  
        temp ->pre = p->pre;  
        p ->pre->next = temp;  
        p ->pre = temp;  
        return head;  
     }      
	else//没有找到合适的位置,只有将数据插入到链表尾部   
     {  
         p->next = temp;  //遍历到链表尾部,p==NULL   
         temp ->pre = p;  
         temp ->next = NULL;  
         return head;  
      }  
}  
    
int main()  
{  
    DlinkedList_Node *head;  
    head = create_dlink();  
    print_dlink(head);  
    return 0;
}  



数据结构之---c语言实现双向链表操作

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/45766767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!