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
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
ADVERTISEMENT