Question

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 from the beginning to the end), although it can be.

append_node: appends a new node (created using the new statement) to the end of the linked list

insert_node: inserts a new node(created using the new statement)at the specified location in the list

delete_node: deletes a node from the specified location in the list

search_node: searches the list for a value

destroy_list: At the end of the program, this function loops through the entire list and destroys the list by using the delete statement on each dynamically allocated linked list node (to avoid a memory leak).

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

#include<bits/stdc++.h>
using namespace std;

struct Node
{
   int value;
   Node *next;
};

Node* append_node(struct Node *head, int new_data)
{
  
   Node *nn = new Node();
   nn->value = new_data;
   nn->next = NULL;
   if(head==NULL) //if it is the first node, then head becomes newnode...
   {
       head = nn;
   }
   else //otherwise traverse till last and add this newnode...
   {
       Node *temp = head;
       while(temp->next != NULL)
       {
           temp = temp->next;
       }
       temp->next = nn;
   }
   return head;
}


void insert_node(struct Node *head, int new_data, int position)
{
   Node *nn = new Node;
   nn->value = new_data;
   nn->next = NULL;
  
   Node *temp = head;
  
   if(position == 1) //if it is in starting, then link with head and make this as new head....
   {
       nn->next=temp;
       head=nn;
   }
   else //otherwise loop till position, then insert...
   {
       for(int i=1;i<position-1;i++)
       {
           temp = temp->next;
       }
      
       nn->next=temp->next;
       temp->next=nn;
   }
}

void delete_node(struct Node *head, int position)
{
   Node *temp = head;
  
   if(position == 1) //change head to next node, and free the old head...
   {
       head=temp->next;
       free(temp);
   }
   else //otherwise traverse till position...
   {
       for(int i=1;i<position-1;i++)
       {
           temp = temp->next;
       }
      
       Node *next = temp->next->next; //storing the next node of deleting node in *next...

   free(temp->next); // deleting current node...
  
   temp->next = next; // attaching next to the previous node...
   }  
  
}

void search_node(Node *head, int data)
{
   //cout<<"hai";
   Node *temp=head;
   int i = 1, flag = 0;
  
   while(temp != NULL)
   {
       if(temp->value == data)
       {
           cout<<"Found at position "<<i<<endl;
           flag = 1;
           break;
       }
       i++;
       temp=temp->next;
   }
   if(flag==0)
   cout<<"Not Found"<<endl;
}

void destroy_list(Node* head)
{
while(head != NULL)
{
Node *temp = head;
head = temp->next;
delete temp;
}
cout<<"Destroyed Successfully\n";
}
void printList(struct Node* head)
{
while (head != NULL) {
cout << " " << head->value;
head = head->next;
}
cout << endl;
}

int main()
{
   struct Node* head = NULL;
  
   //appending nodes....
   head=append_node(head, 7);
head=append_node(head, 1);
head=append_node(head, 3);
head=append_node(head, 2);
head=append_node(head, 8);
  
printList(head); //printing List after appending...
  

insert_node(head,9,2); //inserting node at second position...
printList(head); //print..
  
  
insert_node(head,97,4); //inserting node at fourth position...
printList(head); //print..
  
  
delete_node(head,5); //deleting node at fifth position...
printList(head); //print..
  
  
search_node(head,3); //searching node with value 3...
  
  
search_node(head,97); //searching node with value 97...
  
destroy_list(head); //Destroying list...
  
printList(head); // prints random numbers Because list is already destroyed....
}

---------------------------------------------------------------------------------------------------------------------Output: 7 1 3 28 791328 9 1 97 3 2 8 7 9 1 97 2 8 Not Found Found at position 4 Destroyed Successfully 34471600 34471536 3447

----------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...
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 a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

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

  • Using the program segment and sample txt file below, write a program that contains a linked...

    Using the program segment and sample txt file below, write a program that contains a linked list of students. The program should allow the user to: 1. initialize list of students 2. add additional student to front of list 3. add additional student to rear of list 4. delete student 5. sort students alphabetically 6. sort students by idNum 7. show number of students in list 8. print students 9. quit program XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX The program should be divided into the...

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

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

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

  • C++ assignment about doubly linked list

    You are required to write the following functions using this class: class Doubly_linked_list // Use a class Doubly_linked_list to represent an object {     public:     // constructor initialize the nextPtr     Doubly_linked_list()     {         prevPtr = 0; // point to null at the beginning         nextPtr = 0; // point to null at the beginning     }     // get a number     int GetNum()     {         return number;     }     // set a number     void SetNum(int num)     {         number = num;     }     // get the prev pointer     Doubly_linked_list ...

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