The code below is adding a new node to the head of the list. I was wondering how I would change it so that it would add a new node to the tail(end) of the list instead of the head.
typedef struct node{
int data;
struct node *next;
struct node *prev;
} Node, *NodePtr;
typedef struct linkedlist{
NodePtr head;
NodePtr tail;
} LinkedList, *LinkedListPtr;
void insertTail(LinkedListPtr list, int data){
NodePtr newNode;
newNode = (NodePtr)malloc(sizeof(Node));
newNode->data = data;
newNode->next =NULL;
newNode->prev =NULL;
/* add it to the beginning of linked list*/
if (list->head==NULL){ /*linked list is empty*/
list->head=newNode;
list->tail=newNode;
}
else {
//connect new node to the head of the list
newNode->next = list->head;
list->head->prev=newNode;
//reassign head of the list
list->head = newNode;
}
}
Related
I am trying to create doubly linked list with a function to add a node at the end of the list. I have two typedef structs: one for Node and one for the doubly linked list.
In insertion_last() I am getting an error when trying to set list->tail->next to new_node. The error is "pointer to incomplete class type is not allowed" and is referring to DoublyLinkedList. I suppose I have done something wrong in making the structs, but cannot quite figure out why it is not working.
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeStruct
{
int data;
struct Node *next;
struct Node *prev;
} Node;
typedef struct DoublyLinkedListStruct
{
int size;
struct Node *head;
struct Node *tail;
} DoublyLinkedList;
Node* newNode(int data, Node* next, Node* prev)
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = next;;
new_node->prev = prev;
return new_node;
};
void insertion_beginning(DoublyLinkedList* list, int new_data)
{
Node* new_node = newNode(new_data, list->head, NULL);
list->head = new_node;
if (!list->tail)
{
list->tail = new_node;
}
list->size++;
}
void insertion_last(DoublyLinkedList* list, int new_data)
{
Node *new_node = newNode(new_data, NULL, list->tail);
if(list->tail)
{
list->tail->next = new_node;
}
else
{
list->head = new_node;
}
list->tail = new_node;
list->size++;
}
You define tail and several others fields to have type struct Node *. You don't have any such struct defined.
You want to change those to use struct NodeStruct * instead.
I am getting an error in the constructor for Node inside the sizeof(Node) saying "type name is not allowed" any thought ? Thank you
struct Node {
int data;
Node *next;
};
struct LinkedList {
Node *first;
int size;
};
typedef struct Node Node;
typedef struct LinkedList LinkedList;
//constructor for node
Node* createNode(int data) {
Node * newNode = malloc(sizeOf(Node));
if (data != NULL) {
newNode->data = data;
newNode->next = NULL;
return newNode;
}
return NULL;
}
malloc(sizeOf(Node))
should be
malloc(sizeof(Node))
also I suspect that there are other compiler errors; you have to do
struct Node *
rather than
Node *
I want to make a double linked list and I have a problem with accessing fields in the struct. This is my code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int val;
struct node * next;
struct node * prev;
}node;
void insert(int val, node **head)
{
node * temp= *head;
node * temp2=(node *)malloc(sizeof(node));
node * temp3=(node *)malloc(sizeof(node));
temp2->val=val;
temp2->prev=NULL;
temp2->next=*head;
*head=temp2;
temp2->next->prev=temp2;
}
void print(node* head)
{
node* temp=head;
while(temp!=NULL)
{
printf("%d ", temp->val);
temp=temp->next;
}
}
int main()
{ node * head=NULL;
insert(1, &head);
insert(2, &head);
print(head);
return 0;
}
I get a crash at temp2->next->prev, and I don't understand why. Am I not allowed to access the prev field of the temp2->next node? I tried writing (temp2->next)->prev but also doesn't work. Is there any way that I cant make that work?
When you insert the first node, *head, and therefore temp->next, is NULL. Check that case:
void insert(int val, node **head)
{
node *temp= malloc(sizeof(*temp));
temp->val = val;
temp->prev = NULL;
temp->next = *head;
*head = temp;
if (temp->next) temp->next->prev = temp;
}
(I've removed the unused variables and lost the cast on malloc.)
A doubly linked list should probably have a tail, too. In that case, update the tail when you append an item at the head of an empty list.
try this:
void insert(int val, node **head)
{
if(*head == NULL){
node * temp2=(node *)malloc(sizeof(node));
temp2->val=val;
*head = temp2;
}
else{
node * temp= *head;
node * temp2=(node *)malloc(sizeof(node));
temp2->val=val;
temp2->prev=NULL;
temp2->next=temp;
temp->prev = temp2;
}
}
Most likely head was not initialized
As I have understood you always insert a new node before the head though in double linked list you have to add new nodes usually at the tail of the list. As a double linked list usually have two sides then there is a sense to define two functions: push_front and push_back. Your function insert corresponds to function push_front.
Nevertheless function insert could look the following way
void insert( int val, node **head )
{
node *temp = ( node * )malloc( sizeof( node ) );
temp->val = val;
temp->prev = NULL;
temp->next = *head;
if ( *head ) ( *head )->prev = temp;
*head = temp;
}
It would be better if you would define one more structure named as for example List (or whatever) and that could be defined as
struct List
{
node *head;
node *tail;
};
Also you could add one more data member - a count of the nodes in the list, For example
struct List
{
node *head;
node *tail;
size_t count;
};
Also do not forget to write a function that would delete all nodes of the list when it is not needed any more.
I have some errors to my code and I still don't understand why it does not work.
I have the following code snippet:
void insertBefore(List *lista, Node **node, Node *newNode)
{
newNode->prev = (*node)->prev;
newNode->next = (*node);
if((*node)->prev == 0)
lista->first = newNode;
else
(*node)->prev->next = newNode;
(*node)->prev = newNode;
}
And I call it as:
insertBefore(lista,lista->first, newNode);
And the error is :
error: dereferencing pointer to incomplete type
What I tried and works(no errors but crashes when I debug):
void insertBefore(List *lista, Node **node, Node *newNode)
{
Node *anotherNode = (*node)->prev;
newNode->prev = (*node)->prev;
newNode->next = (*node);
if((*node)->prev == 0)
lista->first = newNode;
else
anotherNode->next = newNode;
(*node)->prev = newNode;
}
Here are the structures I use:
typedef struct NodeT
{
struct nodeT *prev;
struct nodeT *next;
int key;
}Node;
typedef struct ListT
{
Node *first;
Node *last;
Node *current;
}List;
Now, my question is: is there any issue when the compiler parses? I really don't figure it out.
Watch the capitalization!
typedef struct NodeT /* uppercase N */
{
struct nodeT *prev; /* lowercase N */
NodeT and nodeT are different identifiers
I have this c program that delete the first node and show the node head data.
int Delete(struct node** head){
struct node *temp = headRef;
headRef = headRef->next;
tmp->next=NULL;
free(temp);
int headNode = headRef->data;
return headNode; }
I was not able to delete the first node but it give me error of request member 'data' and 'struct'
I don't understand where if headRef comes from.
Secondly, you only need to pass the head node into the function, therefore you need
struct node* head, not struct node** head
This is my code, I hope it helps you.
int Delete(struct node* head) {
struct node* temp = head;
struct node* nextNode = head -> next;
int headData = head -> data;
temp -> next = NULL;
free(temp);
head = nextNode;
return headData;
}