Question
In C++ syntax please
Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;
   struct node{
           int data;
           struct node*next;
       };

void append(struct node*&head,int data){
           struct node*newNode = new node;
           newNode->data = data;
           newNode->next=NULL;
          
           if(!head){
               head = newNode;
               return;
           }
           struct node*current = head;
          
           while(current->next)current=current->next;
           current->next = newNode;
       }
      
       bool find(struct node*head,int data){
           struct node*current = head;
           while(current){
               if(current->data==data)return true;
               current=current->next;
           }
           return false;
       }
       void clear(struct node*&head){
           struct node* current = head;
   struct node* next;
  
           while (current != NULL)
           {
       next = current->next;
       delete(current);
       current = next;
           }
           head = NULL;
       }
       void print(struct node*head){
           struct node*current = head;
           while(current){
               cout<<current->data<<" ";
               current=current->next;
           }
           cout<<"\n\n";
       }
       void remove(struct node*&head,int data){
           if(!find(head,data)){
               cout<<data<<" not in the linklist list\n";
               return ;
           }
           struct node*prev = head,*current=head;
           if(head->data==data){
               head=head->next;
               delete(current);
               return;
           }
           while(current&&current->data!=data){
               prev = current;
               current = current->next;
           }
           prev->next = current->next;
           delete(current);
       }

int main(){
   struct node*head=NULL;
   append(head,42);
   append(head,68);
   append(head,35);
   append(head,1);
   append(head,70);
  
   print(head);
  
   int num=68;
   if(find(head,num))cout<<"Found "<<num<<" int the list!\n";
   else cout<<"Not found "<<num<<" int the list!\n";
  
   remove(head,35);
   print(head);
   return 0;
}

//sample output

CAUsers\IshuManish\ Documents appendlist.exe 2 68 35 1 7D ound 68 int the list! 42 68 1 70 Process exited after 0.1952 second

Add a comment
Know the answer?
Add Answer to:
In C++ syntax please Write a program that implements and demonstrates a linked list using functions....
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
  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

  • Write a C++ function to add a node to the beginning of a linked list. Your...

    Write a C++ function to add a node to the beginning of a linked list. Your function takes two arguments - the head of the linked list and the value num to be added. Note that the list may be empty! Your function should modify the head of the linked list to point to the new node, and set the new node to point to the rest of the list (if not empty). Example: Initial Array: 4->2->3, key = 5...

  • Write in C++ please: Write a class named MyVector using the following UML diagram and class...

    Write in C++ please: Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...

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

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

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

  • Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete...

    Exercise: Delete First Element from a Linked List (pair) This is a pair exercise to complete with your lab partner. Download list_delete_first.c here, or copy it to your CSE account using the following command: cp -n /web/dp1091/20T1/activities/list_delete_first/list_delete_first.c . Your task is to add code to this function in list_delete_first.c: // // Delete the first node in list. // The deleted node is freed. // The head of the list is returned. // struct node *delete_first(struct node *head) { // PUT...

  • Derive a class called Stack from the linked list described in Assignment 2 (list of Dates)....

    Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

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