Why does my code skip over certain nodes?

Nihal

I'm writing this code to store data records of students' first name, last name, scores and zip codes. I have almost everything done but my Print() function doesn't print the first element of node 2(first name of 2nd student) and turns into an infinite loop when I input ore than 2 nodes, what am i doing wrong?

#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void Insert(char first[20], char last[20], float data, char zip[50]);
void delete(int e);
void Print();
double median();
struct node
{
    char first_name[20];
    char last_name[20];
    float score;
    char zip_code[50];
    struct node *ptr;
};
int n;
struct node* head =NULL;
struct node* tail=NULL ;
void Insert(char first[20], char last[20], float data, char zip[50])
{

    struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->ptr=NULL;
    strcpy(temp->first_name,first);
    strcpy(temp->last_name,last);
    temp->score=data;
    strcpy(temp->zip_code,zip);

    if(head==NULL)
    {
        head=temp;
        tail=temp;
        return;
    }
    tail->ptr=temp;
    tail=temp;
    free(temp);
    temp=NULL;

}
void delete(int e)
{
    int i;
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    temp=head;
    if(e==1)
    {
        head=temp->ptr;
        free(temp);
        return;
    }
    else if(e==n)
    {
        while(temp->ptr->ptr!=NULL)
        {
            temp=temp->ptr;
        }
        temp->ptr=NULL;
        free(temp->ptr);
        return;
    }
    for(i=0; i<(e-2); ++i)
        temp=temp->ptr;
    struct node *temp1=temp->ptr;
    temp->ptr=temp1->ptr;
    free(temp1);
}
void Print()
{
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    temp=head;
    printf("Data entered is below: \n");
    while(temp!=NULL)
    {

        printf("First Name: %s, Last Name: %s, Score: %.2f, Zip Code: %s",temp->first_name,temp->last_name,temp->score,temp->zip_code);
        temp=temp->ptr;
        printf("\n");
    }
    printf("\n\n\n");
}
double median()
{
    double median,tmp;;
    double *ex=(double*)malloc(sizeof(double));/*head*/
    double *exe=(double*)malloc(sizeof(double));/*dynamic*/
    ex=exe;
    int i=1,term,j;
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    temp=head;
    while(i<=n)
    {
        temp->ptr;
        *exe=temp->score;

        exe++;
    }
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
            if( *(ex+i) > *(ex+j))
            {
                tmp = *(ex+i);
                *(ex+i) = *(ex+j);
                *(ex+j) = tmp;
            }
        }
    }
    if(n%2==0)
    {
        /*even;median=n/2-1*/

        term=(n/2)-1;
        median= (*(ex+(term-1)));
        return median;
    }
    /*odd; median=n-1/2*/
    term=(n-1)/2;
    median= (*(ex+(term-1)));

    return median;
}
int main()
{
    char name1[20],name2[20], code[50];
    float x;
    int i,option,index;
    printf("Enter the number of nodes: ");
    scanf("%d",&n);
    printf("Please enter the records of students in the following format(click enter for new students)\n");
    printf("First_Name Last_Name Score Zip_Code\n");
    for(i=1; i<=n; ++i)
    {

        scanf(" %s",name1);
        scanf(" %s",name2);
        scanf(" %f",&x);
        scanf(" %s",code);
        Insert(name1,name2,x,code);

    }
    printf("\n");
    while(1)
    {
        printf("Choose one of the following options: \n");
        printf("Print records (press 1)\nAdd a new record (press 2)\nDelete record(s) (press 3)\nSearch by zip code (press 4)\nSearch by score range (press 5)\nFind median score (press 6)\nExit the program (press 0)\n");
        scanf("%d",&option);
        switch(option)
        {
        case 0:
        {
            /*Exit Program*/
            exit(0);
            break;
        }
        case 1:
        {
            /*print*/
            Print();
            break;
        }
        case 2:
        {
            /*insert*/
            getchar();
            printf("Enter the new record in the following format: \nFirst_Name Last_Name Score Zip_Code\n");
            scanf("%s",name1);
            scanf("%s",name2);
            scanf("%f",&x);
            scanf("%s",code);
            getchar();
            Insert(name1,name2,x,code);

            break;
        }
        case 3:
        {
            /*delete*/
            printf("Enter the node/record to be deleted: ");
            scanf("%d",&index);
            delete(index);
            printf("The deletion of record %d has been succesfully completed!\n\n",index);
            break;
        }
        case 4:
        {
            /*search by zip*/

            break;
        }
        case 5:
        {
            /*search by score*/

            break;
        }
        case 6:
        {
            /*find median*/
            printf("Median score for the entered records is: %f",median());
            break;
        }

        }/*switch*/
    }/*while*/
    return 0;

}
jspcal

The Insert function is holding a reference to freed data:

tail=temp;
free(temp);

Attempting to use the storage referenced by tail after it's been freed is an error.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my code not find a certain string that is present in an array?

From Dev

Why does my recursive function to inspect controls skip over every other control?

From Dev

why does javascript skip some of my functions?

From Dev

why does javascript skip some of my functions?

From Dev

Why does my loop skip even steps?

From Dev

Why does the code skip the for-loop

From Dev

Why does Xcode skip for loop code?

From Dev

Why does my for loop skip my last values in my array?

From Dev

Why does this code raise IndexError in certain situation?

From Dev

Why does PHPDoc error in PHPStorm over this code?

From Dev

Why does Mockito skip the initialization of the member variable of my abstract class

From Dev

Why does my call to an MVS POST function skip the RedirectToAction?

From Dev

Why does the compiler skip my second scanf loop?

From Dev

Why does my loop skip every second list entry?

From Dev

Why does my MVar freeze my code?

From Dev

Why does my MVar freeze my code?

From Dev

With ng-repeat skip over certain entries

From Dev

Why does `treemap.nodes` not see the update to my object?

From Dev

Why does the optimizer remove my code?

From Dev

Why does my php code return inf?

From Dev

Why does this code block my main thread?

From Dev

Why does minifyEnabled show most of my code?

From Dev

Why does my code always end with undefined?

From Dev

Why does my code run into an infinite loop?

From Dev

why does my pyautogui code not works good

From Dev

What this code does? and Why my alarm is not stopped?

From Dev

Why does my java code not compile

From Dev

Why does my code say 109 is not a prime?

From Dev

Why does my Matlab code not work correctly?

Related Related

HotTag

Archive