Question

C++ void CLL::push(char data) { // You write - if the size is 0, add a...

C++

void CLL::push(char data) { // You write - if the size is 0, add a first node in the linked list

// by calling addFirst. Otherwise add a new node to the end of the linked list. Note that this

// linked list is circular, so you must make the last node's next pointer point to the first node.

}

void CLL::addFirst(char data) {

// you write - add the very first node to the linked list

}

void CLL::addAtFront(char data) {

// you write - if the size of the linked list is 0, add a first node by calling addFirst.

//Otherwise add a new node to the beginning of the list

//Since this linked list is circular, you must make the last node now point to this new node

//you just added to the front of the linked list

}

void CLL::removeNext(SNode *n) {

// Longest method - given node n, remove the next node in the linked list.

// you have 4 conditions:

//1: there's only one node in the list, in which case you delete that node and set the size to 0

//2: n's next node is a last node, in which case n becomes the new last node and n's next must point

//               to the first node in the list (remember to decrease the size of the list)

//3: n's next node is the first node in the list (i.e., n is the last node in the list), in which case

//               n's next becomes first's next, first is deleted, and first becomes n's next (again, remember

//               to decrease the size)

//4: n is just some node in the middle of the list, in which case you should create a tmp node to point

//               to n's next, then set n's next to n's next's next, and then delete the tmp node (again, decrease

//               size).

}

void CLL::printList(){

// print out the data in each node in the linked list, starting at the first node.

}

You will also be modifying the file DuckDuckGoose.cpp to fill in the methods as defined below:

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

If you have any doubts, please give me comment...

CLL.h

struct SNode{

char data;

SNode *next;

};

#ifndef CLL_H

#define CLL_H

class CLL{

public:

CLL(){

head = NULL;

size = 0;

}

void push(char data);

void addFirst(char data);

void addAtFront(char data);

void removeNext(SNode *n);

void printList();

private:

SNode *head;

int size;

};

#endif

CLL.cpp

#include "CLL.h"

void CLL::push(char data){

if(size==0){

addFirst(data);

}

else{

SNode *n = new SNode();

n->data = data;

n->next = head;

SNode *temp = head;

int i=0;

while(i<size-1){

temp = temp->next;

i++;

}

temp->next = n;

size++;

}

}

void CLL::addFirst(char data){

SNode *n = new SNode();

n->data = data;

n->next = NULL;

head = n;

size++;

}

void CLL::addAtFront(char data){

if(size==0){

addFirst(data);

}

else{

SNode *n = new SNode();

n->data = data;

n->next = head;

head = n;

size++;

}

}

void CLL::removeNext(SNode *n){

if(size==1){

delete head;

head = NULL;

}

else{

SNode *temp = head;

int i=0;

while(temp!=n){

temp = temp->next;

i++;

}

delete temp->next;

temp->next = head;

size--;

}

}

void CLL::printList(){

int i=0;

SNode *temp = head;

while(i<size){

cout<<temp->data<<" ";

temp = temp->next;

i++;

}

cout<<endl;

}

You didn't mentioned file DuckDuckGoose.cpp file.... so I mentioned my file

main.cpp

#include<iostream>

using namespace std;

#include "CLL.cpp"

int main(){

CLL *list = new CLL();

list->push('A');

list->addAtFront('B');

list->push('C');

list->push('D');

list->printList();

return 0;

}

[anunagaoanunaga 16042018]$ gt+ main.cpp [anunaga@anunaga 16042018]$ ./a.out BACD

Add a comment
Know the answer?
Add Answer to:
C++ void CLL::push(char data) { // You write - if the size is 0, add 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
  • 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,...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

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

  • In the first exercise, you will override the add method in a subclass in order to...

    In the first exercise, you will override the add method in a subclass in order to improve its performance. In the second exercise, you will implement an Iterator for your new linked list class. Exercise 1 The add method of the linked list class discussed during lecture performs in O(N) time. What can be done to improve its performance to O(1)? What are the boundary cases? Define a new class that inherits from the CS20bLinkedList class introduced during the lecture....

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

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

  • c++ please Change InsertLast to be used on a double linked list instead of a single...

    c++ please Change InsertLast to be used on a double linked list instead of a single linked list. Use prev as the other pointer name. bool LinkedList::InsertLast(int v) { if(size == 0) { return InsertFirst(v); } Node *tmp = new Node; if(tmp == NULL) return false; tmp->value=v; last->next=tmp; last = tmp; size++; if(size == 1){ first = last; } return true; }

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

  • PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data...

    PLEASE WRITE IN JAVA AND ADD COMMENTS TO EXPLAIN write a class NameCard.java which has •Data fields: name (String), age(int), company(String) •Methods: •Public String getName(); •Public int getAge(); •Public String getCom(); •Public void setName(String n); •Public void setAge(int a); •Public void setCom(String c); •toString(): \\ can be used to output information on this name card 2, write a class DoublyNameCardList.java, which is a doubly linked list and each node of the list a name card. In the DoublyNameCardList.java: •Data field:...

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