| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:欢迎各种大侠进
只看楼主 加入收藏
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
结帖率:97.44%
收藏
已结贴  问题点数:20 回复次数:8 
欢迎各种大侠进
程序代码:
/*
*单循环链表的基本操作
*/
#include<stdio.h>
#include<stdlib.h>


 struct node
{
    int data;
    struct node *next;
};

struct node *temp;
//初始化链表
struct node *initList_link()
{
   
    struct node *head;

    head=(struct node *)malloc(sizeof(struct node));
    if(NULL==head)
    {
        exit(-1);
    }
    head->next=head;


    return head;
}
//添加结点
void add(struct node *head,struct node *pCurrent)
{
    pCurrent->next=head->next;
    head->next=pCurrent;
}
//向链表中添加结点
void add_node(struct node *head,struct node *pCurrent)
{
    add(head,pCurrent);
}
//删除结点 返回被删的上一个结点
struct node *delet(struct node *p,struct node *pCurrent )//???????????看看是不是有逻辑错误
{   
   
    temp=pCurrent->next;
    pCurrent->next=temp->next;
    p=temp->next->next;
    return  p;
}

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

    temp=head->next;

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

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

    while(temp!=head)
    {
        if(data==temp->data)
            {
                temp->data=newValue;
            }
        temp=temp->next;
    }
   
}
//删除整个链表
void delet_list(struct node *head)
{
    //struct node *p;
    temp=head->next;

    while(temp!=head)
    {
        delet(temp->next,temp->next->next);
        free(temp);
        temp=head->next;
    }
    free(head);
     head=NULL;
    printf("删除成功!\n");  
}
void print_list(struct node *head)
{
    temp=head->next;

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

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

    printf("初始化链表\n");
    head=initList_link();
    printf("打印该循环链表:");
    print_list(head);

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

    printf("\n\n删除数据为3的结点\n");
    delet_node(head,3);
    printf("打印循环链表:");
    print_list(head);

    printf("\n\n修改数据为3的结点 新值为18\n");
    change_node(head,3,18);
    printf("打印循环链表");
    print_list(head);

    printf("\n\n删除整个链表\n");
    //delet_list(head);

}
int main()
{
    function();

    return 0;
}
申明下这是模仿别人的,
搜索更多相关主题的帖子: 单循环 
2011-07-24 21:27
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
肿么有木有人
2011-07-24 22:39
deng520126
Rank: 2
等 级:论坛游民
帖 子:17
专家分:46
注 册:2011-7-20
收藏
得分:0 
  有什么问题??
2011-07-25 07:42
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
以下是引用deng520126在2011-7-25 07:42:45的发言:

  有什么问题??
代码里不是标了么
2011-07-25 09:13
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
struct node *delet(struct node *p,struct node *pCurrent )
{   
   
    temp=p->next;
    p->next=pCurrent->next;
    return  pCurrent->next;
}

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

    temp=head->next;
    if(data==temp->data)
    {
        p=delet(head,temp);
        free(head);
        temp=p;
    }
    while(temp!=head)
    {
        if(data==temp->next->data)
        {
            p=delet(temp,temp->next);
            free(temp);
            temp=p;
        }
        temp=temp->next;
    }
   
}
没人就自己来
2011-07-25 10:02
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
//删除整个链表
void delet_list(struct node *head)
{
    //struct node *p;
    temp=head->next;

    while(temp!=head)
    {
        delet(temp,temp->next);
        free(temp);
        temp=head->next;
    }
    free(head);
     head=NULL;
    printf("删除成功!\n"); 
}
这个好像木有问题,咋执行不了呢
2011-07-25 10:45
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
删除部分还是有问题,第一个数据删不掉
2011-07-25 12:39
烟雾中的迷茫
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:621
专家分:1069
注 册:2011-2-9
收藏
得分:20 
当然是删不了了,你把第二个结点的指针赋给temp循环是无法执行到的
2011-07-25 17:59
世界模型
Rank: 4
等 级:业余侠客
威 望:1
帖 子:240
专家分:226
注 册:2010-9-12
收藏
得分:0 
程序代码:
/*
*单循环链表的基本操作(带头结点)
*/
#include<stdio.h>
#include<stdlib.h>


 struct node
{
    int data;
    struct node *next;
};

struct node *temp;
//初始化链表
struct node *initList_link()
{
   
    struct node *head;

    head=(struct node *)malloc(sizeof(struct node));
    if(NULL==head)
    {
        exit(-1);
    }
    head->next=head;
   
    return head;
}
//添加结点
void add(struct node *head,struct node *pCurrent)
{
    pCurrent->next=head->next;
    head->next=pCurrent;
}
//向链表中添加结点
void add_node(struct node *head,struct node *pCurrent)
{
    add(head,pCurrent);
}
//删除结点 返回被删的上一个结点
struct node *delet(struct node *pCurrent,struct node *p)
{   
   
    /*temp=pCurrent->next;            //找到要删除的值并保存在temp中
    pCurrent->next=p->next;
    return  p->next;*/

    temp=pCurrent->next;    //找到要删除的值并保存在temp中
    pCurrent->next=temp->next;
    p=temp->next;

    return p;
}

//从链表中删除某结点 与data数值相同的
//你需要一个记录,即记下前面节点,q就是起这个作用
void delet_node(struct node *head,int data)
{
   
    struct node *p;
    struct node *q=head;//<-----------here


    temp=head->next;
   
    while(temp!=head)
    {
        if(data==temp->data)//<-----------here

        {
            p=delet(q,temp);
            free(temp);
            temp=p;
        }

        q=temp;        //<-----------here


        temp=temp->next;
       
    }
   
}

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

    while(temp!=head)
    {
        if(data==temp->data)
            {
                temp->data=newValue;
            }
        temp=temp->next;
    }
   
}
//删除整个链表
void delet_list(struct node *head)
{
    struct node *q;

    q=head;
    temp=head->next;

    while(temp!=head)
    {
        delet(q,temp);
        free(temp);
        temp=head->next;
    }
    free(head);
    head=NULL;
    printf("删除成功!\n");  
}
void print_list(struct node *head)
{
    temp=head->next;

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

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

    printf("初始化链表\n");
    head=initList_link();
    printf("打印该循环链表:");
    print_list(head);

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

    printf("\n\n删除数据为1的结点\n");
    delet_node(head,1);
    printf("打印循环链表:");
    print_list(head);

    printf("\n\n修改数据为4的结点 新值为18\n");
    change_node(head,4,18);
    printf("打印循环链表");
    print_list(head);

    printf("\n\n删除整个链表\n");
    delet_list(head);

}
int main()
{
    function();

    return 0;
}
2011-07-25 18:20
快速回复:欢迎各种大侠进
数据加载中...
 
   



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

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