Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this program you will store data in a linked list, and then modify the conte

ID: 3881000 • Letter: F

Question

For this program you will store data in a linked list, and then modify the contents of the linked list. You MAY NOT use the list or similar containers from the STL. You may use Strings, and standard input/output. There are two data files that will need to be analyzed for this program. The first, data1.txt, contains 150 integers. You will need to read in the file and load the integers into your vector and linked list. The second data file, data2.txt, contains 25 integers, and not all will be found in the data1.txt. You will need to read those values in, find them in your data structure, and delete the value if found. If the value is not found, print to the screen that it is not present. This deletion should not compromise the integrity of your data structure (i.e. the deletion should not lose the back half of the linked list if the 50th element is deleted).

When reading in the integers from data1.txt, every 50 entries output the contents of your linked list. Then after deleting the 25 values from data2.txt, output the final state of your data structure.  

data1.txt:

3212
3131
2225
3643
3458
4263
592
2725
4574
1302
1055
2201
4349
3453
2160
3359
3494
4795
2294
3419
230
2457
4941
1778
2527
949
4813
64
1788
1546
735
604
1331
94
993
754
1117
1752
3697
229
256
3376
4536
2043
4442
2735
4133
254
2504
4267
3287
4457
4645
2713
462
4718
851
3698
4332
779
2584
4123
3801
3590
2893
4069
1709
2147
2848
366
3810
491
571
3666
479
4388
4516
987
3356
2325
0
3511
2198
1663
2761
2420
3989
4727
959
1660
3051
1259
2686
3382
3559
4382
4351
4226
2140
3021
113
4239
2997
732
3775
1450
890
1092
4556
1249
653
635
1828
2130
3197
2667
1035
4835
2782
1311
789
432
4731
702
1958
2565
3506
4999
2625
4510
960
4124
715
2231
3345
3834
3751
3411
1138
2522
1885
2598
180
721
4513
1194
4983
1348
3163
4228

data2.txt: 3212
3643
592
1607
4349
3359
1234
2457
2527
64
735
9156
229
4536
2735
2502
4457
462
3698
2584
3590
1709
366
571
1234

Explanation / Answer

Explaining the solution in C programming as specific language is not mentioned

#include <stdio.h>

#include <stdlib.h>

/* structure of a linked list node */

struct Node

{

int data;

struct Node *next;

};

/*Function to delete an element from the linked list*/

void deleteNode(struct Node **head_ref, int key)

{

// Store head node

struct Node* temp = *head_ref, *prev;

// If head node itself holds the key to be deleted

if (temp != NULL && temp->data == key)

{

*head_ref = temp->next; // Changed head

free(temp); // free old head

return;

}

// Search for the key to be deleted, keep track of the previous node as we need to change 'prev->next'

while (temp != NULL && temp->data != key)

{

prev = temp;

temp = temp->next;

}

// If key was not present in linked list

if (temp == NULL)

{

printf("%d",key);

printf(" ");

}

return;

// Unlink the node from linked list

prev->next = temp->next;

free(temp); // Free memory

}

/* Utility function to insert a node at the begining */

void push(struct Node **head_ref, int new_data)

{

struct Node *new_node =

(struct Node *)malloc(sizeof(struct Node));

new_node->data = new_data;

new_node->next = *head_ref;

*head_ref = new_node;

}

/* Utility function to print a linked list */

void printList(struct Node *head)

{

while(head!=NULL)

{

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

head=head->next;

}

printf(" ");

}

/* program to test above functions */

int main()

{

struct Node *head = NULL;

int k=0;

/* Read the file and store the data1.txt in a linked list */

char str[50];

int num;

FILE *fptr;

if ((fptr = fopen("data1.txt", "r")) == NULL)

{

printf("Error! opening file");

// Program exits if file pointer returns NULL.

exit(1);

}

// Reads the input line by line

while (fgets(str, 50, fptr) != NULL)

{

num=(int)str;

push(&head,num);

}

fclose(fptr);

/* Read the file and store the data2.txt in an array */ char val[50];

FILE *fptr1;

int data2[30],i=0,n,length;

n= sizeof(data2);

length= n/sizeof(int);

if ((fptr = fopen("C:\Users\Surendra\Desktop\souji\data1.txt", "r")) == NULL)

{

printf("Error! opening file");

// Program exits if file pointer returns NULL.

exit(1);

}

// Reads the input line by line

while (fgets(val, 50, fptr1) != NULL)

{

data2[i]=(int)val;

i++;

}

fclose(fptr1);  

printf("Given Linked List: ");

printList(head);

for(k=0;k<n;k++)

{

deleteNode(&head,data2[k]);

}

getchar();

return 0;

}

Output : It Prints the values which are not present in data1.txt

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote