| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 343 人关注过本帖
标题:大家帮帮我看看这个 【双向的链表程序】
只看楼主 加入收藏
nikobelic
Rank: 2
等 级:论坛游民
帖 子:37
专家分:30
注 册:2012-5-26
结帖率:25%
收藏
 问题点数:0 回复次数:3 
大家帮帮我看看这个 【双向的链表程序】
删除节点后  输出有错误  
程序代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct node
{
    char name[20];
    struct node *prior,*next;
}stud;
stud *creat(int n)  //此处用到了typedef的功能
{
    stud *p,*h,*s;
    int i;
    h = (stud *)malloc(sizeof(stud));
    h->name[0] = '\0';
    h->prior = NULL;
    h->next = NULL;
    p = h;
    for (i=0;i<n;i++)
    {
        s = (stud *)malloc(sizeof(stud));
        p->next = s;
        printf("输入第%d学生的姓名\n",i+1);
        scanf("%s",s->name);
        s->prior = p;
        s->next = NULL;
        p = s;
    }
    p->next = NULL;
    return (h);
}
stud *search(stud *h,char *x)
{
    stud *p;
    char *y;
    p = h->next;
    while(p)
    {
        y = p->name;
        if(strcmp(y,x) == 0)
        {
            return (p);
        }
        else
            p = p->next;
    }
    printf("Can't Find It\n");
}
void del(stud *p)
{
    p->next->prior = p->prior;
    p->prior->next = p->next;
    free(p);
}
void main()
{
    int number;
    char sname[20];
    stud *head,*sp;
    puts("Long of the link\n");
    scanf("%d",&number);
    head = creat(number);
    sp = head->next;
    printf("Now The Link Is :\n");
    while(sp)
    {
        printf("%s   ",sp->name);
        sp = sp->next;
    }
    printf("\n");
    printf("Input the name you want to search\n");
    scanf("%s",sname);
    sp = search(head,sname);
    del(sp);
    printf("Now the Link is \n");
    while(sp)
    {
        printf("%s ",sp->name);
        sp = sp->next;
    }
    printf("\n");
    puts("\nAnykey to exit");
}

搜索更多相关主题的帖子: next 链表程序 
2012-07-23 10:37
westfall999
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:40
专家分:101
注 册:2012-7-22
收藏
得分:0 
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include<stdlib.h>

typedef struct node
{
    char name[20];
    struct node *prior,*next;
}stud;

stud *creat(int n)  //此处用到了typedef的功能
{
    stud *p,*h,*s;
    int i;
    h = (stud *)malloc(sizeof(stud));
    if(h == NULL)
        exit(-1);
    h->name[0] = '\0';
    h->prior = NULL;
    h->next = NULL;
    p = h;

    for (i=0;i<n;i++)
    {
        s = (stud *)malloc(sizeof(stud));
        if(s == NULL)
            exit(-1);
        p->next = s;
        printf("输入第%d学生的姓名\n",i+1);
        scanf("%s",s->name);
        s->prior = p;
        s->next = NULL;
        p = s;
    }
    //p->next = NULL;
    p->next =h;    //最后一个结点的next指针指向头结点
    h->prior=p;    //头结点的前指针指向最后一个结点,这样才能形式循环
    return (h);
}
stud *search(stud *h,char *x)
{
    stud *p;
    char *y;
    p = h->next;
    while(p !=h)
    {
        y = p->name;
        printf("%s %s \n",y,p->name);
        if(strcmp(y,x) == 0)
        {
            return (p);
        }
        else
            p = p->next;
    }
    printf("Can't Find It\n");
    return NULL;    //如果未找到,则返回NULL指针
}

void del(stud *p)
{
    if(p!=NULL)
    {
        p->next->prior = p->prior;
        p->prior->next = p->next;
        free(p);
        p=NULL;    //防止p成为野指针,这是一个好习惯
    }
}

void main()
{
    int number;
    char sname[20];
    stud *head,*sp;
   
    puts("Long of the link\n");
    scanf("%d",&number);
    head = creat(number);
    sp = head->next;
    printf("Now The Link Is :\n");

    //while(sp)
    while(sp !=head)    //因为循环链表中最后一个结点的下一个结点就是头结点,所以判断条件要改成这样
    {
        printf("%s   ",sp->name);
        sp = sp->next;
    }
    printf("\n");
    printf("Input the name you want to search\n");
    scanf("%s",sname);
    sp = search(head,sname);
    del(sp);
    printf("Now the Link is \n");

    sp=head->next;    //sp结点已被删除,需要重新赋值
    //while(sp)
    while(sp !=head)    //判断条件两样需要改变
    {
        printf("%s ",sp->name);
        sp = sp->next;
    }
    printf("\n");
    puts("\nAnykey to exit");
}

2012-07-23 15:44
westfall999
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:40
专家分:101
注 册:2012-7-22
收藏
得分:0 
以下是引用westfall999在2012-7-23 15:44:25的发言:

#include
#include
#include
#include

typedef struct node
{
    char name[20];
    struct node *prior,*next;
}stud;

stud *creat(int n)  //此处用到了typedef的功能
{
    stud *p,*h,*s;
    int i;
    h = (stud *)malloc(sizeof(stud));
    if(h == NULL)
        exit(-1);
    h->name[0] = '\0';
    h->prior = NULL;
    h->next = NULL;
    p = h;

    for (i=0;i
补充一下,上面我将它改成了循环双向链表,如果只想保持双向而已,则只需将sp=head->next;重新赋值即可,外加在del函数中要判断一下传给形参p的指针是不是为空。
2012-07-23 15:55
wengege
Rank: 2
等 级:论坛游民
帖 子:148
专家分:93
注 册:2012-7-23
收藏
得分:0 
你的是VC++ 6.0?

打好基础,学会站在巨人的肩膀上!
2012-07-23 16:34
快速回复:大家帮帮我看看这个 【双向的链表程序】
数据加载中...
 
   



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

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