Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSE 240 – Assignment 7 Maximum points: 50 pts Topics: ? C/C++ Syntax ? Pointers

ID: 3701313 • Letter: C

Question

CSE 240 – Assignment 7

Maximum points: 50 pts

Topics:

? C/C++ Syntax

? Pointers

? Functions

? Dynamic Allocation of Memory

? Data Structures: Linked Lists

? Object Orientation

Linked List Specifications:

You are to create a Linked List data structure from scratch. This linked list should be Templated so that any data could be stored within it.

For a maximum of B on specifications – you can make the linked list only store Integers.

The primary goal of this assignment is to make a Linked List that you can use and re-use. The Linked List itself is the majority of the specifications grade.

Specifications Scoring Breakdown:

Templated Linked List + Josephus Problem – 100% of specifications Templated Linked List only – 80% of specifications
Integer Linked List + Josephus Problem – 80% of specifications Integer Linked List only – 70% of specifications

BIG GIANT NOTE – TEMPLATES AND FILES

When you use a templated type in C++ ALL templated code must be done in the .h file. This means that ALL of your methods will be defined in the .h file as well as your class.

You should still forward declare Classes above then Methods below.

Your .h file should have your LinkedList class and your Node class and the method definitions for both. Remember to use your :: operator correctly.

You will create a Linked List Class and a Node Class/Struct
The Linked List should contain the following methods in its public interface:

? Constructor

? Destructor

? AddToFront(T data) – create a node containing T data and add it to the front of the

list

? AddToEnd(T data) – create a node containing T data and add it to eh end of the list

? AddAtIndex(T data, int index) – create a node containing T data and add it to the

list at index, return boolean for success or failure (optional: you could also

return an integer with failure codes since this method can fail multiple ways)

? NextNode – Move the current pointer to the next node, wraps to front if it

? InsertAfterCurrent(T data) – Create a node containing T data and insert it after

? RemoveCurrent() – Delete the current item and return its contents

? RemoveAtIndex(int index) – delete the index # node in the list and return its

contents

? RemoveFromFront() – Delete first item and return its contents

? RemoveFromEnd() – Delete last item and return its contents

? RemoveFirst(T data) – find first instance of T data and remove it

? RemoveAll(T data) – find each instance of T data and remove it

? ElementExists(T data) – Returns a T/F if element exists in list

? Find(T data) – Look for data in the list, return a pointer to its node

? IndexOf(T data) – returns an index of the item in the list (zero-based)

? RetrieveFront – returns the data contained in the first node, does not delete it

? RetrieveEnd – returns the data contained in the last node, does not delete it

? Retrieve(int index) – returns the data contained in node # index, does not delete

it, returns null if index is out of bounds or data does not exist

? ToArray – Create an array from the contents of the list and return it

? Empty – Empty out the list, delete everything

? Length – How many elements are in the list

More methods private or public should be created as needed to facilitate the functionality of the Interface methods. If you feel your list needs more functionality, feel free to create it.

Node Class

? Constructor

? Destructor

? Getters & Setters

The node class should be fairly rudimentary. It should be templated so you can store anything in it.

Josephus Problem - Linked Lists

Description:

"There are people standing in a circle waiting to be executed. After the first man is executed, certain number of people are skipped and one man is executed. Then again, people are skipped and a man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.

The task is to choose the place in the initial circle so that you survive (are the last one remaining)."

-- Wikipedia, http://en.wikipedia.org/wiki/Josephus_problem
Assume that the number of people, P, in the circle may be any number between zero

Assume that every Nth person around the circle is killed each turn, where N is an integer between one and twenty.

Specifications:

? Create an application in C++ that uses a linked list to represent the circle of people, numbered from 1 to P.

? Acquire the values P and N from the user at runtime via console input.

? Output the the individual that survives the mass execution.

Flash Version

? http://webspace.ship.edu/deensley/flash/JosephusProblem.html
There are several Java applet versions of this problem to check your work as you

debug.

? http://www.wou.edu/~burtonl/josephus.html
o This applet lets you choose how many people are in the circle and

Grading of Programming Assignment

