注册 登录
编程论坛 数据结构与算法

刚编的链表程序**晒晒**

诸葛修勤 发布于 2011-05-09 18:35, 750 次点击
程序代码:
/*带头结点的双向循环链表相关操作 结点采用尾部插入*/
#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;
}
6 回复
#2
yuccn2011-05-10 12:05
编程风格挺好的 呵呵
#3
lly101203032011-05-11 23:01
这个收藏了
#4
gyw520gyw2011-05-16 19:06
…………
#5
水木天2011-05-18 12:02
编程习惯很好哈
#6
寒风中的细雨2011-05-19 23:47
顶一下~~~ 学习
#7
lly101203032011-05-20 22:41
#include <stdio.h>
#include <stdlib.h>
 struct node
{
    int data;
    struct node *pre;
    struct node *next;
};
//双向链表初始化,即创建了一个表头
struct node *initlink(struct node *head)
{
    head = (struct node*) malloc (sizeof (struct node));
    if (NULL == head)
    {
        exit(1);
    }
    head->pre=head;
    head->next=head;
}
//在节点b及b->next之间添加节点a
void add(struct node *b,struct node *a)
{
    a->next=b->next;
    b->next=a;
    a->pre=b;
    b->next->pre=a;
}
//删除节点a,并返回a的前一个节点
struct node *dele(struct node *a)
{
    struct node *temp;
    temp=a->pre;
    a->pre->next=a->next;
    a->next->pre=a->pre;
    free(a);
    return temp;
}
//先序打印
void prev_print_list(struct node *head)
{
    struct node *temp;
    temp = head->next;

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

    while (temp != head)
    {
        printf("%d ", temp->data);
        temp = temp->pre;
    }
    printf("\n");
}
   
void main()
{
    printf("创建一个双向链表\n");
    struct node *head=NULL;
    struct node *temp;
    struct node *p;
    int i=1,n;
    head=initlink(head);
    printf("输入链表的个数n\n");
    scanf("d%",&n);
    printf("输入链表的数据\n");
   
    while(i<=n)
    {
        scanf("d%",&d);
        temp=(struct node*) malloc (sizeof(struct node));
         if (NULL == temp)
         {
             printf("节点分配不成功\n");
         }
         temp->data=d;
         add(head->pre,temp);
         i++;
    }
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);

    printf("\n\n删除数据为 3 的结点\n");
    temp=head->next;
    while(temp!=head)
    {
        if(temp->data==3)
        {
            p=dele(head, temp);
            temp=p;
        }
        temp=temp->next;
    }
    printf("先序打印双向循环链表:");
    prev_print_list(head);
    printf("后序打印双向循环链表:");
    post_print_list(head);
}
这个程序是看完你的后自己动手编的,不知道为什么就是有错,帮指点下,C语言不熟悉
1