如何实现删除功能? [英] How do I implement delete funtion ?

查看:119
本文介绍了如何实现删除功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//这是一个链表程序。在以下程序中,用户输入姓名,姓氏,邮政编码和学生分数。给定程序的删除功能必须使用用户的姓氏,然后删除具有该姓氏的所有学生的条目。我在如何实现这个删除功能方面遇到了麻烦。完整的代码如下。 //





// This is a linked list program. In the following program, the user enters the name , last name , zip code and scores of students. The delete function for the given program must take in a last name from the user and then delete the entries of all the students with that last name. I am having trouble as to how to implement this delete function. The complete code is as follows. //


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print();
void insert();
void searchzip();
void range();
void median();
void delete();
int counter=0;
struct student
{
char firstname[21];
char lastname[21];
float score;
char zip[6];
struct student *next;
};
int n;
struct student *head;

int main()
{
int i,option;
head=NULL;
do
{
printf("\nType the number of records you want to enter (Please ensure that the number is more than or equal to 5) :");
scanf("%d",&n);
}
while(n<5);
for(i=0;i<n;i++)
    {
    struct student *new,*current;
    new=(struct student*)malloc(sizeof(struct student));
    printf("\nnPlease input records of studnent with following format first name last name zipcode score\n");
    scanf("%s%s%s%f",new->firstname,new->lastname,new->zip,&new->score);
    new->next=NULL;
    if(head==NULL)
        {
        head=new;
        current=new;
        }
    else
        {
        current->next=new;
        current=new;
        }
    }
do
{
    printf("\nPlease choose the appropriate options :");                                        /*Switch statement for choosing options.*/
    printf("\nPrint records (press 1)\nAdd a new record (press 2)\nDelete Record(s) (press 3)\nSearch by ZipCode (press 4)\nSearch by score range (press 5)\nFind Median Score (press 7)\nExit the program (press 0)\n");
    scanf("%d",&option);
    switch(option)
        {
        case 1  :
            print();
            break;
        case 2  :
            insert();
            break;
        case 3  :
            delete();
            break;
        case 4  :
            searchzip();
            break;
        case 5  :
            range();
            break;
        case 7  :
            median();
            break;
        case 0  :
            break;
        default : printf("\n Please enter a valid option.");
        }
}
while(option>=1 && option<=5);
return 0;
}

void print()
{
struct student*temp=head;
printf("\nThe following are the student records :");
while(temp!=NULL)
{
printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
temp=temp->next;
}
printf("\n");
}

void insert()
{
struct student *new,*current,*temp;
new=(struct student *)malloc(sizeof(struct student));
printf("\nnPlease input records of studnent with following format first name last name zipcode score\n");
scanf("%s%s%s%f",new->firstname,new->lastname,new->zip,&new->score);
if(head==NULL)
    {
    head=new;
    current=new;
    }
else
    {
    temp=head;
    while(temp->next!=NULL)
        {
        temp=temp->next;
        }
    temp->next=new;
    }
}

void searchzip()
{
char code[6];
printf("\nPlease enter the zip code that you want to search :\n");
scanf("%s",code);
struct student *temp=head;
do
{
if(strcmp(code,temp->zip)==0)
    {
    printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
    }
temp=temp->next;
}
while(temp!=NULL);
}

void range()
{
float max,min;
printf("\nPlease enter the maximum and minimum score range :");
scanf("%f %f",&max,&min);
struct student *temp=head;
do
{
if((temp->score>=min)&&(temp->score<=max))
    {
    printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
    }
temp=temp->next;
}
while(temp!=NULL);
}

void median()
{
float arr[n],tempvar,med;
int i=0,j,pos;
struct student *temp=head;
do
{
arr[i]=temp->score;
i++;
temp=temp->next;
}
while(temp!=NULL);
print();
for(i=0;i<n-1;i++)
    {
    for(j=0;j<n-i-1;j++)
        {
        if(arr[j]>arr[j+1])
            {
            tempvar=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=tempvar;
            }
        }
    }
if(n%2==0)
    {
    pos=n/2;
    med=(arr[pos]+arr[pos-1])/2.0;
    }
else
    {
    pos=(n/2);
    med=arr[pos];
    }
printf("\nThe median of all the students is : %f",med);
printf("\nThe students having a score above the median are :");
struct student *temp_2=head;
do
{
if((temp_2->score)>=med)
    {
    printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp_2->firstname,temp_2->lastname,temp_2->score,temp_2->zip);
    }
temp_2=temp_2->next;
}
while(temp_2!=NULL);
}

void delete()
{
int counter=0;
char name[21];
printf("\nPlease enter the last name that you want to delete :\n");
scanf("%s",name);


}

推荐答案

你有一个单链表: http://en.wikipedia.org/wiki/Linked_list [ ^ ]



步骤如下:



1.找到您要删除的节点 - 目标

2.找到上一个节点。

3.在上一个节点中指向下一个节点目标节点指向。

4.删除目标节点





http://www.geeksforgeeks.org/delete-a-given-node-in-linked -list-under-given-constraints / [ ^ ]
You have a singly linked list: http://en.wikipedia.org/wiki/Linked_list[^]

The steps are:

1. Find the node you wish to delete - target
2. Find the previous node.
3. Point next in previous node to the next that target node points to.
4. Delete target node


http://www.geeksforgeeks.org/delete-a-given-node-in-linked-list-under-given-constraints/[^]


这篇关于如何实现删除功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