The TA will grade your program following these steps:
(1) Compile the code. If it does not compile, If it does not compile you will

receive a U on the Specifications in the Rubric.

(2) The TA will read your program and give points based on the points allocated to each component, the readability of your code (organization of the code and comments), logic, inclusion of the required functions, and correctness of the implementations of each function.

Rubric:

What to Submit?

You are required to submit your solutions in a compressed format (.zip). Zip all files into a single zip file. Make sure your compressed file is labeled correctly - lastname_firstname7.zip.

For this home assignment, the compressed file MUST contain the following:

? Makefile

? README.txt – instructions for using the makefile

? lastname_firstname7.cpp – where you main/josephus problem is

? lastname_linkedList.h – where your linked list/node code is

? any other code/library files you create or use for the sake of the

assignment
No other files should be in the compressed folder.

If multiple submissions are made, the most recent submission will be graded, even if the assignment is submitted late.

Explanation / Answer

here is your program : --------->>>>>>>>>

#ifndef __LINKED_LIST_H__
#define __LINKED_LIST_H__
#include<iostream>

using namespace std;

template<class T>
class Node{
T data;
Node<T> *next;

public:
  Node(T d,Node<T> *nex = NULL){
   data = d;
   next = nex;
  }
  void setData(T d){
   data = d;
  }
  void setNext(Node<T> *nex){
   next = nex;
  }
  
  T getData()const{
   return data;
  }
  Node<T>* getNext()const{
   return next;
  }
};


template<class T>
class LinkedList{
Node<T> *head;
Node<T> *cur_;
int size_;

public:
  LinkedList(){
   head = NULL;
   cur_ = NULL;
   size_ = 0;
  }
  ~LinkedList(){ empty(); }
  void AddToFront(T data);
  void AddAtIndex(T data,int index);
  void NextNode();
  void InsertAfterCurrent(T data);
  T RemoveCurrent();
  T RemoveAtIndex(int index);
  T RemoveFromFront();
  T RemoveFromEnd();
  bool RemoveFirst(T data);
  void RemoveAll(T data);
  bool ElementExists(T data);
  Node<T>* Find(T data);
  int IndexOf(T data)const;
  T RetreiveCurrent()const;
  T RetreiveFront()const;
  T RetreiveEnd()const;
  T Retreive(int index)const;
  T* ToArray();
  void First();
  void empty();
  int Length()const;
  bool isEmpty();
  friend ostream& operator<<(ostream &out,const LinkedList &oth){
   oth.First();
   out<<"[ ";
   for(int i = 0;i<oth.Length();i++){
    out<<oth.RetreiveCurrent();
    if(i != oth.Length()-1){
     out<<", ";
    }
    oth.NextNode();
   }
   out<<" ]";
   return out;
  }
};

template<class T>
void LinkedList<T>::First(){
cur_ = head;
}

template<class T>
bool LinkedList<T>::isEmpty(){
return size_ == 0;
}

template<class T>
int LinkedList<T>::Length()const{
return size_;
}

template<class T>
void LinkedList<T>::empty(){
int s = size_;
for(int i = 0;i<s;i++){
  RemoveFromFront();
}
}

template<class T>
T* LinkedList<T>::ToArray(){
T *temp = new T[size_];
Node<T> *cur_t = cur_;
First();
for(int i = 0;i<size_;i++){
  temp[i] = RetreiveCurrent();
  NextNode();
}
cur_ = cur_t;
return temp;
}

template<class T>
T LinkedList<T>::Retreive(int index)const{
if(index < 0 || index >= size_){
  return (T)NULL;
}
Node<T> *temp = head;
for(int i = 0;i<index;i++){
  temp = temp.getNext();
}

return temp.getData();
}

template<class T>
T LinkedList<T>::RetreiveEnd()const{
Node<T> *temp = head;
while(temp.getNext() != NULL){
  temp = temp.getNext();
}

return temp.getData();
}

template<class T>
T LinkedList<T>::RetreiveFront()const{
return head.getData();
}

template<class T>
T LinkedList<T>::RetreiveCurrent()const{
if(cur_ == NULL){
  First();
}

return cur_.getData();
}

