How does llvm's SDUse works? - llvm

I'm reading llc's code about SDUse,I cannot understand SDUse::addToList(SDUse **List).
How does its **pre and *next works to insert a node in a list?
/// Represents a use of a SDNode. This class holds an SDValue,
/// which records the SDNode being used and the result number, a
/// pointer to the SDNode using the value, and Next and Prev pointers,
/// which link together all the uses of an SDNode.
///
class SDUse {
/// Val - The value being used.
SDValue Val;
/// User - The user of this value.
SDNode *User = nullptr;
/// Prev, Next - Pointers to the uses list of the SDNode referred by
/// this operand.
SDUse **Prev = nullptr;
SDUse *Next = nullptr;
.....
void addToList(SDUse **List) {
Next = *List;
if (Next) Next->Prev = &Next;
Prev = List;
*List = this;
}
void removeFromList() {
*Prev = Next;
if (Next) Next->Prev = Prev;
}
};

Related

Why using & to point to Struct s not correct in C++( on Mac)?

I Use this code to add Node in the linked list's tail.
But it's not correctly running.
truct Node {
int value;
Node *next;
Node() {
}
Node(int i):value(i),next(NULL){
}
};
void addNode(Node **list, int i) {
if (!list ||!*list) {
return;
}
Node *n = *list;
Node *cur = n;
Node *tailP = NULL;
while(cur) {
tailP = cur;
cur = cur->next;
}
Node iNode = Node(i); // I first create a Node struct.
Node *node = &iNode;
tailP->next = node;
}
After I changed the function to the following, It's true now.
void addNode(Node **list, int i) {
if (!list ||!*list) {
return;
}
Node *n = *list;
Node *cur = n;
Node *tailP = NULL;
while(cur) {
tailP = cur;
cur = cur->next;
}
Node *node = new Node(i); // Now I use new to create a Struct Node.
tailP->next = node;
}
So I'm wondering what's going on? Thanks for your attention.
Node iNode = Node(i); is non-static local variable and its lifetime ends when returning from the function addNode, so storing its pointer to the list that is used even after returning from the function isn't appropriate.
On the other hand, Node *node = new Node(i); is creating an object on heap. Objects on heap survives after returning and is appropriate to use here.

Looping through linked lists

