Question

In: Computer Science

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

  1. Add an element to the list, insertion position can be anywhere in the list (first, last and middle)
  2. 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++

Solutions

Expert Solution

#include<iostream>
using namespace std;
//node structure
class node
{
   public :
       int d;
       node *next;
   node(int dd)
   {
       d=dd;
       next=NULL;
   }  

};
//linked list
class single_linkedlist
{
   public:
   node *head,*tail;
   single_linkedlist()
   {
       head=tail=NULL;
   }
   //method to add element to list
   void add(int d)
   {
       if(head==NULL)
       {
           head = new node(d);
           tail=head;  
       }
       else
       {
           tail->next=new node(d);  
           tail=tail->next;
       }
   }
  
   //method to printlist
   void print()
   {
       node *temp=head;
       while(temp!=NULL)
       {
           cout<<temp->d<<"->";
           temp=temp->next;  
       }
       cout<<"\n";
   }
  
  

//recursive method to delete a node at given position
void delete_atpos(node *h,int pos)
{
   if(pos==0)
   {
       if(h==head)
       {
           head=head->next;
       }
      
       return;  
   }
   else
   {
       int i=0;
       node *cur,*prev=NULL;
       cur = h;
       while(i<pos)
       {
           prev=cur;
           cur=cur->next;
           i++;
       }
       if(cur!=NULL)  
       prev->next=cur->next;
       else
       prev->next=NULL;
      
   }
  
}
  
  
  
  
};


int main()
{
   single_linkedlist *d = new single_linkedlist();///creating object
   //creating linked list
   d->add(1);
   d->add(2);d->add(3);d->add(4);
   //testing method 3a
   cout<<"Current list :";
   d->print();


  
   single_linkedlist *dd = new single_linkedlist();///creating object
   //creating linked list
   dd->add(8);
   dd->add(2);dd->add(3);dd->add(1);
   dd->add(7);
   //testing method 3a
   cout<<"Current list :";
   dd->print();
   dd->delete_atpos(dd->head,0);//deleting at head
   dd->print();
  
   dd->delete_atpos(dd->head,3);//deleting at tail
   dd->print();
   dd->delete_atpos(dd->head,1);//deleting in middle
  
   dd->print();
  
   return 0;
}

output:

Current list :1->2->3->4->
Current list :8->2->3->1->7->
2->3->1->7->
2->3->1->
2->1->


Process exited normally.
Press any key to continue . . .


Related Solutions

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 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>...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
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)
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
C++ Write a program to create a linked list which stores the details of employees(Employee number,...
C++ Write a program to create a linked list which stores the details of employees(Employee number, employee name, rate, hours worked). Create a menu to manage the emoployee data. MENU 1. ADD EMPLOYEE DETAILS 2. DELETE EMPLOYEE 3. SEARCH EMPLOYEE 4. PRINT EMPLOYEE PAYROLL 5. EXIT When the user selected option #4 the program should print the following pay report for each employee: EmpNo.     Name      Rate    Hours    Regular Pay      Overtime Pay     Gross Pay Any hours worked above 40 hours are...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank account system (open account, deposit, withdraw, loans etc.). Requirements: 1. Use linked list (queues and/or stacks) 2. Classes 3. Arrays 4. Add, delete, remove, search methods (use Dev. C++ to create the program)
C++ program Complete the following functions for linked list. You are not allowed to alter the...
C++ program Complete the following functions for linked list. You are not allowed to alter the names or the function prototypes. #ifndef _ULL #define _ULL #include <iostream> #include "nodeType.h" using namespace std; void initializeList(nodeType *&head, nodeType *&tail, int&count); //Initialize the list to an empty state. //Postcondition: head = NULL, tail = NULL, count = 0; bool isEmptyList(const nodeType *head) ; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false. void print(const...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT