Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
// 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. //


C#
#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);


}
Posted

1 solution

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/[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-May-15 0:20am    
5ed. And I'll add: don't use these hard-coded immediate constants 1, 2, 3, ...; define explicit constants or defines, but better enum.
—SA
[no name] 12-May-15 0:55am    
Agree.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900