Question

C++ codes ( please use Dev++ compiler ) please send the full program and please compile...

C++ codes ( please use Dev++ compiler )

please send the full program and please compile before send

linked list program with the delete function (and traverse)

void delNode(node** start, int delNum)

if there is no match, print an error message otherwise, delete node

do not forget the test plan and code

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

#include <iostream>
#include <cstdlib>
using namespace std;

// A linked list node
struct Node
{
int data;
struct Node *next;
};

void printList(struct Node *node)
{
while (node != NULL)
{
cout << " " << node->data << " ";
node = node->next;
}
}

/* Given a reference (pointer to pointer) to the head of a list and a key,
deletes the first occurrence of key in linked list */

void delNodeLinkedList(struct Node **start, int delNum)
{
// Store head node
struct Node* temp = *start, *prev;
  
// If head node itself holds the delNum to be deleted
if (temp != NULL && temp->data == delNum)
{
*start = temp->next; // Changed head
free(temp); // free old head
return;
}
  
// Search for the delNum(key) to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->data != delNum)
{
prev = temp;
temp = temp->next;
}
  
// If delNum(key) was not present in linked list
if (temp == NULL)
   {
       cout << "Element not found in the linked list.\n\n";
       return;
   }
  
// Unlink the node from linked list
prev->next = temp->next;
  
free(temp); // Free memory
}

int main()
{
struct Node* start = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
struct Node* fourth = NULL;
struct Node* fifth = NULL;
  
// allocate 3 nodes in the heap   
start = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
fourth = (struct Node*)malloc(sizeof(struct Node));
fifth = (struct Node*)malloc(sizeof(struct Node));

start->data = 10; //assign data in first node
start->next = second; // Link first node with second

second->data = 20; //assign data to second node
second->next = third;   

third->data = 30; //assign data to third node
third->next = fourth;
  
fourth->data = 40;
fourth->next = fifth;
  
fifth->data = 50;
fifth->next = NULL;
  
cout << "List elements: \n";
printList(start);
  
cout << "\n\nDeleting node with value: 40\n";
delNodeLinkedList(&start, 11);
  
printList(start);
}

If there is anything that you do not understand, or need more help with then please mention it in the comments section.

Add a comment
Know the answer?
Add Answer to:
C++ codes ( please use Dev++ compiler ) please send the full program and please 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
  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include...

    BELOW IS THE CODE I ALREADY HAVE Linked List - Delete Modify Lab 5 and include the method to delete a node from the Linked List. In summary: 1) Add the method Delete 2) Method call to delete, with a number that IS in the list 3) Method call to delete, with a number that is NOT in the list - Be sure to include comments - Use meaningful identifier names (constants where appropriate) import java.io.*; 1/ Java program to...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C...

    CSCI 2010 Lab11 Link-Lists Lab 11A Linked-Lists Preparation Create a Visual Studio C++ Project C2010Lab11A Add the following to the project. //LinkedList.cpp #include <cstdlib> #include "LinkedList.h" using namespace std; //--------------------------------------------------- //List Element Members //--------------------------------------------------- ListElement::ListElement(int d, ListElement * n) {    datum=d;    next=n; } int ListElement::getDatum () const {    return datum; } ListElement const* ListElement::getNext () const {    return next; } //--------------------------------------------------- //LinkedList Members //--------------------------------------------------- LinkedList::LinkedList () {    head=NULL; } void LinkedList::insertItem(int item) {    ListElement *currPtr = head;    ListElement *prevPtr =...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • 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...

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