| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 750 人关注过本帖, 2 人收藏
标题:刚编的链表程序**晒晒**
取消只看楼主 加入收藏
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
结帖率:96.55%
收藏(2)
已结贴  问题点数:50 回复次数:0 
刚编的链表程序**晒晒**
程序代码:
/*带头结点的双向循环链表相关操作 结点采用尾部插入*/
#include <stdio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;//后继
    struct node *prev;//前驱
};

struct node *temp;

//初始化链表
struct node * init_list(struct node *head)
{
    head = (struct node*) malloc (sizeof (struct node));

    if (NULL == head)
    {
        exit(-1);
    }
   
    head->next = head;
    head->prev = head;
   
    return head;
}

//添加结点
void add(struct node *next, struct node *prev, struct node *position)
{
    position->next = next;
    prev->next = position;
    position->prev = prev;
    next->prev = position;
}

//向链表中添加结点
void add_node(struct node *head, struct node *position)
{
    add(head->prev->next, head->prev, position);
}

//删除结点 返回被删的上一个结点
struct node * delet(struct node *next, struct node *prev)
{
    prev->next = next;
    next->prev = prev;

    return prev;
}

//从链表中删除某结点 与data数值相同的
void delet_node(struct node *head, int data)
{
    struct node *p;
    temp = head->next;

    while (temp != head)
    {
        if (temp->data == data)
        {
            p = delet(temp->next, temp->prev);
            free(temp);
            temp = p;
        }
       
        temp = temp->next;
    }
}

//修改链表中的结点 与data数值相同的
void chg_node(struct node *head, int data, int newValue)
{
    temp = head->next;

    while (temp != head)
    {
        if (temp->data == data)
        {
            temp->data = newValue;
        }
        temp = temp->next;
    }
}

//删除整个链表
void delet_list(struct node *head)
{
    temp = head->next;
   
    while (temp != head)
    {
        delet (temp->next, temp->prev);
        free(temp);
        temp = head->next;
    }

    free(head);
    head = NULL;
    printf("删除成功!\n");
}
//先序打印
void prev_print_list(struct node *head)
{
    temp = head->next;

    while (temp != head)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
//后序打印
void post_print_list(struct node *head)
{
    temp = head->prev;

    while (temp != head)
    {
        printf("%d ", temp->data);
        temp = temp->prev;
    }
    printf("\n");
}

void function()
{
    struct node *head = NULL;
    struct node *temp = NULL;
    int index = 10;

    printf("初始化链表\n");
    head = init_list(head);//初始化
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);

    printf("\n\n项链表中连续插入 %d 个元素\n", index);
    while (index)
    {
        temp = (struct node*) malloc (sizeof(struct node));
        temp->data = index--;
        add_node(head, temp);//向链表中添加结点
    }
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);

    printf("\n\n删除数据为 3 的结点\n");
    delet_node(head, 3);
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);
   
    printf("\n\n修改数据为 4 的结点 新值为 18\n");
    chg_node(head, 4, 18);
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);
   
    printf("\n\n删除整个链表\n");
    delet_list(head);
}

int main(void)
{
    function();

    return 0;
}
收到的鲜花
搜索更多相关主题的帖子: 链表程序 
2011-05-09 18:35
快速回复:刚编的链表程序**晒晒**
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044598 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved