Question

Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile correctly and produce the specified output. Please note that your programs should comply with the commenting and formatting rules we discussed in class. Please see the descriptive ile on eLearning for details. Program 1- Linear Search Algorithm In Computer Science, it is often very important to be able to locate a specific data item inside a list or collection of data. Algorithms that perform this function are called searching algorithms, and there are many such algorithms in Computer Science. Although it is inefficient, one of the most common searching algorithms is called Linear Search. In Linear Search we have a set of data that serves as the standard, usually stored within an array, and a separate value that we are searching for within that data set. Wed like to know whether the value is within the data set, so we scan through the data set looking for it, one element at a time, starting at the beginning of the array and proceeding, if necessary, to the very last element. If the value is found within the standard array, we return a number indicating its index position within the array. If the value is not found, we return an error indicator, oftentimes a -1, that indicates the value was not in the data set For this problem, please implement a linear search algorithm that performs this function. You will be given two input files, “LSS tandard. txt and “LSTest . txt. The LSStandard. txt file contains integer values against which we are searching. (There will be no more than 100 of these.) The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. (There will be no more than 50 of these.) Read both of these into separate arrays and then determine which of the numbers in the LSTest file are included in the LSStandard data set by using a Linear Search algorithm. Have your program print out a report (to the console only is sufficient) that indicates whether the number was found or not. Your output should look something like: Number 1 79) was located at index 44 Number 2 74) was not in the file. Number 3 56) was not in the file. Number 4 (103) was located at index 75

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*
* C Program to Implement Singly Linked List using Dynamic Memory Allocation
*/
#include <stdio.h>
#include <malloc.h>
#define ISEMPTY printf("\nEMPTY LIST:");
/*
* Node Declaration
*/
struct node
{
int value;
struct node *next;
};

snode* create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void delete_pos();
void search();
void update_val();
void display();
void rev_display(snode *);

typedef struct node snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;

/*
* Main :contains menu
*/

int main()
{
int ch;
char ans = 'Y';

while (ans == 'Y'||ans == 'y')
{
printf("\n---------------------------------\n");
printf("\nOperations on singly linked list\n");
printf("\n---------------------------------\n");
printf("\n1.Insert node at first");
printf("\n2.Insert node at last");
printf("\n3.Insert node at position");
  
printf("\n4.Delete Node from any Position");
printf("\n5.Update Node Value");
printf("\n6.Search Element in the linked list");
printf("\n7.Display List from Beginning to end");
printf("\n8.Display List from end using Recursion");
printf("\n9.Exit\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("\nEnter your choice");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("\n...Inserting node at first...\n");
insert_node_first();
break;
case 2:
printf("\n...Inserting node at last...\n");
insert_node_last();
break;
case 3:
printf("\n...Inserting node at position...\n");
insert_node_pos();
break;
  
case 4:
printf("\n...Deleting Node from any Position...\n");
delete_pos();
break;
case 5:
printf("\n...Updating Node Value...\n");
update_val();
break;
case 6:
printf("\n...Searching Element in the List...\n");
search();
break;
case 7:
printf("\n...Displaying List From Beginning to End...\n");
display();
break;
case 8:
printf("\n...Displaying List From End using Recursion...\n");
rev_display(first);
break;
case 9:
printf("\n...Exiting...\n");
return 0;
break;
default:
printf("\n...Invalid Choice...\n");
break;
}
printf("\nYOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &ans);
}
return 0;
}

/*
* Creating Node
*/
snode* create_node(int val)
{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf("\nMemory was not allocated");
return 0;
}
else
{
newnode->value = val;
newnode->next = NULL;
return newnode;
}
}

/*
* Inserting Node at First
*/
void insert_node_first()
{
int val;

printf("\nEnter the value for the node:");
scanf("%d", &val);
newnode = create_node(val);
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\n----INSERTED----");   
}

/*
* Inserting Node at Last
*/
void insert_node_last()
{
int val;

printf("\nEnter the value for the Node:");   
scanf("%d", &val);
newnode = create_node(val);
if (first == last && last == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
last->next = newnode;
last = newnode;
last->next = NULL;
}
printf("\n----INSERTED----");
}   

/*
* Inserting Node at position
*/
void insert_node_pos()
{
int pos, val, cnt = 0, i;

printf("\nEnter the value for the Node:");
scanf("%d", &val);
newnode = create_node(val);
printf("\nEnter the position ");
scanf("%d", &pos);
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf("\nInserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf("\n----INSERTED----");
}
else
{
printf("Position is out of range");
}
}

/*
* Delete Node from specified position in a non-empty list
*/
void delete_pos()
{
int pos, cnt = 0, i;

if (first == NULL)
{
ISEMPTY;
printf(":No node to delete\n");
}
else
{
printf("\nEnter the position of value to be deleted:");
scanf(" %d", &pos);
ptr = first;
if (pos == 1)
{
first = ptr->next;
printf("\nElement deleted");   
}
else
{
while (ptr != NULL)
{
ptr = ptr->next;
cnt = cnt + 1;
}
if (pos > 0 && pos <= cnt)   
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
}
else
{
printf("Position is out of range");
}
free(ptr);
printf("\nElement deleted");
}
}
}
/*
* Updating Node value in a non-empty list
*/
void update_val()
{
int oldval, newval, flag = 0;

if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to update\n");
}
else
{
printf("\nEnter the value to be updated:");
scanf("%d", &oldval);
printf("\nEnter the newvalue:");   
scanf("%d", &newval);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
if (ptr->value == oldval)
{
ptr->value = newval;
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nUpdated Successfully");
}
else
{
printf("\nValue not found in List");
}
}   
}

