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
Http://moomoo.io/?server=8:16:0
love history [14]

Explanation:

noooooooooooooooooooooooooooooooooo

5 0
3 years ago
Read 2 more answers
Which of the following tasks requires you to use the Tabs option?
Monica [59]
Indenting a paragraph :)
7 0
3 years ago
What type of compensation does a travel agent receive from suppliers?
insens350 [35]

The travel agents receive compensation from suppliers such as waiting time compensation, night ride compensation, and cancellation compensation.

<u>Explanation</u>:

  • The travel agents from ola, uber and other companies usually receive compensation such as 10 percent commission of booking fees.  Usually, if the customer cancels the ride the travel agent will receive compensation for that ride.  They also receive time compensation for their work.
  • For long rides, the travel agents will receive extra compensation for that. Because the distance for that ride will be long. So they will receive these types of compensations from suppliers.    

6 0
3 years ago
Read 2 more answers
What email server does Global Enterprises use?
harina [27]

Answer:

Global Enterprises uses an open source mail server called Courier.

Explanation:

6 0
3 years ago
Classify each of the following acts as a violation of confidentiality, of data integrity, source integrity, or availability, or
AlexFokin [52]

Answer:

A) Violation of Confidentiality and Source Integrity:

Passwords are very confidential information and are usually protected by both the owner and the ower of the system (which in this case is Yahoo). To obtain the passwords, the hacker had to illegally gain access to the source of the data which is on Yahoo servers. Hence a breach of confidentiality and lack of adequate integrity on the part of Yahoo.

B) Source Integrity: Anthony accidentally cuts the electricity from the server room. Why would he be able to do that? how did he gain access into the server room? This speaks to source integrity. People should not be able to access the server room except they are authorised.

C) Source Integrity and Confidentiality: NSA finds an efficient method to break AES. AES refers to the Advanced Encryption Standard. AES is a symmetric encryption technology known as a block cipher.

It is used by the United States Government for the classification of information. Their ability to break this code speaks to Violation of Source Integrity. In this case, they haven't exactly hacked any document. However, any document that was encrypted using this technology is already compromised at the source.

D)   Anna registers the domain name, "JohnSmith" and refuses to let John Smith buy or use the domain name: Violation of Availability.

It is assumed here that John Smith was the original owner of the domain but it was highjacked at expiration and registered by Anna. This kind of breach is called Violation of Availability of the domain name.

E) Some malware encrypts the victim’s hard drive with a secret key and a criminal asks for a ransom to decrypt.

This move is called the Ransomeware attack. The Integrity of the source of information (which is the hard drive) is already compromised. This is a combination of Source Integrity or availability and confidentiality violation.

F) The wiretapping of a cell phone is a breach of confidentiality and data integrity.

G) when there is a vulnerability in a system or software that is unknown to those who should be interested, it is called a Zero-Day vulnerability. Where there is a Zero-Day vulnerability there could be a violation of confidentiality, data integrity, source data and or it's availability.

Cheers!

4 0
3 years ago
Other questions:
  • Which of the following best explains why some people invest their saving in the stock market and others put their saving in bank
    5·2 answers
  • 2. What is the layout of the modern keyboard known as?
    13·1 answer
  • What feature of a word processing program helps you to easily check and correct spelling mistakes?
    9·1 answer
  • _____ can render data useless to unauthorized users who might have violated some of the database security layers.
    7·1 answer
  • Are to print or find<br> the sum<br> 5 numbers<br> of<br> using flow chart
    6·1 answer
  • The remove() method in the Queue interface ________. Group of answer choices retrieves, but does not remove, the head of this qu
    14·1 answer
  • Write bash script which takes array as an input of size 10 bind its even indexes to accept even values and odd indexes to accept
    5·1 answer
  • The Python language uses a compiler which is a program that both translates and executes the instructions in a high-level langua
    15·1 answer
  • Can someone reply me
    11·1 answer
  • Select the correct technical term for each definition by using the drop-down menus.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!