linked-list

polynomial addition using linked list in java

纵然是瞬间 提交于 2019-12-06 08:49:13
Here's my implementation of a addition of two polynomials using a linked List. For example if I want to add 3x^2+5^x+3 and 4x^3+5x+2 first I check if there are similar exponents in the two polynomials and if so I add their coefficients and I append the exponents to a string. After adding similar exponents then using the string I add the remaining parts in both polynomials to the final result. public class Node2{ int coef; int exp; Node2 next; Node2(int c,int e,Node2 n){ coef=c; exp=e; next=n; } Node2(int c,int e){ coef=c; exp=e; } } public class LinkedPoly{ static String exponent=""; Node2

Pass the field name of struct to access inside a function

丶灬走出姿态 提交于 2019-12-06 08:42:25
问题 I have a linked list and I made a function to fetch a node. But I want to use it both to search by first or last name. typedef struct people { char name[60], lastname[60]; struct people *next; } people; people *search(const char *key, people *list, FIELD) { while (list && strcmp(key, list->FIELD) != 0) { list = list->next; } return list; } Example: people *aux; aux = search("John", list_of_people, "name"); Or: aux = search("Smith", list_of_people, "lastname"); There is a clear and efficient

C Linked-list Destroy Function

心已入冬 提交于 2019-12-06 08:17:22
问题 I'm trying to learn C, and as many people, I've been a little stuck with pointers. Anyways, I made a recursive function that destroys my linkedlist, but as I've debugged, when I'm back from the function the head of the list is not null as it should be, so I'm guessing it's some basic misunderstanding with pointers. Here's the function: void destroy(struct node* n){ if(!n) return; destroy(n->next); free(n); n = NULL; } Thanks in advance. 回答1: void deleteList(struct node** head_ref) { struct

Is it possible to sort a an array list while ignoring the first 3 characters in each string?

旧街凉风 提交于 2019-12-06 07:53:32
问题 I am trying to figure out how to sort my list alphabetically, normally this would be really easy, but I need to ignore the first 5 characters of each string in my list. (they are numerical IDS) ArrayList<String> tempList = new ArrayList<String>(); for(String s : AddressBook){ tempList.add(s); Collections.sort(tempList ); } System.out.println(tempList); 回答1: You can do it by supplying your own Comparator implementation. Collections.sort (tempList, new Comparator<String>() { public int compare

C - Inserting into linked list in ascending order

末鹿安然 提交于 2019-12-06 06:45:48
问题 I am trying to create a program that inserts numbers into a linked list in ascending order. This is my insert function. It works for inserting some numbers but not others. I think it has something to do with the last part, but i cant figure it out. node* insert(node* head, int value) { //check if head hasn't been created if (head == NULL) { head = malloc(sizeof(node)); if(head == NULL) { printf("Failed to create head node"); return head; } head->value = value; head->next = NULL; return head;

Redis Data Structure Space Requirements

半世苍凉 提交于 2019-12-06 06:31:44
问题 What is the difference in space between sorted sets and lists in redis? My guess is that sorted sets are some kind of balanced binary tree, and lists are a linked list. This means that on top of the three values that I'm encoding for each of them, key, score, value, although I'll munge together score and value for the linkedlist, the overhead is that the linkedlist needs to keep track of one other node, and the binary tree needs to keep track of two, so that the space overhead to using a

How to remove a specific value from linked list in Java?

回眸只為那壹抹淺笑 提交于 2019-12-06 06:15:53
问题 How to remove a specific value from a linked list java? I tried to make it in my implementation, but it wasn't easy.. Here is what I'm trying to make: //How to do this...;<.. int remove(Item item) { Node cur = first.next; Node prev = first; while (cur !=null) { if (cur.item.equals(item)) { item = dequeue(); } cur = cur.next; // TODO } return 0; } These are the pre-setup: public class LinkedQueue<Item> implements Iterable<Item> { private int N; // number of elements on queue private Node first

Struct and pointer to pointer

梦想与她 提交于 2019-12-06 05:13:02
I am learning about linked lists and how to create them in C with structs and pointers. I have an example below. From my understanding the called push() passes the beginning memory location of our struct where the head node lies as an argument. The parameter of our push() function takes a struct node as a pointer to pointer so it is passed as a reference, not an actual copy. So the first pointer of our struct node** headref is just a pointer to the memory location of our head node and the second pointer points to the value, which is the next memory location that the head node points to. We

Sorting a linked list in ascending order in C

时间秒杀一切 提交于 2019-12-06 05:09:11
I am working on a program for my C programming course that is supposed to give us experience using linked lists. One of the last parts of the assignment asks for us to take a linked list and sort it in ascending order using prepend or append functions that we wrote earlier in our program. struct lnode { int datum; struct lnode *next; }; struct lnode* prepend(struct lnode *list, int x) { struct lnode *node = (struct lnode *)malloc(sizeof(struct lnode)); node -> datum = x; node -> next = list; list = node; return list; } struct lnode* append(struct lnode *list, int x) { if(list==NULL){ list =

Linked list insertion sort

半城伤御伤魂 提交于 2019-12-06 05:06:13
I'm not very advanced in the sorting part of programming yet, so I was looking for some help with my algorithm. void sortList() { Item_PTR tmpNxt = current->nextItem; Item_PTR tmpPTR = current; int a, tmp; while(tmpNxt != NULL) { a = tmpPTR->value; while(tmpNxt != tmpPTR && tmpNxt->value < a) { tmp = a; tmpPTR->value = tmpNxt->value; tmpNxt->value = tmp; tmpPTR = tmpPTR->nextItem; } tmpPTR = current; tmpNxt = tmpNxt->nextItem; } } The list state before sorting: 9 8 7 6 5 4 3 2 1 after sorting: 1 9 8 7 6 5 4 3 2 I'm not sure why...I've played computer a lot on paper and I feel like it should