So basically, what I'm trying to do is going through all the nodes and verify if node.value is <= cost. If it is I need to remove that node and in the end I want to store the nodes that weren't remove in a new struct. I'm not sure how exactly am I suppose to do this.
This struct can be an example:
struct node {
int value;
node * next;
}
I'm going through all the nodes and remove only the ones that doen't have the required.
node * verify_money(node * head, int cost)
{
node * iterator = head;
while(iterator != NULL){
if(iterator.value <= cost) {
/*remove this node*/
}
iterator = iterator.next;
}
return /*struct without nodes removed/*
}
I want to get the remaining nodes.
What you are asking for really depends on your requirements, which you did not make clear enough.
If you are expected to modify the input list that is being verified, you can do something like this:
node * verify_money(node * head, int cost)
{
node * iterator = head;
node * previous = NULL;
while (iterator) {
node *next = iterator->next;
if (iterator->value <= cost) {
if (previous) previous->next = next;
if (head == iterator) head = next;
delete iterator;
}
else
previous = iterator;
iterator = next;
}
return head;
}
If you are expected to return a new list without modifying the original list, you can do something like this instead:
node * verify_money(node * head, int cost)
{
node * new_head = NULL;
node ** new_node = &new_head;
node * iterator = head;
while (iterator) {
if (iterator->value > cost) {
*new_node = new node;
(*new_node)->cost = value;
(*new_node)->next = NULL;
new_node = &((*new_node)->next);
}
iterator = iterator->next;
}
return new_head;
}

LinkedList creating nodes

I tried to implement C++ singly linked. I have created a method which creates a node and add a value and points to another node but i have to remember index.
How to improve the code and create nodes without remembering index? (I want to maintain order = first created node points to another etc.)
Class method:
void LinkedList::addValue ( int val )
{
if ( ! index )
{
n = new Node();
head = n;
n->value = val;
n->next = NULL;
}
else
{
n->next = new Node( );
n = n->next;
n->value = val;
}
++index;
}
I guess, you already have two member variables: head which is the root node and n which is the last node. You should initialize both of them with NULL (nullptr for c++11) in constructor. Then you can just check if n==NULL when you add a new value to the list.
LinkedList::LinkedList():head(NULL),n(NULL)
{}
void LinkedList::addValue ( int val )
{
if (n==NULL)
{
n = new Node();
head = n;
n->value = val;
n->next = NULL;
}
else
{
n->next = new Node( );
n = n->next;
n->value = val;
}
}
The index variable, however, can be useful if you want to find the list size in one fast read operation without iterating over all its nodes.
You can create another pointer called Tail to point to the last element. That way you can add value to the list without the index.
I call this method append
void append(int val){
void append(int val) {
Node* tmp = new Node(); // creating a temporary pointer to a new node
tmp -> value = val
last -> next = tmp; // connect the new node to the linked list
last = tmp; // set the last to the newly created node
listSize++; // increase the size of the list
}
You can also improve the code by making a Constructor for the node:
Class Node{
public:
int value;
Node * next;
Node(Node * nextEle = NULL) {
next = nextEle;
}
Node(int val,Node * nextEle = NULL) {
value = val;
next = nextEle;
}
}

How do I add an element to the front of a linked list?

this is how the node is set up:
struct Node {
Node *next;
Node *prev;
T datum;
};
this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
Node newfirst = first; // set newnode to first
&first = &datum;
datum = newfirst;
}
Node *first; // points to first Node in list, or 0 if list is empty
Node *last; // points to last Node in list, or 0 if list is empty
for some reason, I don't think this is right.
It seems you need the following
//this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
first = new Node { first, nullptr, datum };
if ( !last ) last = first;
}
If your compiler does not support initializer lists for the operator new then you can write
//this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
Node *tmp = new Node();
tmp->datum = datum;
tmp->next = first;
first = tmp;
if ( !last ) last = first;
}
You want to (i) create a new node with a valid content, and (ii) set is as the first node of your list. You can do that like in the following example:
void push_front(const T &datum)
{
Node* newFirst = new Node; //construct new Node
newFirst->next = first; // set newFirst's next node
newFirst->datum = datum; //set the content
first = newFirst; //assign new first node;
}
This is just as a sketch; for more details, you should post more code (such as mentioned in one of the comments).
Another thing to mention: I would prefer using a unique_ptr for one of those Node pointers, e.g.
struct Node {
std::unique_ptr<Node> next;
Node *prev;
T datum;
};
This you can very easily destroy the list (and also avoid the new command which is often recommended in modern C++).

Dummy Head Node Linked List

I'm trying to write an insert function for string values for a circular doubly linked list. I saw that creating a dummy node is beneficial in doing this so I can eliminate special cases like when the list is empty. The problem is I'm not finding alot of good information on dummy head nodes. I understand their purpose, but I don't understand how I create/implement it.
appreciate all the code examples guys, tried to figure it out on my own getting a little stuck though if someone can look at it.
#include <iostream>
#include <string>
using namespace std;
typedef string ListItemType;
struct node {
node * next;
node * prev;
ListItemType value;
};
node * head;
node * dummyHead = new node;
void insert(const ListItemType input, node * & within);
void main(){
insert("bob",dummyHead);
}
void insert( const ListItemType input, node * &ListHead){
node *newPtr = new node;
node *curr;
newPtr->value = input;
curr = ListHead->next; //point to first node;
while (curr != ListHead && input < curr->value){
curr = curr->next;
}
//insert the new node pointed to by the newPTr before
// the node pointed to by curr
newPtr->next = curr;
newPtr->prev = curr->prev;
curr->prev = newPtr;
newPtr->prev->next = newPtr;
}
For a circular doubly linked list, you can setup 1 sentinel node where both "next" and "prev" points to itself when list is empty. When list is not empty, sentinel->next points to first element and sentinel->prev points to last element. With this knowledge, your insert and remove function would look something like this.
This is very basic and your LinkedList and Node class maybe implemented differently. That is OK. The main thing is the insert() and remove() function implementation that shows how sentinel node(s) removes the need for checking for NULL values.
Hope this helps.
class DoublyLinkedList
{
Node *sentinel;
int size = 0;
public DoublyLinkedList() {
sentinel = new Node(null);
}
// Insert to the end of the list
public void insert(Node *node) {
// being the last node, point next to sentinel
node->next = sentinel;
// previous would be whatever sentinel->prev is pointing previously
node->prev = sentinel->prev;
// setup previous node->next to point to newly inserted node
node->prev->next = node;
// sentinel previous points to new current last node
sentinel->prev = node;
size++;
}
public Node* remove(int index) {
if(index<0 || index>=size) throw new NoSuchElementException();
Node *retval = sentinel->next;
while(index!=0) {
retval=retval->next;
index--;
}
retval->prev->next = retval->next;
retval->next->prev = retval->prev;
size--;
return retval;
}
}
class Node
{
friend class DoublyLinkedList;
string *value;
Node *next;
Node *prev;
public Node(string *value) {
this->value = value;
next = this;
prev = this;
}
public string* value() { return value; }
}
Why are you trying to use dummy node?
I hope you can handle it without a dummy node.
Eg:
void AddNode(Node node)
{
if(ptrHead == NULL)
{
ptrHead = node;
}else
{
Node* itr = ptrHead;
for(int i=1; i<listSize; i++)
{
itr = itr->next;
}
itr->next = node;
}
listSize++;
}
The above one is an example to handle the linked list without dummy node.
For a circular double linked list without a dummy node, the first node previous pointer points to the last node, and the last node next pointer points to the first node. The list itself has a head pointer to first node and optionally a tail pointer to last node and/or a count.
With a dummy node, the first node previous pointer points to the dummy node and the last node next pointer points to the dummy node. The dummy nodes pointers can point to the dummy node itself or be null.
The HP / Microsoft STL list function uses a dummy node as a list head node with the next pointer used as a head pointer to the first real node, and the previous pointer used as a tail pointer to the last real node.
#include <iostream>
#include <string>
using namespace std;
typedef string ElementType;
struct Node
{
Node(){}
Node(ElementType element, Node* prev = NULL, Node* next = NULL):element(element){}
ElementType element;
Node* prev;
Node* next;
};
class LinkList
{
public:
LinkList()
{
head = tail = dummyHead = new Node("Dummy Head", NULL, NULL);
dummyHead->next = dummyHead;
dummyHead->prev = dummyHead;
numberOfElement = 0;
}
void insert(ElementType element)
{
Node* temp = new Node(element, NULL, NULL);
if (0 == numberOfElement)
{
head = tail = temp;
head->prev = dummyHead;
dummyHead->next = head;
tail->next = dummyHead;
dummyHead->prev = tail;
}
else
{
tail->next = temp;
temp->prev = dummyHead->next;
temp->next = dummyHead;
dummyHead->next = temp;
tail = temp;
}
numberOfElement += 1;
}
int length() const { return numberOfElement; }
bool empty() const { return head == dummyHead; }
friend ostream& operator<< (ostream& out, const LinkList& List);
private:
Node* head;
Node* tail;
Node* dummyHead;
int numberOfElement;
};
ostream& operator<< (ostream& out, const LinkList& List)
{
Node* current = List.head;
while (current != List.dummyHead)
{
out<<current->element<<" ";
current = current->next;
}
out<<endl;
return out;
}
int main()
{
string arr[] = {"one", "two", "three", "four", "five"};
LinkList list;
int len = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < len; ++i)
{
list.insert(arr[i]);
}
cout<<list<<endl;
}
I think this code can help you. When you want to implement some data structure, you must have a clear blueprint about it.
Do the following inside the constructor
ptrHead = new Node();
listSize = 1;
if you have tail also,
ptrHead->next = ptrTail;
The above code will create dummy node.
Make sure you implementation should not affected by this dummy node.
eg:
int getSize()
{
return listSize-1;
}