template<class T>
Node<T>* LinkedList<T>::Find(T data){
Node<T> *temp = head;
while(temp != NULL){
  if(temp.getData() == data){
   return temp;
  }
  temp = temp.getNext();
}

return NULL;
}

template<class T>
bool LinkedList<T>::ElementExists(T data){
if(Find(data) == NULL){
  return false;
}
return true;
}

template<class T>
void LinkedList<T>::RemoveAll(T data){
while(RemoveFirst(data));
}

template<class T>
bool LinkedList<T>::RemoveFirst(T data){
Node<T> *temp = head;
Node<T> *prev = NULL;
while(temp != NULL){
  if(temp.getData() == data){
   if(prev == NULL){
    head = head.getNext();
    size_--;
    delete temp;
    return true;
   }
   prev.setNext(temp.getNext());
   size_--;
   delete temp;
   return true;
  }
  prev = temp;
  temp = temp.getNext();
}
return false;
}

template<class T>
T LinkedList<T>::RemoveFromFront(){
if(isEmpty()){
  return (T)NULL;
}
Node<T> *temp = head;
head = head.getNext();
size_--;
T tt = temp.getData();
delete temp;
return tt;
}

template<class T>
T LinkedList<T>::RemoveFromEnd(){
if(isEmpty()){
  return (T)NULL;
}
Node<T> *temp = head;
Node<T> *prev = NULL;
while(temp.getNext() != NULL){
  prev = temp;
  temp = temp.getNext();
}

if(prev == NULL){
  head = NULL;
  size_ = 0;
}else{
  prev.setNext(NULL);
  size_--;
}
T tt = temp.getData();
delete temp;
return tt;
}

template<class T>
T LinkedList<T>::RemoveAtIndex(int index){
if(isEmpty()){
  return (T)NULL;
}
if(index < 0 || index >= size_){
  return (T)NULL;
}

Node<T> *temp = head;
Node<T> *prev = NULL;
for(int i = 0;i<index;i++){
  prev = temp;
  temp = temp.getNext();
}

if(prev == NULL){
  head = NULL;
  size_ = 0;
}else{
  prev.setNext(NULL);
  size_--;
}
T tt = temp.getData();
delete temp;
return tt;
}

template<class T>
T LinkedList<T>::RemoveCurrent(){
if(isEmpty()){
  return (T)NULL;
}
if(cur_ == NULL){
  return (T)NULL;
}
Node<T> *temp = head;
Node<T> *prev = NULL;
while(temp != cur_){
  prev = temp;
  temp = temp.getNext();
}

if(prev == NULL){
  head = NULL;
  size_ = 0;
}else{
  prev.setNext(NULL);
  size_--;
}
T tt = temp.getData();
delete temp;
return tt;
}

template<class T>
void LinkedList<T>::InsertAfterCurrent(T data){
if(cur_ == NULL){
  return;
}
Node<T> *temp = new Node<T>;
temp.setData(data);
temp.setNext(cur_.getNext());
cur_.setNext(temp);
size_++;
}

template<class T>
void LinkedList<T>::AddToFront(T data){
Node<T> *temp = new Node<T>;
temp.setData(data);
if(head == NULL){
  head = temp;
  temp.setNext(NULL);
  cur_ = head;
}else{
  temp.setNext(head);
  head = temp;
}
size_++;
}

template<class T>
void LinkedList<T>::AddAtIndex(T data,int index){
if(isEmpty() && index != 0){
  return;
}
if(index < 0 || index >= size_){
  return;
}
if(index == 0){
  AddToFront(data);
  return;
}
Node<T> *temp = new Node<T>;
temp.setData(data);
Node<T> *tt = head;
for(int i = 0;i<index;i++){
  tt = tt.getNext();
}
temp.setNext(tt.getNext());
tt.setNext(temp);
size_++;
}
template<class T>
void LinkedList<T>::NextNode(){
if(cur_ == NULL){
  First();
  return;
}
if(cur_.getNext() != NULL){
  cur_ = cur_.getNext();
  return;
}
First();
}
#endif