Question

1.1 llist: Linked list Write a program llist that maintains and manipulates a sorted linked list of integers according to instructions received from standard inpu. The linked list is maintained in order, meaning that each integer will be larger than the one preceeding it. 1list maintains a linked list containing zero or more integers, ordered from least to greatest llist will need to allocate space for new nodes as they are created using malloc; any allocated space should be deallocated using free before 11ist terminates. It supports two operations: insert n Adds an integer n to the list. If n is already present in the list, it does nothing. The instruction format is an i followed by a space and an integer n delete n Removes an integer n from the list. If n is not present in the list, it does nothing. The instruction format is a d followed by a space and an integer n After each command, llist will print the length of the list followed by the contents of the list in order from first (least) to last (greatest) Your program will halt once it reaches the end of standard input Input format Each line of the input contains an instruction. Each line begins with a letter (either i, or d), followed by a space, and then an integer. A line beginning with-i indicates that the integer should be inserted into the list. A line beginning withd indicates that the integer should be deleted from the list. Your program will not be tested with invalid input. You may choose to have llist terminate in response to invalid input. Output format After performing each instruction, 11ist will print a single line of text containing the length of the list, a colon, and the elements of the list in order, all separated by spaces. Usage Because llist reads from standard input, you may test it by entering inputs line by line from the terminal. You may use any invalid input, such as #, to terminate your session. $./111st
media%2F001%2F0013d4b4-9aeb-4b62-a5e0-3d
media%2F947%2F94767e00-f62f-450e-8982-ef
This is being done in c using command line in Linux
0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
#include<stdlib.h>

/* Link list node */
struct Node
{
    int value;
    struct Node* next;
};

/* A utility function to create a new node */
struct Node *newNode(int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));

    /* put in the data  */
    new_node->value  = new_data;
    new_node->next =  NULL;

    return new_node;
}

/* function to insert a new_node in a list. Note that this
  function expects a pointer to head_ref as this can modify the
  head of the input linked list*/
void sortedInsert(struct Node** head_ref, int value)
{
    struct Node* current;

    struct Node* new_node = newNode(value);

    /* Special case for the head end */
    if (*head_ref == NULL || (*head_ref)->value >= new_node->value)
    {
        new_node->next = *head_ref;
        *head_ref = new_node;
    }
    else
    {
        /* Locate the node before the point of insertion */
        current = *head_ref;
        while (current->next!=NULL &&
               current->next->value < new_node->value)
        {
            current = current->next;
        }
        new_node->next = current->next;
        current->next = new_node;
    }
}

/* Function to print linked list */
void printList(struct Node *head)
{
    struct Node *t = head;
    while(t != NULL)
    {
        printf("%d  ", t->value);
        t = t->next;
    }
}

void delete(struct Node *head, int value){
    struct Node * temp = head;
    struct Node * prev = NULL;
    while (temp != NULL){
        // looking for match in list
        if (temp->value == value) {
            // if match is found delete and free the memory
            prev->next = temp->next;
            free(temp);
            break;
        }
        prev = temp;
        temp = temp->next;
    }
}

int main()
{
    /* Start with the empty list */
    struct Node* head = NULL;
    int size = 0, item; // to store the size of list
    char c;
    while (1) {
        scanf("%c", &c); // check which query is inputted by user
        if(c == 'i'){
            //insert into list
            scanf("%d", &item);
            sortedInsert(&head, item);
            size++;
        }

        else if (c == 'd'){
            // delete from list
            scanf("%d", &item);
            delete(head, item);
        }

        else if(c == '#'){
            // if any of the above query is not matched program exists
            break;
        }

        printf("%d : ", size);
        printList(head);
        printf("\n");
        getchar();
    }
}

OUTPUT

i 5 1 5 d 3 1 5 i 3 2 3 5 i 500 3 3 5 500 d 5 3: 3 500

Add a comment
Know the answer?
Add Answer to:
This is being done in c using command line in Linux 1.1 llist: Linked list Write...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • Write a C program, named sortit, that reads three integers from the command line argument and...

    Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...

  • Command Line Arguments: Write a program in C Compiler that will accept two integers on command...

    Command Line Arguments: Write a program in C Compiler that will accept two integers on command line, subtract the second from the first (1st - 2nd) and display the answer. It exactly two numbers are not provided on command line, there should be a simple error message printed and the program ends with no further input, output, or calculations

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained...

    Using Unix/Linux Command line in terminal and C programming. (b) Write a basic C program contained in a1q4.c and perform the following tasks in your program’s main() function: (8 marks) • Declare and initialize at least one variable of these types: char, int, unsigned int, float, double and long double. When initializing your variables, ensure the constants you use are of the same type as your variable. See the “Constants” section of Topic 7 for more information. • Use printf(3)...

  • C++ Write a program that reads three positive integers (> 0) from the command line (one...

    C++ Write a program that reads three positive integers (> 0) from the command line (one at a time), then computes and prints the smallest entered number. Use if-else statements for three integer comparison. Example: Enter an integer: 5 Enter an integer: 23 Enter an integer: 7 The smallest number is: 5 Example 2: Enter an integer: 3 Enter an integer: 3 Enter an integer: 6 The smallest number is: 3 "The input must be positive number. The program should...

  • IN JAVA Write a program that first gets a list of integers from input. The input...

    IN JAVA Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT