linked-list

Maximum Size List in Java

核能气质少年 提交于 2019-12-03 23:56:26
It's useful to me to have a data structure in Java that has all the functionality of a List, but has a maximum storage capacity, and drops older data when newer data is added. Conceivably at some point I might want to implement a fixed size Queue which keeps a more general ordering of the data, and drops the old data lowest in that ordering, but that's the for the future. At the moment I'm implementing it like this: public class FixedSizeList<T> { private final int maxSize; private final LinkedList<T> list = new LinkedList<T>(); public FixedSizeQueue(int maxSize) { this.maxSize = maxSize < 0 ?

Extract middle element from a single linked list [closed]

ⅰ亾dé卋堺 提交于 2019-12-03 23:20:23
I have a question: Find the middle element from a single linked list. I need to know the way/method of this problem. You can use two pointers to iterate through the list - one which iterates twice as fast as the other. When the fast pointer reaches the end of the list then the slow pointer will be pointing at the mid-point. Algorithm: init slow_pointer = head init fast_pointer = head repeat fast_pointer = fast_pointer->next; if fast_pointer == NULL break; fast_pointer = fast_pointer->next; if fast_pointer == NULL break; slow_pointer = slow_pointer->next; until false // slow_pointer now points

Invert linear linked list

余生颓废 提交于 2019-12-03 22:45:30
a linear linked list is a set of nodes. This is how a node is defined (to keep it easy we do not distinguish between node an list): class Node{ Object data; Node link; public Node(Object pData, Node pLink){ this.data = pData; this.link = pLink; } public String toString(){ if(this.link != null){ return this.data.toString() + this.link.toString(); }else{ return this.data.toString() ; } } public void inc(){ this.data = new Integer((Integer)this.data + 1); } public void lappend(Node list){ Node child = this.link; while(child != null){ child = child.link; } child.link = list; } public Node copy(){

Bubble sort in c linked list [closed]

前提是你 提交于 2019-12-03 22:00:20
I need to do is read in an input file into a linked list. Part of the file is: NameA, 25 NameB, 33 NameC, 23 NameD, 39 And after i need to sort by the number (bubble sort) and write it to another file. Here is what i have: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node{ char name[20]; int number; struct node *next; struct node *prev; }*head; int main(void) { struct node *temp; temp = malloc(sizeof(struct node)); temp->next = NULL; head = temp; FILE *ifp; char fnamer[100] = ""; char line[128]; // printf("\n\nPlease Enter the Full Path of the file: \n"); // scanf("%s",

Sorting a linked list in c++

别说谁变了你拦得住时间么 提交于 2019-12-03 21:39:57
I'm getting mad with infinite loop, what do you think is suitable solution? void sorting () { node * temphead = head; node * tempnode = NULL; for (int i=0; i<count; i++) { for (int j=0; j<count-i; j++) { if (temphead->data > temphead->next->data) { tempnode = temphead; temphead = temphead->next; temphead->next = tempnode; } temphead=temphead->next; count++; } } } I tried to increment count and use many conditions with while- before and after the for loop with no result sircodesalot An easier way to slide through a linked list is like this: for (node *current = head; current != nullptr; current

Passing a linked list head through a function as address in C

▼魔方 西西 提交于 2019-12-03 21:17:32
I have a question regarding passing the head of a linked list in C through a function. So the code goes something like this: #include <stdio.h> //Defining a structure of the node struct node { int data; struct node* next; }; void insert (struct node* rec, int x) { struct node* temp = (struct node*)malloc(sizeof(struct node)); temp->data = x; temp->next = NULL; rec = temp; // head and rec is now pointing to the same node } void print(struct node* rec){ printf("%d", rec->data); //error occurs here puts(""); } main(){ struct node *head = NULL; //head is currently pointing to NULL insert (head, 5)

Linked list with multiple parent and child nodes

ぃ、小莉子 提交于 2019-12-03 21:13:36
问题 I am trying to design a program that takes in data from a file, after which it gives numbering to unique data, linked list also contains parent and child lists. Data structure: ____A / | B C | / \ E--> F G | | | I J K The nodes can have more than one next nodes (e.g. A and C), and can have more than one previous nodes. The text file contains the data like this, i'll get the data from file and turn them into linked list : A B E I A C E F J A C G K My Question: Is it possible to create linked

How to find the middle node of a single linked list in a single traversal (if the length of the list is not given)

心已入冬 提交于 2019-12-03 20:21:43
I have a problem statement like: "How to find the middle node of a singly linked list in only one traversal, and the twist is we don't know the number of nodes in the linked list?" I have an answer like "take a vector and start pushing all the nodes' addresses as and when you are traversing the linked list and increment a counter till you reach the end of the list". So at the end we can get the number of nodes in the list and if even (counter/2) or if odd (counter/2 + counter%2) gives the middle node number and with this we can get vectore.at(middlenodenumber) points to the middle node". This

Reverse Linked List Recursively

只谈情不闲聊 提交于 2019-12-03 19:38:55
问题 I have a Node defined in Linked List as: typedef struct abc { int id; struct abc *next; }node; I want to reverse a Linked List recursively.I am passing the head pointer to the function. My function definition looks like: node *reverseLinkedListRecursively(node *head) { node *current; node *rest; if(head == NULL) return head; current=head; rest=head->next; if(rest == NULL) { return rest; } reverseLinkedListRecursively(rest); current->next->next=rest; current->next=NULL; return rest; } How

Getting merged/unified entries from ABAddressBook

老子叫甜甜 提交于 2019-12-03 18:30:19
问题 I'm developing an application that is showing the iPhone contacts. The ABAddressBookRef returns duplicate entries for a contact that appears only once in the iPhone contacts application. Looking on the contact card (from the iPhone contacts), in the bottom there is a section called "Linked Contacts" so obviously apple "merge"/"unify" these two entries into the one i see. The question here is what is the best way to mimic the same behavior so my app will show only one entry? is there an API