//LinkNode is a class for storing a single node of a linked list storing integer values. It has two public data fields for the data and the link to
//the next node in the list and has three constructors:
public class LinkNode {
public int data;
public LinkNode next;
// post: constructs a node with data 0 and null link
public ListNode() {
this(0, null);
}
// post: constructs a node with given data and null link
public LinkNode (int data) {
this(data, null);
}
// post: constructs a node with given data and given link
public LinkNode (int data, LinkNode next) {
this.data = data;
this.next = next;
}
}
Assume:
LinkNode list = new LinkNode( );
a.
+----+----+ +----+----+
list ----> | 1 | +----> | 2 | / |
+----+----+ +----+----+
List.next = new LinkNode(9);
Draw your diagram here…
b.
+----+----+ +----+----+
list ----> | 1 | +----> | 2 | / |
+----+----+ +----+----+
List.next = new LinkNode(9, list.next);
Draw your diagram here…
c.
+----+----+ +----+----+ +----+----+
list ----> | 7 | +----> | 8 | +----> | 3 | / |
+----+----+ +----+----+ +----+----+
List = new LinkNode(5, list.next.next);
Draw your diagram here…
In: Computer Science
compress(image)
Description:
It compresses a grayscale image and returns a data structure (list of lists) that holds a summary of the content, based on the following algorithm: First, we construct from the original image a black and white bitmap – values less than 128 are mapped to black and the rest are mapped to white. Then, we encode each row of this bitmap as a list containing the lengths of the alternating black and white sequences. Last, we package all these lists in an outer list. You can think of the encoding of each row as the process of counting the length of the consecutive black and white pixels (starting always with the black pixels) and adding these values in a list in the same order as we encounter the alternating black and white sequences. Obviously, each row, depending on its content, will have a different number of alternating sequences, so the full data structure will most probably be a list of variable-size sublists.
Parameters: image (2D list of int) is the grayscale image
Return value: A list of lists that summarizes the image
Example:
compress([[100,170,200,111,250,128],
[223,244,255,115,127,105],
[100,100,100,120,120,120]]) → [ [1, 2, 1, 2], [0, 3, 3], [6] ]
In: Computer Science
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working?
main.c Source Code:
#include
#include
#include "node.h"
int main()
{
struct mynode *head=NULL;
int value;
printf("Give first value: \n");
scanf("%d",&value);
printf("Give next values, and input 0 to end list: \n");
do{
if(value>0){
head = pushNode(head, value);
scanf("%d",&value);
}
}while (value>0);
printf("Before insertion sort: ");
printlist(head);
head=insertsort(head);
printf("After insertion sort: ");
printlist(head);
return 0;
}
Source Code node.c:
//defines all functions of myNode declared in node.h
#include
#include
#include "node.h"
void printlist (struct mynode *node)
{
while(node)
{
if(node->next)
printf("%d<==>", node->value);
else
printf("%d\n", node->value);
node=node->next;
}
return;
}
struct mynode* pushNode(struct mynode *node, int const
newValue)
{
struct mynode *newNode = (struct mynode*)malloc(sizeof(struct
mynode));
//assign the head in the list which will be returned every
time
struct mynode *list = node;
int *ptr = &newNode->value;
*ptr = newValue;
newNode->next = NULL;
if(node == NULL)
{
newNode->prev=NULL;
node=newNode;
list=node;
return list;
}
while(node->next != NULL)
{
node=node->next;
}
newNode->prev = node;
node->next = newNode;
return list;
}
struct mynode* insertsort(struct mynode *head)
{
//list variable to store the sorted list
struct mynode* sorted = NULL;
struct mynode* list = NULL;
struct mynode* current = head;
while (current != NULL) {
struct mynode* next = NULL;
next = current->next;
current->next = NULL;
current->prev = NULL;
if (sorted == NULL)
{
sorted = current;
list=sorted;
}
else if (sorted->value >= current->value)
{
current->next = sorted;
current->next->prev = current;
sorted = current;
list=sorted;
}
else
{
list=sorted;
struct mynode* newVal = NULL;
newVal = sorted;
//check where to enter the new value
while (newVal->next != NULL && newVal->next->value
< current->value)
{
newVal = newVal->next;
}
current->next = newVal->next;
if (newVal->next != NULL)
current->next->prev = current;
newVal->next = current;
current->prev = newVal;
}
current = next;
}
//return the sorted list
return list;
}
Source Code node.h:
//declares the data structure and the functions of said data
structure
#ifndef NODE_H_
#define NODE_H_
struct mynode {
int const value;
struct mynode *next;
struct mynode *prev;
};
struct mynode * insertsort(struct mynode *head);
void printlist (struct mynode *node);
struct mynode* pushNode(struct mynode *node, int const
newValue);
#endif
In: Computer Science
C++ ONLY!
Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator.
#include <iostream>
#include <cstddef>
#include <string>
using Item = std::string;
class List {
private:
class ListNode {
public:
Item item;
ListNode * next;
ListNode(Item i, ListNode *n=nullptr) {
item = i;
next = n;
}
};
ListNode * head;
ListNode * tail;
public:
class iterator {
ListNode *node;
public:
iterator(ListNode *n = nullptr) {
node = n;
}
Item& getItem() { return node->item; }
void next() { node = node->next; }
bool end() { return node==nullptr; }
friend class List;
};
public:
List() {
// list is empty
head = nullptr;
tail = nullptr;
}
bool empty() {
return head==nullptr;
}
// Only declared, here, implemented
// in List.cpp
void append(Item a);
bool remove (Item ©);
void insertAfter(iterator, Item);
void removeAfter(iterator, Item&);
iterator begin() {
return iterator(head);
}
iterator find(Item);
};
void List::append(Item a) {
ListNode *node = new ListNode(a);
if ( head == nullptr ) {
// empty list
head = node;
tail = node;
} else {
tail->next = node;
tail = node;
}
}
bool List::remove(Item ©)
{
if (!empty()) {
copy = head->item;
ListNode *tmp = head->next;
delete head;
head = tmp;
if (head==nullptr)
tail = nullptr;
return true;
}
return false;
}
void List::insertAfter(iterator it, Item item)
{
if (it.node == nullptr)
{
// insert at head
ListNode *tmp = new ListNode(item,head);
// if list is empty, set tail to new node
if (tail==nullptr) {
tail = tmp;
}
// set head to new node
head = tmp;
}
else
{
ListNode *tmp = new
ListNode(item,it.node->next);
it.node->next = tmp;
// could be a new tail, if so update tail
if (tail==it.node) {
tail = tmp;
}
}
}
void List::removeAfter(iterator it, Item& item)
{
// emtpy list or at tail, just return
if (it.node == tail) return;
if (it.node == nullptr)
{
ListNode * tmp = head;
head = head->next;
delete tmp;
if (head==nullptr)
tail = nullptr;
}
else
{
ListNode *tmp = it.node->next;
it.node->next = tmp->next;
delete tmp;
// could be that it.node is the new nullptr
if (it.node->next == nullptr)
tail = it.node;
}
}
List::iterator List::find(Item item)
{
//YOUR CODE HERE
return iterator();
}
int main()
{
List l;
l.append("one");
l.append("two");
l.append("three");
l.append("four");
auto it = l.find("one");
if (!it.end())
std::cout << "Should be one: " << it.getItem() <<
std::endl;
else
std::cout << "Iterator should not have been null." <<
std::endl;
auto no_it = l.find("zero");
if (no_it.end())
std::cout << "As expected, zero not found." <<
std::endl;
else
std::cout << "Oops! should not have found zero." <<
std::endl;
return 0;
}
In: Computer Science
In: Math
Create an HTML page that contains a three-level nesting list (you may choose the type of the list you want to use). One item on each level. Use the three attached images as the content of each of the three list items.
use any random image name located in the default directory
In: Computer Science
Discussion 1 : Part 2 of 2 : Work in Groups and come up with a simple survey for Voice of customer on a new Pho-Let (Phone & Tablet combined) as to what they think the features and requirements should be:
1.List the Requirements
2.List the Critical to Quality elements
3.List the Critical to Satisfaction elements
In: Operations Management
1) What is meant by Factoring of Accounts Receivables?
2) List 3 advantages of Factoring
3) List 3 disadvantages of Factoring
4) List 3 Factors in the USA.
5) If you are a company that factors your receivables, would you prefer "recourse" or "non-recourse" factoring? Explain your choice.
In: Accounting
Javascript problem:
Given an argument of a list of book objects:
Example test case input:
{
"title" : "Book A",
"pages": "50",
"next": {
"title": "Book B",
"pages": 20,
"next": null
}
}
create a recursive function to return the total number total # of pages read in the list. If there are no books in the list, return 0.
In: Computer Science
|
int main() { int ListDataSample1[] = { 1, 1, 1 }; int ListDataSample2[] = { 2, 2, 2 }; List<int> List1 = List<int>(ListDataSample2, 3); List<int> List2 = List<int>(ListDataSample2, 3);
cout << "List1 :" << List1 << endl; cout << "List2 :" << List2 << endl << endl; List1 += List2;
cout << "List1 :" << List1 << endl; cout << "List2 :" << List2 << endl << endl; system("pause"); return 0; } |
In: Computer Science