/*
* searching an element in a non-empty list
*/
void search()
{
int flag = 0, key, pos = 0;

if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list\n");
}
else
{
printf("\nEnter the value to search");
scanf("%d", &key);
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
pos = pos + 1;
if (ptr->value == key)
{
flag = 1;
break;
}
}
if (flag == 1)
{
printf("\nElement %d found at %d position\n", key, pos);
}
else
{
printf("\nElement %d not found in list\n", key);
}
}   
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
if (first == NULL)
{
ISEMPTY;
printf(":No nodes in the list to display\n");
}
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{   
printf("%d\t", ptr->value);
}
}
}

/*
* Display non-empty list in Reverse Order
*/
void rev_display(snode *ptr)
{
int val;

if (ptr == NULL)
{
ISEMPTY;
printf(":No nodes to display\n");
}
else
{
if (ptr != NULL)
{
val = ptr->value;
rev_display(ptr->next);
printf("%d\t", val);   
}

}
}

OUTPUT:
$gcc linkedlist.c
a.out
---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice7

...Displaying List From Beginning to End...

EMPTY LIST::No nodes in the list to display

YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------
  
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice4

...Deleting Node from any Position...

EMPTY LIST::No node to delete

YOU WANT TO CONTINUE (Y/N)
y

---------------------------------

Operations on singly linked list

---------------------------------


1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice5

...Updating Node Value...

EMPTY LIST::No nodes in the list to update

YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice
6

...Searching Element in the List...

EMPTY LIST::No nodes in the list

YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice
3

...Inserting node at position...

Enter the value for the Node:1010

Enter the position 5
Position is out of range
YOU WANT TO CONTINUE (Y/N)

---------------------------------

Operations on singly linked list

---------------------------------
  
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice1

...Inserting node at first...

Enter the value for the node:100

----INSERTED----
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------
  
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice1

...Inserting node at first...

Enter the value for the node:200

----INSERTED----
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice7

...Displaying List From Beginning to End...
200 100
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice2

...Inserting node at last...

Enter the value for the Node:50

----INSERTED----
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------
  
1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice2

...Inserting node at last...

Enter the value for the Node:150

----INSERTED----
YOU WANT TO CONTINUE (Y/N)y

---------------------------------

Operations on singly linked list

---------------------------------


1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice8

...Displaying List From Beginning to End...
200 100 50 150
YOU WANT TO CONTINUE (Y/N)

---------------------------------

Operations on singly linked list

---------------------------------

1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice3

...Inserting node at position...

Enter the value for the Node:1111

Enter the position 4

----INSERTED----
YOU WANT TO CONTINUE (Y/N)Y

---------------------------------

Operations on singly linked list

---------------------------------


1.Insert node at first
2.Insert node at last
3.Insert node at position
4.Delete Node from any Position
5.Update Node Value
6.Search Element in the linked list
7.Display List from Beginning to end
8.Display List from end using Recursion
9.Exit


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Enter your choice9

...Exiting...

Add a comment
Know the answer?
Add Answer to:
Arrays Arravs Introduction Your ninth programming assignment will consist of two C++programs. Your programs should compile...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains...

    C++ code The assignment has two input files: • LSStandard.txt • LSTest.txt The LSStandard.txt file contains integer values against which we are searching. There will be no more than 100 of these. The LSTest.txt file contains a set of numbers that we are trying to locate within the standard data set. There will be no more than 50 of these. Read both files into two separate arrays. Your program should then close both input files. All subsequent processing will be...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves...

    Using Arrays with Sorting and Searching Algorithms 1) This program has six required outputs and involves searching and sorting an array of integers. Write a java application that initializes an array with the following numbers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Then display the unsorted values. This is required output #1 of 6 for this program. 2) Using a...

  • The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an ar...

    The purpose of this assignment is to familiarize you with sort algorithms. Problem Description is as follows: 8. Search Benchmarks Write a program that has at least 20 250 integers stored in an array in ascending order. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that...

  • This lab is to give you more experience with C++ Searching and Sorting Arrays Given a...

    This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program. Read Data Write a function that reads in data from a file using the prototype below....

  • C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going t...

    C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going to compare 3 different algorithms.You are going to do operational studies on them to determine their order. O(N), O(log(N)), and O(1)? Search algorithms.Linear, Binary, Hash You should know which algorithms are what order,now you are going to prove it. Fill the 3 arrays with the...

  • C++ please read all the question Create 3 large arrays to search, where the size will...

    C++ please read all the question Create 3 large arrays to search, where the size will be variable up until it reaches your machines limit. They should contain the same information since we are going to compare 3 different algorithms.You are going to do operational studies on them to determine their order. O(N), O(log(N)), and O(1)? Search algorithms.Linear, Binary, Hash You should know which algorithms are what order,now you are going to prove it. Fill the 3 arrays with the...

  • Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from...

    Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from the previous assignment and modify it so that it uses an array of Car objects as the principal data structure instead of an ArrayList-of-Car. This is an example of the OOP principle of information hiding as your Car and test classes will not have to be modified at all. Unless you broke encapsulationon the previous assignment, that is   II. Specifications Specifications for all 3...

  • Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that...

    Detecting Substrings (C++ Version) Introduction A very common task that is often performed by programs that work with text files is the problem of locating a specific substring within the file. I am sure we’ve all done this many times when working with Word, Notepad, or other editors. Since we don’t have a GUI or other means of displaying the contents of a file all at once, let’s modify the problem slightly. Rather than locating a specific substring within a...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT