Question

In: Computer Science

Write a program to implement linked list data structure that will have following functions: a. Append...

Write a program to implement linked list data structure that will have following functions:
a. Append a node in the list
b. Insert a node in the list
c. Delete a node from the list
d. Display list
e. Find maximum value in the list
f. Find how many times a value exists in the list.
g. Search
Portion of the code is give below. You have to write code for the items (e, f, g)

Program:
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
struct node {
int data;
node* next;
};
node* head=NULL;
void appendNode(int value){
node *newNode, *curr;
newNode = new node();
newNode->data=value;
newNode->next=NULL;
if (!head)
head=newNode;
else // Otherwise, insert newNode at end
{
curr=head;
while (curr->next)
curr = curr->next;
curr->next = newNode; }
}
void insertNode(int value){
node *newNode, *curr, *previous;
}
newNode = new node();
newNode->data = value;
if (head==NULL) { head = newNode;
newNode->next = NULL;
}
else {
curr = head;
while (curr != NULL && curr->data < value) previous = curr;
curr = curr->next;
}
if (previous == NULL)
{head = newNode;
newNode->next = curr;}
else {previous->next = newNode;
newNode->next = curr;}
}
{
void displayList(void) { node *curr;
curr = head;
while (curr!=NULL)
{
std::cout << curr->data <<" ";
curr = curr->next;

}
std::cout << "\n";
}
void deleteNode(int value){ node *deletedNode, *curr; if (curr == NULL){
// Deleting first node deletedNode = head; head = head ->next;
} else{
// Deleting other nodes deletedNode = curr->next; curr->next = deletedNode ->next;
}
delete deletedNode;
}
int findmaxNode(){
//complete the code here
}
int countNode(){
//complete the code here
}
int search_item() {
int found;
return found; }
int main()
{
int choice;
int x;
do{
system("cls");
std::cout << "1. Append Node"<<"\n"; std::cout << "2. Insert Node"<<"\n";
std::cout << "3. Delete Node"<<"\n";
std::cout << "4. Find Maximum Node"<<"\n"; std::cout << "5. Display Linked List"<<"\n";
std::cout << "6. Search Node"<<"\n";
std::cout << "7. Count nodes "<<"\n";
std::cout << "8. Exit"<<"\n";
std::cout << "Enter your choice(1-8):"; std::cin>>choice;
switch(choice) {
case 1:
std::cout<<"Enter a value:";
std::cin>>x;
appendNode(x);
std::cout<<"value is appened\n";
break;
case 2:
std::cout<<"Enter a value to insert:";
std::cin>>x;
insertNode(x);
std::cout<<"Value is inserted\n";
break;
case 3:
std::cout<<"complete code\n";
std::cout<<"Enter a value to delete:";
std::cin>>x;
deleteNode(x);
//complete code first break;
case 4:
std::cout<<"complete code\n";
//int max = maxNode();
//complete code first //std::cout<<"Maximum is :"<<max;
break;
case 5:
displayList();
break;
case 6:
std::cout<<"Complete code for search\n"; int x;
x= search_item(); if(x==0)
std::cout<<"Not found\n";
else
std::cout<<"found\n";
break;
case 7:
std::cout<<"Complete code\n";
//int count = count_nodes();
//std::cout<<"Number of nodes in the list is:"<<count; break;

}
system("pause");
} while(choice!=8);
}

Solutions

Expert Solution

As per the question we have to implement linked list with given objectives as:

a. Append a node in the list

b. Insert a node in the list

c. Delete a node from the list

d. Display list

e. Find maximum value in the list

f. Find how many times a value exists in the list.

g. Search

As per above objectives you need help only in (E,F,G) as per your mention. So I am implementing the expected result in the program given by you itself. As I am giving you proper discrption og code implemented by me as part of comments. Do refer that for your guidance.

//Start of Code

#include<stdlib.h>

#include<stdio.h>

#include<iostream>

struct node {

int data;

node* next;

};

node* head=NULL;

void appendNode(int value){

node *newNode, *curr;

newNode = new node();

newNode->data=value;

newNode->next=NULL;

if (!head)

head=newNode;

else // Otherwise, insert newNode at end

{

curr=head;

while (curr->next)

curr = curr->next;

curr->next = newNode; }

}

void insertNode(int value){

node *newNode, *curr, *previous;

}

newNode = new node();

newNode->data = value;

if (head==NULL) { head = newNode;

newNode->next = NULL;

}

else {

curr = head;

while (curr != NULL && curr->data < value) previous = curr;

curr = curr->next;

}

if (previous == NULL)

{head = newNode;

newNode->next = curr;}

else {previous->next = newNode;

newNode->next = curr;}

}

