标题:各位大虾,我的链表为什么删除功能执行不了啊
取消只看楼主
陈便往
Rank: 2
等 级:论坛游民
帖 子:61
专家分:32
注 册:2011-9-21
结帖率:55.56%
已结贴  问题点数:20 回复次数:0 
各位大虾,我的链表为什么删除功能执行不了啊
#include<stdio.h>
#include<stdlib.h>
typedef struct part
{
    int number;
    struct part *next;
}node;
node *search(node*head,int n);
node*insert(node*head,int n);
node*dele(node*head,int n);
void print(node*head);
int main()
{
    node*head=NULL;
    char ch;
    int n;
    for(; ;)
    {
        printf("input ch");
        scanf(" %c",&ch);
        switch(ch)
        {
        case'i':
            printf("input n");scanf("%d",&n);
            head=insert(head,n);
            break;
        case'd':
            printf("input n");
            scanf("%d",&n);
            head=dele(head,n);
            break;
        case's':
            printf("input n");
            scanf("%d",&n);
            head=search(head,n);
            break;
        case'p':
            print(head);break;
        default:
           printf("you make a mistake");
        }
    }
}
node* insert(node*head,int n)
{
    node *new_node;
    new_node=malloc(sizeof(node));
    if(new_node==NULL)
    {
        printf("no space lefted");
        exit(0);
    }
    else
    {new_node->number=n;
      new_node->next =head;
      return new_node;
    }
}
node *dele(node*head,int n)
{
    node *ctr,*ptr;
    for(ctr=head,ptr=NULL;ctr->number !=n&&ctr!=NULL;ptr=ctr,ctr=ctr->next)
        ;
    if(ctr=NULL)
        return head;
    if(ptr=NULL)
        head=head->next;
    else
        ptr->next=ctr->next;
    free(ctr);
    return head;
}
node *search(node*head,int n)
{
    node *ptr;
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
        if(ptr->number=n)
            return ptr;
    else
        return NULL;
}
void print(node*head)
{
    node *ptr;
    for(ptr=head;ptr!=NULL;ptr=ptr->next)
        printf("%d",ptr->number);
}
搜索更多相关主题的帖子: dele include search number 
2011-11-12 14:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-355072-1-1.html




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

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