linked-list

Write a function that rearranges a linked list to put the nodes in even positions after the nodes in odd positions in the list

旧城冷巷雨未停 提交于 2019-12-13 02:22:30
问题 Write a function that rearranges a linked list to put the nodes in even positions after the nodes in odd positions in the list, preserving the relative order of both the evens and the odds. I found this problem in the book Algorithm in c writtern by Sedgewick. I have tried but failed. I trid to put all nodes in even positions on another linked list. It's grateful for you to help me. A good idea is enough. Thanks :). This is my Code in C. /* * File: rearranges.c <Exercise 3.36> * Note: Write a

Linked list program in C

折月煮酒 提交于 2019-12-13 02:21:01
问题 I am coding for a simple linked list , but facing a little problem. The program is like it is accepting name, age and DOB through user, and memory for it is dynamically allocated. After taking data from the user, it is searching a name, asked by user, if the name exists, it should print all the details related to it. Here is my code- //function declarations struct node *initnode(char *, char *, char *); void add(struct node *); struct node *printnode(struct node *); struct node *searchname

C Program: Print Linked List from Recursive Ordering Function

不想你离开。 提交于 2019-12-13 02:18:01
问题 I am creating a linked list by reading in a text file and inserting the letters in alphabetical order in the list. I need to print the list, but cannot seem to get the correct function. I keep getting an error error: invalid type argument of ‘->’ (have ‘order_list’) error: invalid type argument of ‘->’ (have ‘order_list’) I know this is incorrect, but I am at a loss for correctly stating the print_alph function. Any help in finding a way to correctly print my list would be greatly appreciated

How to pop element from tail in linked list?

人走茶凉 提交于 2019-12-13 01:26:20
问题 I am trying to learn linked list using various push and pop functions but I am not able to do popping of element from tail in linked list. Can anyone help me to solve popBack function? I have tried something like: typedef struct { float val; }data; typedef struct nodePtr { struct nodePtr *next; data *d; }node; typedef struct { node *head; node *tail; }linkList; linkList* createlinkList() { linkList *ll = (linkList*)malloc(sizeof(linkList)); ll->head = NULL; ll->tail = NULL; return ll; } node*

C++ linked list append method

拥有回忆 提交于 2019-12-13 00:17:32
问题 I am writing a linked list template class in C++ as an exercise for myself to help me get back into C++ programming. I've got the following class definition: template <typename T> class List { public: List(); ~List(); void append(T data); void display(); int count(); private: struct Node { T data; Node *next; } *head; }; I have two versions of the append method - one that works and one that doesn't. I can't figure out what the difference, in terms of the operations performed, is, and why the

Significance of ** in pointers - C language

此生再无相见时 提交于 2019-12-13 00:12:56
问题 I am trying some operations on linked lists in C language. I am a newbie and sort of getting confused in the function append() . They have passed arguments in the function like struct node **q . And they are using it with *q . Function append() in C language: void append(struct node **q, int num) { struct node *temp, *r; if(*q==NULL) { temp=malloc(sizeof(struct node)); temp->data=num; temp->link=NULL; *q=temp; } else { temp=*q; while(temp->link!=NULL) temp=temp->link; r=malloc(sizeof(struct

Why use a pointer to a pointer to the stack when creating a push function?

允我心安 提交于 2019-12-12 23:44:52
问题 I am looking at a textbook example of a linked list that implements a stack. I don't understand why using a pointer to a pointer to the stack is necessary for the push operation. See the following example: bool push( Element **stack, void *data) { Element *elem = new Element; if(!elem) return false; elem->data = data; elem->next = *stack; *stack = elem; return true; } If anyone can help clarify why the first parameter of the push method is a pointer to a pointer, I would greatly appreciate it

Linked list in C with fgets()

老子叫甜甜 提交于 2019-12-12 22:07:53
问题 I'm having issues with my code skipping the first question in the second data structure. I'm pretty sure it's because the gets(), but not sure. I think I tried fgets(), but it still was giving me issues. Why? #include <stdio.h> #include <stdlib.h> #include <string.h> #define NumberOfActresses 5 typedef struct Actress { char *name, *placeofbirth, *haircolor; int age; float networth; struct Actress *next; } Actress; void PopulateStruct(Actress *node) { node->name = (char *) malloc(sizeof(char)

Valgrind 8 bytes inside a block of 16 free'd

て烟熏妆下的殇ゞ 提交于 2019-12-12 20:46:17
问题 I'm writing code for a lab in class, which is an exercise in OOD design using a circular linked list. This just means that a few key functions that are used aren't accessible to me. However, I'm mostly confused because although my driver mimics the one written by the professor, I'm still getting the mchk error in the title. Here is the code it references { int nNodesFreed{0}; node* n{head}; for(; n!= head || ! nNodesFreed; n = n->next) { delete n; nNodesFreed++; } cout << "# nodes freed: " <<

How to reverse a linked list - detailed explanation

旧巷老猫 提交于 2019-12-12 18:34:57
问题 Can anyone share a link to the code which explains of how to reverse a linked list? Or could somebody explain the code snippet below? I've tried to draw/write it, but still don't get how nodes get reversed. public void reverseList() { Node reversedPart = null; Node current = head; while (current != null) { Node next = current.next; current.next = reversedPart; reversedPart = current; current = next; } head = reversedPart; } 回答1: Let's take a look into a simple example: 1 > 2 > 3 > 4 > 5 >