{

void displayList(void) {

node *curr;

curr = head;

while (curr!=NULL)

{

std::cout << curr->data <<" ";

curr = curr->next;

}

std::cout << "\n";

}

void deleteNode(int value){ node *deletedNode, *curr; if (curr == NULL){

// Deleting first node deletedNode = head; head = head ->next;

} else{

// Deleting other nodes deletedNode = curr->next; curr->next = deletedNode ->next;

}

delete deletedNode;

}

int findmaxNode(){              //finding max from the list.

struct node *curr = head;

int max;

if(head=NULL){                     //if head is empty that means list is empty.

cout<<”Linked List is empty\n”;

}

else{

max= head->data;                //setting max with head node.

while(curr!=NULL)

{

If(max<curr->data){

//if current node value is greater than max then change current value to max value.

max = curr-> data;

}

curr=curr->next;

}

Cout<<”The maximum value from the linked list is %d\n”<<max;    //Printing maximum value.

}

}

}

int countNode(node *head, int searching){    //for counting the number of occurrences.

node *curr = head;

int cnt=0;

while( curr != NULL)       //until current node is null or not.

{

cnt++;

cnt = cnt -> next;             

}

return cnt;                 //returning node.

}

int search_item(int item,int m) {

struct node *temp;

int found=0;        //position of search.

int cnt;

temp=head;

if(head == NULL) //checking if head is null or not.

{

cout<<”List is empty\n”;

}

else{

while(temp != NULL)   //until temp is null

{

If(temp -> data ==item){                     // if item is present in the list

cout<<”item is found at:”<<(found+1);

cnt=0;

}

else

{

cnt++;

}

found++;

temp=temp -> next;

}

If(cnt == m)           //if item is not present in the list

{

cout<<”Item is not found in the list\n”;

}

}

}

int main()

{

int choice;

int x;

do{

system("cls");

std::cout << "1. Append Node"<<"\n"; std::cout << "2. Insert Node"<<"\n";

std::cout << "3. Delete Node"<<"\n";

std::cout << "4. Find Maximum Node"<<"\n"; std::cout << "5. Display Linked List"<<"\n";

std::cout << "6. Search Node"<<"\n";

std::cout << "7. Count nodes "<<"\n";

std::cout << "8. Exit"<<"\n";

std::cout << "Enter your choice(1-8):"; std::cin>>choice;

switch(choice) {

case 1:

std::cout<<"Enter a value:";

std::cin>>x;

appendNode(x);

std::cout<<"value is appened\n";

break;

case 2:

std::cout<<"Enter a value to insert:";

std::cin>>x;

insertNode(x);

std::cout<<"Value is inserted\n";

break;

case 3:

std::cout<<"complete code\n";

std::cout<<"Enter a value to delete:";

std::cin>>x;

deleteNode(x);

//complete code first break;

case 4:

std::cout<<"complete code\n";

//int max = maxNode();

//complete code first //std::cout<<"Maximum is :"<<max;

break;

case 5:

displayList();

break;

case 6:

std::cout<<"Complete code for search\n"; int x;

x= search_item(); if(x==0)

std::cout<<"Not found\n";

else

std::cout<<"found\n";

break;

case 7:

std::cout<<"Complete code\n";

//int count = count_nodes();

//std::cout<<"Number of nodes in the list is:"<<count; break;

}

system("pause");

} while(choice!=8);

}

As by above code we can fully implement the data structure called list linked list.


Related Solutions

Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
How do you write an append function for a linked list that is a that has...
How do you write an append function for a linked list that is a that has a O(1) Big O notation in Python? The one given in class is O(n). He recommended using a variable to track the end the list. This is the code I have written so far. I don't think I'm properly defining the tail in the add possibly class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return...
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Write a program that create a single linked list and consist of all the necessary functions...
Write a program that create a single linked list and consist of all the necessary functions to do the following Add an element to the list, insertion position can be anywhere in the list (first, last and middle) delete an element from the list, deletion position can be anywhere in the list (first, last and middle) Note: You need to add proper documentation to your programs and you need to include the output of each program C++
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class...
C++ coding functions Implement the following class using linked lists. Creating a simple linked list class to use is probably your first step.(Please do not use collections like .push() . pop(), etc.) and instead create the implementation A templated stack class that stores type T with the following public functions: - void Push(T t) - adds t to the top of the stack - T Pop() - asserts that the stack is not empty then removes and returns the item...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT