Question

CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a...

CODE IN C++

Given:

typedef char ItemType;

// a struct type named NodeType that includes a ItemType and pointer

// (to a NodeType) field

struct NodeType {

   ItemType value;

   NodeType * next;

};

bool Delete (NodeType * & firstPtr, ItemType value)

{

   NodeType * prev, * cur;

cur = Search (firstPtr, value, prev);

if (cur==NULL)

return false;

else {

// remove cur node from the linked list

if (prev!=NULL)

{ //not the first one

prev->next = cur->next; //bypass the cur node

}

else //cur is the first node

firstPtr = cur->next; //update the head pointer

delete cur; //deallocate the node itself

return true;

}

}

  1. Write a function to search for a value in the linked list, returning both the matching node, and the node before it, i.e. Search function. Note that this function can be used by the following functions:

/* Search for given value in the listed list pointed to by firstNodePtr,

* return the matching node's address, and

* set its previous node's address in prevNode parameter

@param firstNodePtr

@param value

@postcondition: if the value is found in the linked list,

   the node's address will be returned, and

   prevNode will point to the node before; if not found, return NULL, prevNode is NUL L

@return value: same as Search...

*/

NodeType * Search (NodeType * firstNodePtr, ItemType value, NodeType * & prevNode)

{

//Todo by you

}

  1. Write a function insert a new node before a node (of a giving value). Write a function that insert a number before an existing node in the linked list storing a certan value, e.g.,

If the linked list contains 1 ->10 -> 30,

and we want to insert a number 20 before node 30, we can call the above

function passing 20, 30 as parameters (in addition to the head), and

after the insertion, the list looks like:

1 -> 10 -> 20 -> 30.

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

Though, I have documented the code still if you have any doubt feel free to comment. I'll try my best to resolve them as soon as possible.

PS: This program can be done using classes in that case we don't have to pass firstPtr n every function. Also, we can use Search() in insertBefore() and i leave this up to you for practice still if you need any help do let me know.

===============

Code:

#include <iostream>
using namespace std;
typedef int ItemType;

// a struct type named NodeType that includes a ItemType and pointer
// (to a NodeType) field
struct NodeType {
ItemType value;
NodeType * next;
};

void insertBefore(NodeType* &firstPtr, ItemType value, ItemType target)
       {
       NodeType *t=new NodeType;
       t->value=value;
       t->next=NULL;
       NodeType *p=firstPtr;
           //at the beginning
           if(firstPtr==NULL)
           {
               t->next=NULL;
               firstPtr=t;
               return;
           }
           //if insertbefore first node
           if(firstPtr->value==target)
           {
               t->next=firstPtr;
               firstPtr=t;
               return;
           }
           NodeType *q=firstPtr;
           p=p->next;
           //searching to get the target node
           while(p!=NULL)
           {
               if(p->value==target)
               {
               t->next=q->next;
               q->next=t;
               return;
               }
               q=p;
               p=p->next;
           }
           //if not found
           if(p==NULL)
           {
           cout<<"\nTarget is not found in the list. Hence, inserting at the end.";
           t->next=NULL;
           q->next=t;
           }
           return;
       }
NodeType* Search(NodeType * firstNodePtr, ItemType value, NodeType * & prevNode)
       {
NodeType *p=firstNodePtr;
prevNode=NULL;
if(p->value==value)
{
return p;
}
p=p->next;
prevNode=firstNodePtr;
           while(p!=NULL)
           {
               if(p->value==value)
               {
                   return p;
               }
               prevNode=p;
               p=p->next;
           }
           //not found
           prevNode=NULL;  
           return NULL;
}
  
bool Delete (NodeType * & firstPtr, ItemType value)
{
NodeType * prev, * cur;
cur = Search (firstPtr, value, prev);
if (cur==NULL)
return false;
else{
// remove cur node from the linked list
if (prev!=NULL)
{ //not the first one
prev->next = cur->next; //bypass the cur node
}
else //cur is the first node
firstPtr = cur->next; //update the head pointer
delete cur; //deallocate the node itself
return true;
  
}
}
  
void disp(NodeType * firstPtr)
{
NodeType *p=firstPtr;
if(p==NULL)
{
cout<<"Nothing to display!";
return;
}
cout<<"\n"<<p->value;
p=p->next;
while(p!=NULL)
{
cout<<" -> "<<p->value;
p=p->next;
}
return;
}
  
int main()
{
NodeType* firstPtr=NULL;
disp(firstPtr);
insertBefore(firstPtr,30,0);
disp(firstPtr);
insertBefore(firstPtr,10,30);
disp(firstPtr);
insertBefore(firstPtr,1,10);
disp(firstPtr);
insertBefore(firstPtr,20,30);
disp(firstPtr);
insertBefore(firstPtr,150,45);
disp(firstPtr);
if(Delete(firstPtr,1))
{
cout<<"\nDeleted 1 Sucessfully!";
disp(firstPtr);
}else
cout<<"\nNot Found!";
if(Delete(firstPtr,45))
{
cout<<"\nDeleted 45 Sucessfully!";
disp(firstPtr);
}else
cout<<"\nNot Found!";
return 0;
}
==============

Syntax:

===============

Output:

Add a comment
Know the answer?
Add Answer to:
CODE IN C++ Given: typedef char ItemType; // a struct type named NodeType that includes a...
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
  • Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p,...

    Consider the following C++ statements: struct node Type int info; node Type *link; nodeType *head, *p, q, *newNode; newNode = new node Type; 1. Write C++ statement to store 50 in the info field of the newNode. 2. Write C++ statement to set the link field of the newNode to NULL. 3. Write C++ statement to make head pointer points to newNode. 4. Write C++ statement to delete the first node in the linked list. (the first an the only...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • () Given the following structure definition and typedef for a linked list of strings: typedef struct...

    () Given the following structure definition and typedef for a linked list of strings: typedef struct node st node; struct node st { char *word; /* a valid string pointer or NULL */ node *next; /* next node in the list or NULL */ }; Write a C function, free list(), that takes as an argument one of these lists, possibly NULL, and frees all the strings as well as the list itself. Write robust code. void free list(node *list){

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • I want the full code for this part and Its needs to be done in C++...

    I want the full code for this part and Its needs to be done in C++ Use a linked list to implement the following skeleton of an unsorted list typedef int ItemType: struct NodeType ItemType item; NodeType "next; class List List(); // default constructor List( const List &x); I copy constructor: deep copy is required List & operator = (const List &x); // assignment operator: deep copy. 1s required bool IsThere(ItemType x); identify if x is in the list void...

  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member...

    ***Using C++ and also please label "a" and "c" accordingly. Thank you!!**** a) Write the member function void deleteNode() that will delete the first node from the linked list. b) Modify the LList to create a data member Node *cur that points to the current node. Write the following member functions: • void forward() that moves the cur pointer to the next node. • void backward() that moves the cur pointer to the previous node. • void delete() that removes...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

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