linked-list

Pass the field name of struct to access inside a function

泪湿孤枕 提交于 2019-12-04 13:23: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 way to solve this problem without repeating code? use offsetof(<stddef.h>) macro. E.g: people *search

dealing with array of linked list

只谈情不闲聊 提交于 2019-12-04 13:12:11
问题 My approach: An array of fixed-length (lets say 20) each element is pointer to the first node of a linked list. so i have 20 different linked list. This is the structure: struct node{ char data[16]; struct node *next; }; My declaration for that array struct node *nodesArr[20]; now to add a new node to one of the linked list, i do this: struct node *temp; temp = nodesArr[i]; // i is declared and its less than 20 addNode(temp,word); // word is declared (char *word) and has a value ("hello") The

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

拈花ヽ惹草 提交于 2019-12-04 12:58:13
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; // beginning of queue private Node last; // end of queue // helper linked list class private class

Why can't I push this object onto my std::list?

拜拜、爱过 提交于 2019-12-04 12:56:19
Just started programming in C++. I've created a Point class, a std::list and an iterator like so: class Point { public: int x, y; Point(int x1, int y1) { x = x1; y = y1; } }; std::list <Point> pointList; std::list <Point>::iterator iter; I then push new points onto pointList. Now, I'm needing to iterate through all the points in pointList, so I need to loop using the iterator. This is where I get screwed up. for(iter = pointList.begin(); iter != pointList.end(); iter++) { Point currentPoint = *iter; glVertex2i(currentPoint.x, currentPoint.y); } Update You guys were right, the problem isn't in

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

China☆狼群 提交于 2019-12-04 12:52:29
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); You can do it by supplying your own Comparator implementation. Collections.sort (tempList, new Comparator<String>() { public int compare(String o1, String o2) { String sub1 = o1.substring (3); String sub2 = o2.substring (3); return sub1

C# singly linked list implementation

痞子三分冷 提交于 2019-12-04 12:19:50
问题 While trying to understand how a singly list can be implemented in C#, I came across the link below : Creating a very simple linked list. However, as I am new to C#, I got confused by the syntax that is listed in the initial section of the discussion above. A class named Node is being declared and there is another statement within the class declared as "public Node next". Is this statement called a constructor? Please help. public class Node { public Node next; public Object data; } 回答1: In a

adding two linked lists efficiently in C

别来无恙 提交于 2019-12-04 11:55:48
问题 I have two linked lists representing the digits of decimal numbers in order from most- to least-significant. for eg 4->7->9->6 and 5->7 The answer should be 4->8->5->3 without reversing the lists because reversing the lists would result in decrease of efficiency. I am thinking of solving the problem using stack.I will traverse both the lists and push the data elements into two separate stacks.One for each linked list.Then I pop both the stacks together and add both the elements and if the

“warning: useless storage class specifier in empty declaration” in struct

左心房为你撑大大i 提交于 2019-12-04 11:20:31
问题 typedef struct item { char *text; int count; struct item *next; }; So I have this struct with nodes defined as above, but Im getting the error below and Im not able to figure out whats wrong. warning: useless storage class specifier in empty declaration }; 回答1: I'm not sure, but try like that : typedef struct item { char *text; int count; struct item *next; }item; 回答2: The typedef is useless because you didn't give it a name. You cannot use the typedef in any way. That's why you get a warning

Count number of nodes in a linked list that may be circular

主宰稳场 提交于 2019-12-04 08:47:28
Here is the problem, it is from Sedgwick's excellent Algorithms in Java (q 3.54) Given a link to a node in a singly linked list that contains no null links (i.e. each node either links to itself or another node in the list) determine the number of different nodes without modifying any of the nodes and using no more than constant memory space. How do you do it? scan through the list once using the hare and tortoise algorithm to work out whether it is circular in any way, and then scan through again to work out where the list becomes circular, then scan through again counting the number of nodes

atomic swap with CAS (using gcc sync builtins)

前提是你 提交于 2019-12-04 08:18:41
Can the compare-and-swap function be used to swap variables atomically? I'm using C/C++ via gcc on x86_64 RedHat Linux, specifically the __sync builtins. Example: int x = 0, y = 1; y = __sync_val_compare_and_swap(&x, x, y); I think this boils down to whether x can change between &x and x; for instance, if &x constitutes an operation, it might be possible for x to change between &x and x in the arguments. I want to assume that the comparison implicit above will always be true; my question is whether I can. Obviously there's the bool version of CAS, but then I can't get the old x to write into y