Question

In: Computer Science

Write a program in C as per following requirements: a) It takes two fields from the...

Write a program in C as per following requirements:

a) It takes two fields from the user 1) NAME and 2) age. Hint: (Take the name of employee

in an array of 50 characters).

b) It saves the data given by the user in a doubly link list.

c) As soon a record is entered by the user it should be given a record number or serial

number.

d) Program should have a menu for

a. Entering new data.

b. Printing a particular record by serial number.

c. Printing the whole data with serial numbers.

d. Exit the program.

Solutions

Expert Solution

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

int count=0;
struct node
{
    char name[50];
    int sno;
    int age;
    struct node *next;
    struct node *pre;
};
struct node *cur=NULL,*head=NULL;//head is the start node and cur is the current node
void add()/*function to add new nodes*/
{
    struct node *newnode=(struct node*) malloc(sizeof(struct node));
    char name[50];
    int age;
    printf("Enter the name :");
    scanf("%s",name);
    printf("Enter the age :");/*inputting name and age and assigning it to the node*/
    scanf("%d",&age);
    strcpy( newnode->name,name);
    newnode->age=age;
    count++;/*glogal variable which gets incremented when a new node comes*/
    newnode->sno=count;
    if(head==NULL)
    {
        head=newnode;  /*checks if head is null. then assign it is assigned with new node*/
        head->pre=NULL;
        head->next=head;
        cur=head;
        
    }
    else
    {
        newnode->pre=cur;
        newnode->next=NULL;/*current node'snextis assigned to new node.previousnode's next is assigned to current node*/
        cur->next=newnode;
        cur=newnode;/*new node is assigned tocurrent node to keep track of nodes*/
    }
    
    
}

void displayslno(int sno)
{
    struct node *temp=head;
    int f=0;
    while(temp!=NULL)/*loop through all nodes*/
    {
        if(temp->sno==sno)/*checks if sno entered equals node's sno*/
        {
        printf("\nSerial Number : %d",temp->sno);
        printf("\nName :%s",temp->name);
        printf("\nAge :%d",temp->age);
        temp=temp->next;
        f=1;/*flag to check if the condition satisfies*/
        break;
        }
       
    }
    if(f==0)
    {
        printf("\nInvalid Serail Number\n");
    }
}

void display()
{
    struct node *temp=head;/*function to display all nodes*/
    while(temp!=NULL)
    {
        printf("\nSerial Number : %d",temp->sno);
        printf("\nName : %s",temp->name);
        printf("\nAge : %d",temp->age);
        temp=temp->next;
    }
}

int main()
{
    
    struct node *new_nd = (struct node*) malloc(sizeof(struct node));
    
    int sno;
    int ch,choice;
    do
    {
       printf("Press options(1/2/3/4)\n1.Enter new data\n2. display a single record\n3.Display entire data\n4.Exit\n");
    scanf("%i",&ch);
   
    switch(ch)
    {
        case 1:add();
               break;
        case 2:printf("Enter the slno:");
                scanf("%i",&sno);
                displayslno(sno);
               break;
        case 3:display();
               break;
        case 4:printf("Exiting!!!");
                break;
        default:printf("Invalid Choice\n");       
    }
    if(ch!=4)
    {
    printf("\nPress 1 to continue\n");
    scanf("%d",&choice);
    }
    }while((choice==1)&&(ch!=4));
    
    return 0;
}


Related Solutions

IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
3. Write a C++ program that takes in the name of a store, and the following...
3. Write a C++ program that takes in the name of a store, and the following details for 4 employees working at the store; their first name, last name, number of hours they worked that week and how much they are paid per hour. Your program should output the name of the store, along with each employee's full name and how much they earned that week in the manner below. Monetary values should be format to 2 decimal places. Also...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a program in Objective C that takes an integer keyed in from the terminal and...
Write a program in Objective C that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in Eglish. So if the user types 647, the program should display the following: six four seven
c# language Write a program that takes in a number from the user. Then it prints...
c# language Write a program that takes in a number from the user. Then it prints a statement telling the user if the number is even or odd. If the number is odd, it counts down from the number to 0 and prints the countdown on the screen, each number on a new line. If the number is even, it counts down from the number to 0, only even numbers. For example, if the user enters 5, the output will...
Write a C++ program that takes input from the keyboard of the 3 dimensions of a...
Write a C++ program that takes input from the keyboard of the 3 dimensions of a room (W,L,H in feet) and calculates the area (square footage) needed to paint the walls only. Use function overloading in your program to pass variables of type int and double. You should have two functions: one that accepts int datatypes and one that accepts double datatypes. Also assume a default value of 8 if the height parameter is omitted when calling the functions.
1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT