1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
antoniya [11.8K]
3 years ago
13

In this part, you have to implement a linked list that maintains a list of integers in sorted order. Thus, if the list contains

2, 5 and 8, then 1 will be inserted at the start of the list, 3 will be inserted between 2 and 5 and 10 will be inserted at the end. The list can contain duplicate elements. 1 Input format: This program takes a le name as an argument from the command line. The le is either blank or contains successive lines of input. Each line contains a character, either `i' or `d', followed by a tab character and then an integer. For each of the lines that starts with `i', your program should insert that number in the linked list in sorted order. If it is already there, your program can insert it before or after the existing entry. If the line starts with a `d', your program should delete the rst value if it is present in the linked list. If there are duplicates your program must delete just the rst occurrence of the value. Your program should silently ignore the line if the requested value is not present in the linked list.
Computers and Technology
1 answer:
kupik [55]3 years ago
7 0

Answer:

#include <stdio.h>   // header file

#include <stdlib.h>

struct node{    //define structure "node"

int data;

struct node* next;    // object

};

struct node* add(struct node* head, int num);

struct node* delete(struct node* head, int num);

void print(struct node* head);

int size(struct node* head);

int main(int argc, char* argv[]){

FILE *fp = NULL;

struct node* head = NULL;

int num;

char command;

if(argc != 2){

printf("Please provide input filename as a command line argument\n");

return 1;

}

fp = fopen(argv[1], "r");

if(fp == NULL){

printf("error\n");

return 1;

}

fscanf(fp, "%c %d", &command, &num);

while(!feof(fp)){

if(command == 'i')

head = add(head, num);

else if(command == 'd')

head = delete(head, num);

fscanf(fp, "%c %d", &command, &num);

}

fclose(fp);

printf("%d\n",size(head));

print(head);

}

struct node* add(struct node* head, int num){

struct node* prev = NULL;

struct node* curr = head;

struct node* n = (struct node*) malloc(sizeof(struct node));

n->data = num;

n->next = NULL;

while(curr != NULL){

if(num < curr->data) //found a place to insert

break;

else if(num == curr->data) //duplicate

return head;

prev = curr;

curr = curr->next;

}

n->next = curr;

if(prev != NULL)

prev->next = n;

else

head = n;

return head;

}

struct node* delete(struct node* head, int num){

struct node* prev = NULL;

struct node* curr = head;

while(curr != NULL){

if(num < curr->data)

return head;

else if(num == curr->data)

break;

prev = curr;

curr = curr->next;

}

if(curr == NULL) //did not find

return head;

if(prev == NULL) //remove 1st node

head = curr->next;

else

prev->next = curr->next;

free(curr);

return head;

}

void print(struct node* head)

{

struct node* curr = head;

if(head != NULL){

printf("%d", curr->data);

curr = curr->next;

while(curr != NULL){

printf("\t%d", curr->data);

curr = curr->next;

}

}

printf("\n");

}

int size(struct node* head){

struct node* curr = head;

int count = 0;

while(curr != NULL){

count++;

curr = curr->next;

}

return count;

}

Output:

Provide input filename as a command-line argument

Test2 sg$ ./.s.out in.txt

2  5  8

1  2  3  5  10

Test2 sg$  

Explanation:

we create a link list of integer in sorted order then if we input as 2  5  8, then 1  insert at the starting of the list and 3 in middle of 2 and 5 and 10 is inserted at the last.

You might be interested in
4.
KATRIN_1 [288]

Income inequality and  Digital literacy prevents many people from having access to technology and the Internet.

<h3> How does digital system affect people's lives?</h3>

People do not have access to internet because of poor economic and social problems, such as fewer job opportunities, less competitive economies and others.

Other Factors such as digital literacy and low income levels, geographical restrictions, lack of zeal to use technology are known to have contributed to the digital division in the country.

Learn more about Income inequality from

brainly.com/question/24554155

6 0
3 years ago
1. What is the difference between background sound and nat sound
OleMash [197]

Answer:

Well the background sound is the sound of course in the background and the nat sound is the curremt sound

Explanation:

6 0
2 years ago
The use of multiple _______ is sometimes called using a search phrase.
zysi [14]
Hello, thank you for your question.

The answer to your question, "<span>The use of multiple _______ is sometimes called using a search phrase.," would be:

Keywords</span>
4 0
3 years ago
How do you insert a new row into a worksheet
OleMash [197]
<span>Do one of the following

To insert a single column, select the column or a cell in the column immediately to the right of where you want to insert the new column. ...

 Do one of the following: Right-click the selected cells and then click Insert.</span>
3 0
4 years ago
Which of the statements below are true about Digital Signatures?
Leokris [45]

Answer:

The answer is "Option a, and c".

Explanation:

Following are the description of the Digital Signature choices:

  • To change the physical transportation of paper or link files in the computerized messaging system, some processes should be identified and utilized in digital signatures, which enables documentation to be validated unforgivably.
  • Using public-key cryptography, digital signatures, that's an exquisite system, that's why options a and b are correct.
  • Digital signatures are not a central authority, that's why the option B is wrong.
5 0
3 years ago
Other questions:
  • Which of the following is FALSE?
    8·1 answer
  • The CPU (central processing unit), also known as a processor, is considered the brain of the computer. What is the CPU’s functio
    6·1 answer
  • Read the following scenario and decide who, if anyone, is committing sexual harassment:
    13·2 answers
  • Speed is how fast an object moves a certain distance within a length of time. How is speed calculated?
    14·2 answers
  • we studied FIFO, Priority, Round Robin (RR), and Weighted Fair Queueing (WFQ) packet scheduling disciplines. Which of these queu
    12·1 answer
  • Is there a way I can put an already made sound that i created over an already made video
    6·1 answer
  • On an unweighted scale, a grade of A is worth _____ points
    7·1 answer
  • Use a physical stopwatch to record the length of time it takes to run the program. Calculate the difference obtained by calls to
    11·1 answer
  • You have been having issues with your printer, so you decide to learn more about how the printer works. You learn that if you re
    12·1 answer
  • Which of these are the two main components of a CPU?
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!