linked-list

Linked List implementation for a stack

早过忘川 提交于 2019-12-25 02:07:15
问题 Here is my implemetation of stack with linkedlist STACK using linked list STACK-EMPTY: if L.head == NIL return True else return False PUSH(x): x.next = L.head if L.head != NIL L.head.prev = x L.head = x x.prev = NIL POP(): x = L.head L.head = x.next x.next.prev = L.head return x would you validate this? how to improve ? thanks 回答1: You can improvement the consistency of your data structure: The prev of the list head is always NIL An element which is not in the list has next and prev set to

Deleting Nodes in a Linked List

喜你入骨 提交于 2019-12-25 01:58:17
问题 After a lot of effort, I've managed to piece together a function that deletes some node from my linked list. But, out of sheer interest, I would like to find out how you can go about deleting the first node from the list, i.e. the head. My program asks for a letter to delete, so for example. Hello is stored in the list, the user inputs H for deletion, so that now the list is ello At the moment with my code, the program crashes obviously as if H is deleted, there is no head, and the program

I'm confused on how i'd go about overloading the + operator on a stack using a linked list

做~自己de王妃 提交于 2019-12-25 01:31:14
问题 First of all, i know how to do this using a linked list in general, which i will paste below: List operator+(const List& l) const { // copy the first list List t = *this; Node *n = l.head; // iterate through the second list and copy each element to the new list while (n != NULL && !t.IsFull()) { t.InsertAfter(n->data); n = n->link; } return t; } Now in my Stack i have something that looks like this: Stack operator+(const Stack& s) const { // copy the first list Stack t = *this; Node *n = s

Priority Queue C

空扰寡人 提交于 2019-12-24 23:48:11
问题 I'm trying to create a priority queue using an array of queues, each index of the array being a priority. I tried the following solution, The queue data type contains an array llist, Queue *q_create(int size) { struct queue *p; struct q_head *h; int i; if ((p = (struct queue *)malloc(sizeof(struct queue))) != NULL) { p->size = size; for (i = 0; i < PRIODIFF; i++) { h = &(p->llist[i]); h->head = NULL; h->tail = NULL; } } return p; } I'm confused by the line: h = &(p->llist[i]); I was thinking

Generic Single Linked List using std::unique_ptr, unknown uncompilation errors in Microsoft Visual Studio C++

大城市里の小女人 提交于 2019-12-24 23:32:02
问题 I am very new to Microsoft visual studio C++ as well as std::unique_ptr. On CodeReview I was recommended to rewrite using std::unique_ptr. You can find the question I am referencing here. Here are the following errors I am receiving: 1>main.cpp 1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(26): error C2760: syntax error: unexpected token 'identifier', expected ';' 1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(61): note: see reference to class template instantiation 'SingleLinkedList<T

How to make a class a member of two linked lists in Scala?

匆匆过客 提交于 2019-12-24 23:26:33
问题 I have a feeling I'm missing something very obvious here. I'm converting a compiler generator written in Java into Scala as a learning exercise. It's not really written in Java, it seems to be transliterated C. In one part of the program, there are Nodes. Each node is part of two linked lists, and there are fields that are references to the next item in each of the linked lists (call these "across" and "down"). Various methods traverse these lists, either one of them or both of them and do

adding a new user to list in c program

随声附和 提交于 2019-12-24 22:03:34
问题 Im just trying to write a simple function to addd a friend to a UserAccount list. All the information is provided through the parameter. If the user is already in the list I do not need add him again but display records saying he is already in list. I wrote this piece of code. not sure if this is correct, any suggestion to improve the code? Does this work? int add_friend(UserAccount* user, char Circle, UserAccount* friend) { struct UserAccountNode *p; p = (struct UserAccountNode *) malloc

Multiple user processes accessing kernel linked list

爱⌒轻易说出口 提交于 2019-12-24 19:56:29
问题 I have 10 user space application which could invoke the same system call to kernel. For example 10 user space program could invoke bind system call. Upon receiving the system call, my code in the kernel would insert a data in linked list I created in the kernel space. I am using kernel linked list to add, remove and traverse node. I am also locking the node before writing to prevent the scenario where the list is not corrupted when accessed simultaneously by different user space processes. I

How do I insert a node at the beginning of a linked list?

*爱你&永不变心* 提交于 2019-12-24 19:22:55
问题 What is wrong with addAtBegin ? The list seems to be correct after assigning the newly created node to start , but when control returns to main , the new value is not saved. typedef struct node { int data; struct node *link; }node; node* createList(int data) { node *tempNode=(node*)malloc(sizeof(node)); tempNode->data=data; tempNode->link=NULL; return tempNode; } void addAtBegin(node *start,int data) { node *addedNode=(node *)malloc(sizeof(node)); addedNode->data=data; addedNode->link=(start=

C - Nested linked lists

旧时模样 提交于 2019-12-24 19:05:08
问题 I'm trying to create a linked list of students, each with a linked list of grades, but I'm having trouble accessing the linked list of grades inside the linked list of students. typedef struct student_data_struct{ char student[MAX]; struct grades_list_struct *gradeP; } student_Data; typedef struct student_list_struct{ student_Data studentData; struct student_list_struct *next; } StudentNode; typedef struct grades_list_struct{ int grade; struct grades_list_struct *next; } GradeNode; GradeNode