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
Jill is interested in a career as a paramedic. She is trained to use medical equipment, she remains calm under pressure, and she
Igoryamba

This question is incomplete because it lacks the appropriate options

Complete Question:

Jill is interested in a career as a paramedic. She is trained to use medical equipment, she remains calm under pressure, and she has good bedside manner. Which career pathway would best fit Jill’s interests and skills?

A. Security and Protective Services

B. Law Enforcement Services

C. Emergency and Fire Management Services

D. Correction Services

Answer:

c) Emergency and Fire Management Services

Explanation:

Emergency and Fire Management Services is a career path or occupation where personnels work to ensure that there is a prompt emergency response to incidents or accidents whereby the safety of human lives and properties are threatened.

Emergency and Fire Management services deal with the following incidents listed below:

a) Fire incidents

b) Car accidents

c) Medical emergencies

Staffs or Personnels that work in Emergency and Fire Management services:

a) Fire Fighters

b) Paramedics

Personnels who work with Emergency and Fire Management services should have the following traits or characteristics.

a) They must be calm regardless of any situations they are in

b) They must have the ability and training to use essential medical equipments.

c) They must have excellent people skills as well as good bedside manners.

d) They must possess the ability to work under intense pressure

e) They must possess the ability to calm victims of fire or car accidents

In the question above, the career pathway that is best for Jill based on the skills and interests that she possesses is a career pathway in Emergency and Fire Management Services.

7 0
4 years ago
How is social media changing the world as we know it?
Alisiya [41]

Answer:

Explanation:

Social media is making it easier to make a lot of noise about a pressing issue in society, such as human rights violations.

I use social media because I want to keep tabs on people I know.

A change in social media is that people have become increasingly overreliant on it. We tend to spend increasing amounts of time on social media.

Social media should be INFORMATIVE about presidential elections, and it should not be used to spread baseless information. This way, people are more informed about the facts of the candidates and their platforms.

Social media has paved the way for me to make new relationships to important people in my life.

Social media does pose a threat to family and leisure time when said family members tend to be overreliant on the phones and ignore those around them.

The person who is addicted to social media is myself because I spend several hours on it everyday— and that much interaction is too much because it should be one hour a day at most.

I have experienced cyberbullying; the solution to this problem is accountability and finding those who instigate it to bring them to justice.

3 0
3 years ago
If you arrive at the same time as another user straight across from you yield if ___.
Tcecarenko [31]

Answer:

D

Explanation:

i just took the same exact quiz

4 0
4 years ago
Read 2 more answers
Kali linux os and window os who is the best​
kogti [31]

Answer:

Windows

Explanation:

It is down to preference. I prefer windows due to its large compatibility with a wide range of apps.

4 0
3 years ago
Read 2 more answers
Match each definition to the correct type of media.
Murrr4er [49]

Answer:

1-2

2-3

3-1

Explanation:

I couldn't find a way to explain this.

6 0
3 years ago
Read 2 more answers
Other questions:
  • What would be the best thing you could do to prepare yourself to work for a company that has embraced globalization?
    9·2 answers
  • Consider the following table used for grading assignments.
    10·1 answer
  • I’m stuck in a class where i don’t understand anything, it’s only been 3 days.. any tips for learning computer coding?
    12·1 answer
  • Digital art is created by using __?
    5·1 answer
  • is either the number of bits used to indicate the color of a single pixel, or the number of bits used for each color component o
    7·1 answer
  • You have just replaced the motherboard in your computer. now your computer will not start. you press the power button on the sys
    6·1 answer
  • Nonverbal messages from the movie it​
    5·2 answers
  • when you enter a formula in a cell the result of the calculation displays in a cell. how do views of formula after entering it?
    9·2 answers
  • Third party providers of publicly available data sets protect the anonymity of the individuals in the data set primarily by a. R
    6·1 answer
  • Create a program that receives the age of a user and determine whether he/she can vote or not. Assume the voting age to be 20. C
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!