linked-list

How to have List Iterator start at a given index?

强颜欢笑 提交于 2020-06-17 09:00:27
问题 I have a linked list and I need to make method that returns an iterator at a given point in the list. I currently have an iterator that starts at the head: public Iterator<E> iterator( ) { return new ListIterator(); } All I have for the other one is: public Iterator<E> iterator(int x ) { return new ListIterator(); } I'm not sure how to go about utilizing the given position(x) that won't affect my ListIterator constructor which starts at head. I tried using a for loop to get to "x" but

Missed scanf and function goes on without it. If I add a space still doesn't work

纵然是瞬间 提交于 2020-06-01 05:09:02
问题 #include <stdio.h> struct mychar { char value; struct mychar *nextPtr; }; typedef struct mychar Mychar; void instructions(); void append(Mychar **, char ); void printlist(Mychar *); int main(){ instructions(); Mychar *startPtr = NULL; unsigned int choice; char newchar; do { scanf("%d",&choice); switch (choice) { case 1: printf("\nWrite the character you want to add."); printf("\n> "); scanf(" %c", &newchar); append(&startPtr, newchar); printlist(startPtr); break; case 2: break; default:

Missed scanf and function goes on without it. If I add a space still doesn't work

那年仲夏 提交于 2020-06-01 05:07:46
问题 #include <stdio.h> struct mychar { char value; struct mychar *nextPtr; }; typedef struct mychar Mychar; void instructions(); void append(Mychar **, char ); void printlist(Mychar *); int main(){ instructions(); Mychar *startPtr = NULL; unsigned int choice; char newchar; do { scanf("%d",&choice); switch (choice) { case 1: printf("\nWrite the character you want to add."); printf("\n> "); scanf(" %c", &newchar); append(&startPtr, newchar); printlist(startPtr); break; case 2: break; default:

Missed scanf and function goes on without it. If I add a space still doesn't work

末鹿安然 提交于 2020-06-01 05:07:14
问题 #include <stdio.h> struct mychar { char value; struct mychar *nextPtr; }; typedef struct mychar Mychar; void instructions(); void append(Mychar **, char ); void printlist(Mychar *); int main(){ instructions(); Mychar *startPtr = NULL; unsigned int choice; char newchar; do { scanf("%d",&choice); switch (choice) { case 1: printf("\nWrite the character you want to add."); printf("\n> "); scanf(" %c", &newchar); append(&startPtr, newchar); printlist(startPtr); break; case 2: break; default:

Why isn't “new” used while making the current variable in the linkedlist?

我是研究僧i 提交于 2020-05-31 04:49:21
问题 This is the solution to printing elements of a linked list. Why isn't it Node *current = new Node; and then current = head; ? void printLinkedList(Node* head) { Node *current = head; while(current!=NULL){ cout << current -> data << endl; current = current -> next; } } 回答1: This is a great spot to draw pictures! Imagine we have a linked list pointed at by head : head | v +------+ +-----+ +-----+ +-----+ | i'm | -> | the | -> | bad | -> | guy | -> null +------+ +-----+ +-----+ +-----+ If we use

Hashing of small dictionary

ⅰ亾dé卋堺 提交于 2020-05-31 03:46:26
问题 I want to hash small dictionary ("dictionaries/small"). Main file compiles correctly, but at runtime it produces "Segmentation fault" message with function insert() (specifically something wrong with malloc() , but I don`t know what). HASH.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <ctype.h> #include <string.h> typedef struct node { char* name; struct node* next; } node; node* first[26] = {NULL}; int hash(const char* buffer) { return tolower(buffer[0]) - 'a'; }

Hashing of small dictionary

若如初见. 提交于 2020-05-31 03:46:08
问题 I want to hash small dictionary ("dictionaries/small"). Main file compiles correctly, but at runtime it produces "Segmentation fault" message with function insert() (specifically something wrong with malloc() , but I don`t know what). HASH.c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <ctype.h> #include <string.h> typedef struct node { char* name; struct node* next; } node; node* first[26] = {NULL}; int hash(const char* buffer) { return tolower(buffer[0]) - 'a'; }

sorting a linked list containing strings

青春壹個敷衍的年華 提交于 2020-05-30 13:11:49
问题 So what I want to do is to sort an linked list containing only strings. To do so, I have 2 options. Option 1 - dynamically allocate an array with the same size as the linked list and the strings containing it also with the same size, copy the contents of the linked list into the array and sort it using qsort . Option 2 - implement a merge sort algorithm in order to sort it. One of the problems is will it cost more memory and time if I do option 2 over option 1 or the option is the better? My

Store the address of previous node into the “prev” section of current node

做~自己de王妃 提交于 2020-05-28 06:42:56
问题 First, I defined my node using a struct called listrec. So each node has 3 parts: prev (used to store the address of the previous node), value (used to store the value), and next (used to store the address of next node). #include <iostream> using namespace std; struct listrec { struct listrec *prev; float value; struct listrec *next; }; listrec *head, *tail; Then, I used a loop to initialize the linked list (based on the number of nodes requested by the user. for (float i = 0; i < number; i++

Store the address of previous node into the “prev” section of current node

杀马特。学长 韩版系。学妹 提交于 2020-05-28 06:42:54
问题 First, I defined my node using a struct called listrec. So each node has 3 parts: prev (used to store the address of the previous node), value (used to store the value), and next (used to store the address of next node). #include <iostream> using namespace std; struct listrec { struct listrec *prev; float value; struct listrec *next; }; listrec *head, *tail; Then, I used a loop to initialize the linked list (based on the number of nodes requested by the user. for (float i = 0; i < number; i++