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
Whats a hybrid wired network
LuckyWell [14K]
A hybrid network is any computer network that uses more than one type of connecting technology or topology. For example, a home network that uses both Wi-Fi and Ethernet cables to connect computers is a hybrid.
5 0
3 years ago
Attribute variables have the same meaning as participants variables. is this true or false?​
zysi [14]

Answer:

true

Explanation:

because I said so dude

5 0
2 years ago
Match the following:
mash [69]

Answer:

a:1, b:4, c:5, d:2, e:3

3 0
2 years ago
_____ rows indicate that there is different formatting for odd and even rows.
Sladkaya [172]
Banded rows indicate that there are different formatting for odd and even rows.
5 0
3 years ago
Many inventions have enabled us to use digital cameras. The biggest difference between traditional and digital cameras is that d
Andreyy89

1 Film Roll Vs SD card.

2 No LCD Display Vs LCD Display

3 No mega Pixels Vs Mega Pixels

4 Zero Optical zoom Vs Optical Zoom

5 No Picture Modes Vs Different Picture Modes

6 Limited Number of Pics per roll Vs unlimited number of pictures depending upon the size of a Card

7 Hassle of developing pictures Vs no hassle, simply transfer them to the pc/laptop

8 Limited features Vs multiple features

hope this helps

8 0
3 years ago
Other questions:
  • Using a database of precomputed hashes from sequentially calculated passwords called a(n) __________, an attacker can simply loo
    14·1 answer
  • This toolbar can be used to change the way the text in your presentation looks. Drawing
    5·2 answers
  • Which code fragment constructs an arrray list named players that is initialized to contain the strings "player 1" and "player 2"
    15·1 answer
  • Consider the partially-filled array named a. What does the following loop do? (cin is a Scanner object)int[] a = {1, 3, 7, 0, 0,
    6·1 answer
  • In this website/app what are the points for?
    7·1 answer
  • Jemima is reviewing her history report and notices that her headings are not formatted the same throughout the
    7·1 answer
  • In an information systems framework, ________ is the bridge between the computer side on the left and the human side on the righ
    14·1 answer
  • Assume the existence of an UNSORTED ARRAY of n characters. You are to trace the CS111Sort algorithm (as described here) to reord
    14·1 answer
  • Type the correct answer in the box. Spell the word correctly. A company has its branches spread over five places in a state. It
    14·1 answer
  • What are the common camera hazards? When might you encounter these hazards in your life, and what do you plan to
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!