简化我的链接列表"搜索和删除"功能

时间:2014-07-16 04:38:16

标签: c linked-list

该函数要求用户输入要搜索的值,然后删除列表中的所有匹配值。

程序运行完美,但我无法找出一种简单/优雅的方法来分离“第一个节点”,“最后一个节点”和“中间节点”的案例。

请帮助我使它更优雅和可读。

我会发布相关的代码块,然后是整个程序。

在相关函数中,以下代码块是我正在使用的switch-case结构的一部分(如果用户输入'v',则表示搜索'value'并删除,而不是'删除第一个node'或'删除最后一个节点')

case 'v':
            printf("\n\tEnter value to delete:\t");
            scanf("%d",&value);
            r=q;
            if(!q)//If linked list is empty
            {
                printf("\n\t\tLinked list is EMPTY\n");
                return;
            }
            else if(!(q->next))//If linked list consists of only one node
            {
                if((q->data)==value)
                {
                    free(q);
                    printf("\nValue found and deleted in position 1\n");
                    count=1;
                }
            }
            else//If linked list consists of more than one node
            {
                for(i=1;q;i++)
                {
                    if(q->data==value)
                    {
                        if(i==1)//for first node
                        {
                            q=q->next;
                            free(r);
                            *pp=q;
                            printf("\nValue found and deleted in position 1\n");
                            count=1;
                        }
                        else//for the rest of the nodes
                        {
                            printf("\nValue found and deleted in position %d\n",i);
                            r->next=q->next;
                            temp=q;
                            q=q->next;
                            free(temp);
                            count++;
                        }
                    }
                    else
                    {
                        r=q;
                        q=q->next;
                    }
                }
            }
            if(count==0)//If no matches are found
                printf("Value not found");
            break;

完成计划:

#include <stdio.h>
#include <stdlib.h>

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

int count(struct node *);
void traverse(struct node *);
void insert(struct node **);
void delete(struct node **);
void deletelist(struct node **);

int main()
{   char choice;
    struct node *head, **tohead;
    head=(struct node *)malloc (sizeof(struct node));
    head=NULL;
    tohead=&head;
    do
    {
        printf("\nChoose operation to perform:\n");
        printf("\tCount number of nodes(c):\n");
        printf("\tInsert a node(i)\n");
        printf("\tDelete a node(d)\n");
        printf("\tDelete the list(D)\n");
        printf("\tShow currenct linked list(s)\n");
        printf("\tQuit without saving changes(q):\t");
        scanf(" %c",&choice);
        switch(choice)
        {
            case 'i':
                insert(tohead);
                break;
            case 'c':
                printf("\nList count is %d\n",count(head));
                break;
            case 's':
                traverse(head);
                break;
            case 'q':
                printf("QUITTING\n");
                break;
            case 'd':
                delete(tohead);
                break;
            case 'D':
                deletelist(tohead);
                break;
            default:
                printf("\n\tINVALID CHOICE\n");
        }
    }while(choice!='q');
}

int count(struct node *p)
{
    int i;
    for(i=0;p;i++)
        p=p->next;
    return i;
}

void traverse(struct node *p)
{
    printf("\nLinked list looks like: ");

    if(!p)
    {
        printf("nothing. It's EMPTY");
    }
    else
    {   while(p)
        {
            printf("%d\t",p->data);
            p=p->next;
        }
    }
    printf("\n");
}

void insert(struct node **pp)
{
    int value,position;
    struct node *q;
    q=*pp;
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    printf("Insert what number?:\t");
    scanf("%d",&value);
    printf("In what position? Push '0' for last,'1' for first");
    printf("\n\t\tOR\nenter position no.:\t");
    scanf("%d",&position);
    newnode->data=value;
    if(position==1)
    {   newnode->next=q;
        *pp=newnode;
    }
    else if(position==0)
    {
        if(!q)
        {
            newnode->next=q;
            *pp=newnode;
        }
        else
        {
            while(q->next)
                q=q->next;
            q->next=newnode;
            newnode->next=NULL;
        }
    }
    else if((position>1)&&(position<=count(q)+1))
    {
        int i;
        for(i=1;i<position-1;i++)
            q=q->next;
        newnode->next=q->next;
        q->next=newnode;
    }
    else
        printf("\n\t\tINVALID POSITION\n");
}

void delete(struct node **pp)
{
    struct node *q,*r,*temp;
    q=*pp;
    int i,count=0,value,position;
    char choice;
    printf("\n\tPush 'v' to delete a specific value");
    printf("\n\t\t\tOR");
    printf("\n\t'1' to delete the first value");
    printf("\n\t'0' to delete the last value:\t");
    scanf(" %c",&choice);
    if(q==NULL)
    {   printf("List is EMPTY\n");
        return;
    }
    switch(choice)
    {
        case '1':
            *pp=q->next;
            free(q);
            break;
        case '0':
            while(q->next!=NULL)
            {
                r=q;
                q=q->next;
            }
            r->next=NULL;
            free(q);
            break;
        case 'v':
            printf("\n\tEnter value to delete:\t");
            scanf("%d",&value);
            r=q;
            if(!q)//If linked list is empty
            {
                printf("\n\t\tLinked list is EMPTY\n");
                return;
            }
            else if(!(q->next))//If linked list consists of only one node
            {
                if((q->data)==value)
                {
                    free(q);
                    printf("\nValue found and deleted in position 1\n");
                    count=1;
                }
            }
            else//If linked list consists of more than one node
            {
                for(i=1;q;i++)
                {
                    if(q->data==value)
                    {
                        if(i==1)//for first node
                        {
                            q=q->next;
                            free(r);
                            *pp=q;
                            printf("\nValue found and deleted in position 1\n");
                            count=1;
                        }
                        else//for the rest of the nodes
                        {
                            printf("\nValue found and deleted in position %d\n",i);
                            r->next=q->next;
                            temp=q;
                            q=q->next;
                            free(temp);
                            count++;
                        }
                    }
                    else
                    {
                        r=q;
                        q=q->next;
                    }
                }
            }
            if(count==0)//If no matches are found
                printf("Value not found");
            break;
        default:
            printf("\nBad choice");
    }return;
}

void deletelist(struct node **pp)
{
    struct node *p,*temp;
    p=*pp;
    while(p)
    {
        temp=p;
        p=p->next;
        free(temp);
    }
    *pp=NULL;
    printf("\nLIST DELETED\n");
}

1 个答案:

答案 0 :(得分:0)

void filter(struct node **pp, int value){
    struct node *curr, *prev=NULL, *tmp;
    curr = *pp;
    if(!curr)
        return ;
    tmp = curr;
    while(tmp){
        if(tmp->data != value){
            curr->data = tmp->data;
            prev = curr;
            curr = curr->next;
        }
        tmp = tmp->next;
    }
    deletelist(&curr);
    if(prev){
        prev->next = NULL;
    } else {
        *pp = NULL;
    }
}