I made a snake program with Linked List, the program is working but I have some warning, I wonder if I did something wrong or its just something to ignore.The program itself working without any bugs.
I
//Thats what I send from main
SNAKE* snake = (SNAKE*)malloc(sizeof(SNAKE));
//Thats the function
void initialSnaKE(SNAKE* snake){
NODE* tail = (NODE*)(malloc(sizeof(NODE)));
NODE* head = (NODE*)(malloc(sizeof(NODE)));
head->prev = NULL;
tail->prev = head;
head->next = tail;
tail->next = NULL;
head->x = ROWS / 2;
head->y = COLS / 2;
snake->head = head;
snake->tail = tail;
}
There are chances that malloc may fail, that is what compiler is complaining.
You have to have proper NULL checks before dereferencing the pointers.
void initialSnaKE(SNAKE* snake){
if (!snake) return; //invalid input
NODE* tail = (NODE*)(malloc(sizeof(NODE)));
if (!tail) return; //allocation failed.
NODE* head = (NODE*)(malloc(sizeof(NODE)));
if (!head) {
free(tail);
return;
}
head->prev = NULL;
tail->prev = head;
head->next = tail;
tail->next = NULL;
head->x = ROWS / 2;
head->y = COLS / 2;
snake->head = head;
snake->tail = tail;
}
Related
If I call createnode(x) in main and then follow that with printNodes(); I will get an infinite while-loop that seems to be printing some memory address. I am guessing the issue lies in the fact that i set head = temp?
SinglyLinkedList *head = NULL;
void createNode(int data){
SinglyLinkedList *temp = malloc(sizeof(SinglyLinkedList));
temp-> data = data;
if(head == NULL){
head = temp;
return;
}
temp->next = head;
head = temp;
}
void printNodes(){
SinglyLinkedList *temp = head;
while ( temp != NULL ){
printf("%d\r\n",temp->data);
temp = temp->next;
}
}
SinglyLinkedList *temp = malloc(sizeof(SinglyLinkedList));
temp->data = data;
if(head == NULL){
head = temp;
return;
}
temp->next = head;
head = temp;
should be
SinglyLinkedList *temp = malloc(sizeof(SinglyLinkedList));
temp->data = data;
temp->next = head; // Moved
if(head == NULL){
head = temp;
return;
}
head = temp;
which is a very complicate way of writing
SinglyLinkedList *temp = malloc(sizeof(SinglyLinkedList));
temp->data = data;
temp->next = head;
head = temp;
Without this fix, printNodes results in undefined behaviour as a result of the lack of initialization of temp->next.
This is the code I tried writing down to add an element in a double linked list, that takes an index and a value and adds a new element to the original list.
It is taking index and value but just adds the elements I give like a stack.
node *add(node *head, int index, int val)
{
node *new = create(val);
node *temp = malloc(sizeof(node));
if (head == NULL)
{
head = new;
temp = head;
//printf(": %d",temp->data);
}
temp = head;
int i = 1;
while (i < (index - 1) && (temp->next != NULL))
{
i++;
temp = temp->next;
}
temp->next = new;
new->next = NULL;
new->prev = temp;
return head;
}
however this code(for a doubly linked list) just adds elements one after the other, disregarding the index passed.
My prof gave us a code for a singly linked list where he did something similar.
struct Node *insert(struct Node *listp, int pos, int info)
{
/*Inserts a Node in list at position pos with data info
pos>0. If pos>list length then new node added at end.
If pos<1 adds at beginning.
*/
struct Node *new=malloc(sizeof(struct Node)), *prev;// new is the new node we create everytime.
//create new node and initialize fields
new->data=info;
new->next=NULL;
if (listp==NULL) listp=new;
else
if (pos<=1) { //negative or 1 index.
new->next=listp; //first node bann gaya new
listp=new; //head is pointing at new
}
else {
//pos>1. Go to node at pos-1.
prev=listp;
int i=1;
while ((i++<pos-1) && prev->next!=NULL) { //indexing
prev=prev->next;
}
new->next=prev->next;
prev->next=new;
}
return listp;
}
how do I address this problem?
To implement a doubly linked list, your node needs to have pointers to the previous node and next node, then you can write something almost as similar to what your professor gave you for single linked list:-
#include <stdlib.h>
#include <stdio.h>
//Doubly linked list Node
typedef struct Node{
int info;
struct Node* next;
struct Node* prev;
} Node;
Node* insert(Node* listp, int pos, int info){
//Allocate memory for node and its previous neighbor
Node* new = malloc(sizeof(Node));
Node* prev = malloc(sizeof(Node)), *tail;
//initialize new node with these values
new->info = info;
new->next = NULL;
new->prev = NULL;
//if head doesn't exist then create one
if(listp == NULL){
/*
listp gets whatever new had, ie
listp->info = info
listp->next = NULL
listp->prev = NULL
*/
Node* tail = malloc(sizeof(Node));
tail->info = 0;
tail->next = NULL;
tail->prev = NULL;
listp = new;
listp->next = tail;
tail->prev = listp;
}
//Lets Loop through the List and insert node at pos
else {
if(pos <= 1){
/*
This should replace the current head
listp = new
*/
new->next = listp;
new->prev = listp->prev;
listp->prev = new;
listp = new;
}
else{
int i = 2;
prev = listp->next;
printf("%d\n", prev->prev->info);
while(i != pos){
printf("%d\n", new->info);
prev = prev->next;
i++;
}
new->next = prev;
new->prev = prev->prev;
prev->prev->next = new;
prev->prev = new;
}
}
return listp;
}
// Test case
int main(){
Node* listp;
listp = insert(NULL, 0, 2);
listp = insert(listp, 1, 3);
listp = insert(listp, 2, 5);
Node* cnt = listp;
printf("|");
while(cnt->next != NULL){
printf("--%d-->", cnt->info);
cnt = cnt->next;
}
printf("\n");
}
Try that. Make any typo corrections if any
#include <stdio.h>
#include <stdlib.h>
/**
* add_node_end - adds a new node at the end of a dllist list
* #head: head of linked list
* #n: integer value of node
*
* Return: address of new element, NULL if fails
*/
node *add_node_end(node **head, const int n)
{
node *new, *temp;
new = malloc(sizeof(node));
if (new == NULL)
return (NULL);
new->n = n;
new->next = NULL;
if (*head == NULL)
{
new->prev = NULL;
*head = new;
return (*head);
}
temp = *head;
while (temp->next)
{
temp = temp->next;
}
temp->next = new;
new->prev = temp;
return (new);
}
/**
* add_dnodeint - adds a new node at the beginning of a dlistint_t list
* #head: head of linked list
* #n: integer value of node
*
* Return: address of new element, NULL if fails
*/
node *add_node_start(node **head, const int n)
{
node *new;
new = malloc(sizeof(node));
if (new == NULL)
return (NULL);
new->n = n;
new->prev = NULL;
if (*head == NULL)
{
new->next = NULL;
*head = new;
return (*head);
}
new->next = *head;
(*head)->prev = new;
*head = new;
return (*head);
}
/**
* node_len - returns the number of elements in a dllist list
* #h: head of doubly linked list
*
* Return: number of nodes
*/
size_t node_len(const node *h)
{
int count = 0;
while (h)
{
count++;
h = h->next;
}
return (count);
}
/**
* insert_dnodeint_at_index - inserts a new node at a given position
* #h: a pointer to a pointer of the first node of node linked list
* #index: the position to add the new node
* #val: the data n of the new node
* Return: if the function fails = NULL
* otherwise - the address of the new node/element
*/
node *insert_dnodeint_at_index(node **h, unsigned int index, int val)
{
node *newNode, *current;
size_t list_length;
unsigned int i = 0;
if (h == NULL)
return (NULL);
if (index == 0)
return (add_node_start(h, n));
list_length = node_len(*h);
if (index == (list_length - 1))
return (add_node_end(h, n));
newNode = malloc(sizeof(node));
if (newNode == NULL)
return (NULL);
newNode->val = val;
if (*h == NULL)
{
newNode->prev = NULL;
newNode->next = NULL;
return (newNode);
}
current = *h;
while (current)
{
if (i == index)
{
newNode->next = current;
newNode->prev = current->prev;
current->prev->next = newNode;
current->prev = newNode;
return (newNode);
}
current = current->next;
i++;
}
free(newNode);
return (NULL);
}
I want to reverse a linked list in pair such that if the list is 1->2->3->4->X
then it should output the following 2->1->4->3->X
I have tried to solve this problem but it doesn't seem to run. Guys, can you please help me with what is wrong in my code.
ListNode* reverseListInPairs(ListNode *head){
ListNode *current = head,*newHead = NULL,*temp = NULL,*prev = NULL;
while(current != NULL && current->next != NULL){
temp = current->next;
current->next = current->next->next;
temp->next = current;
prev->next = temp;
prev = current;
current = current->next;
if(newHead == NULL){
newHead = temp;
}
}
return newHead;
}
what is wrong in my code.
The main problem I see is here:
prev->next = temp;
On the first iteration of the loop, prev is still NULL at that point, so you're performing a null-pointer dereference.
You can resolve that issue and also remove the special case for the list head by introducing a synthetic head node in front of the real nodes:
ListNode* reverseListInPairs(ListNode *head) {
ListNode fake_head = { .next = head };
ListNode *prev = &fake_head;
ListNode *current = head;
while (current != NULL && current->next != NULL) {
ListNode *temp = current->next;
current->next = current->next->next;
temp->next = current;
prev->next = temp;
prev = current;
current = current->next;
}
return fake_head.next;
}
I've stuck as close as possible to your original code there, but personally, I'd tighten it up a little further. In particular, you don't need to maintain both current and prev across iterations; just the latter would be sufficient.
ListNode* reverseListInPairs(ListNode *head) {
ListNode fake_head = { .next = head };
ListNode *prev = &fake_head;
while (prev->next && prev->next->next) {
ListNode *first = prev->next;
ListNode *second = first->next;
prev->next = second;
first->next = second->next;
second->next = first;
prev = first;
}
return fake_head.next;
}
Node *reverse (Node *head, int k)
{
Node* current = head;
Node* next = NULL;
Node* prev = NULL;
int count = 0;
while (current != NULL && count < k)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
if (next != NULL)
head->next = reverse(next, k);
return prev;
}
pass the value of k is 2
void pairWiseSwap(struct Node* head)
{
struct Node* temp = head;
/* Traverse further only if there are at-least two nodes left */
while (temp != NULL && temp->next != NULL) {
/* Swap data of node with its next node's data */
swap(&temp->data, &temp->next->data);
/* Move temp by 2 for the next pair */
temp = temp->next->next;
}
}
Sourced from GeeksForGeeks.
As for what is the error, it has been pointed out that prev is already NULL.
ListNode *current = head,*newHead = NULL,*temp = NULL,*prev = NULL;
.
.
prev->next = temp;
We can't have NULL->next as it will throw a segmentation fault.
The last printf call (printf("%d\n", current->val);) will not be executed. After the 1st printf functions' results appear, I am given the error "program.exe has stopped working". I would appreciate some help.
#include <stdio.h>
typedef struct node
{
int val;
struct node * next;
} node_t;
void print_list(node_t * head);
void main()
{
node_t * head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL)
return 1;
head->val = 3;
head->next = malloc(sizeof(node_t));
head->next->val = 2;
head->next->next = malloc(sizeof(node_t));
head->next->next->val = 3;
head->next->next->next = malloc(sizeof(node_t));
head->next->next->next->val = 18;
print_list(head);
head->next->next->next->next = malloc(sizeof(node_t));
head->next->next->next->next->val = 5556;
head->next->next->next->next->next = NULL;
node_t * current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = malloc(sizeof(node_t));
current->next->val = 32;
current->next->next = NULL;
printf("%d", current->next->val);
system("pause");
}
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d\n", current->val);
current = current->next;
}
}
This won't go well:
head->next->next->next = malloc(sizeof(node_t));
head->next->next->next->val = 18;
print_list(head);
You never initialized head->next->next->next->next to NULL. Instead, use calloc, or explicitly set the value to NULL. Even better, write a function to create a new node so you never forget to initialize. Even better still, write a function to insert a node.
What about something like this:
node_t * create_node( int val ) {
node_t *node = malloc(sizeof(*node));
if( node ) {
node->val = val;
node->next = NULL;
}
return node;
}
node_t * insert_value( node_t *list, int value ) {
node_t *new_node = create_node( value );
if( !new_node ) {
return list;
} else if( list ) {
new_node->next = list->next;
list->next = new_node;
}
return new_node;
}
Then you can:
node_t *head = insert_value( NULL, 3 );
node_t *tail = head;
tail = insert_value( tail, 2 );
tail = insert_value( tail, 3 );
tail = insert_value( tail, 18 );
print_list( head );
A common approach for lists is to use a dummy head node, which you never print out. It only contains a next pointer which is the start of your list and you ignore the value. If you did this, then you could also use that insert_value function to insert a value before the first element of the list. You also get the benefit that head always manages your entire list and you never have to worry about it changing.
I'm having trouble inserting in a Binary Search Tree using for loop, when I call the InorderTraversal function, there is no output all I get is a blank line, as far as I think rest of the code is okay the only problem is in the insert function.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct BinaryTree{
int data;
struct BinaryTree *left;
struct BinaryTree *right;
} node;
node* Insert(node* head, int value)
{
_Bool flag = true;
for(node *temp = head; flag == true; (temp = (value >= temp->data)?(temp->right):(temp->left)))
{
if(temp == NULL)
{
temp = (node*)malloc(sizeof(node*));
temp->data = value;
temp->left = NULL;
temp->right = NULL;
flag = false;
}
}
return head;
}
void InorderTraversal(node* head)
{
if(head == NULL)
{
return;
}
InorderTraversal(head->left);
printf("%d ",head->data);
InorderTraversal(head->right);
}
int main(void)
{
node *head = NULL;
for(int i = 0; i < 40; i++)
{
head = Insert(head,i);
}
InorderTraversal(head);
return 0;
}
Here try these changes in your Insert function
node* Insert(node *head, int value)
{
if(!head) //Explicitly insert into head if it is NULL
{
head = malloc(sizeof *head);
head->data = value;
head->left = NULL;
head->right = NULL;
return head;
}
for(node *temp = head,*temp2 = head; ;(temp = (value >= temp->data)?(temp->right):(temp->left)))
{
if(temp == NULL)
{
temp = malloc(sizeof *temp);
temp->data = value;
temp->left = NULL;
temp->right = NULL;
if(value >= temp2->data) //update previous nodes left or right pointer accordingly
temp2->right = temp;
else
temp2->left = temp;
break;
}
temp2 = temp; //Use a another pointer to store previous value of node
}
return head;
}
Call me crazy, but shouldn't that malloc(sizeof(node*)) be malloc(sizeof node)?
I am not that so informed, other than being able to read C, so excuse me if this is simply wrong...
Edit: ... or malloc(sizeof * temp)
When you insert the first node you dereference an uninitialized pointer here:
temp->data
Where temp is head and head in uninitialized and pointing to NULL.
So you first have to make special case when head is NULL:
if( !head )
{
head = malloc(sizeof(node));
head->data = value;
head->left = NULL;
head->right = NULL;
return head ;
}
When you continue adding elements you don't update the pointer of the last node. Your for loop should have an extra pointer to the previous node and when you get to the last node and find NULL update the previous nodes left or right pointer.
if(temp == NULL) //wrong, should be: not equal
{
temp = (node*)malloc(sizeof(node*)); //wrong, should be: sizeof the node not the pointer
temp->data = value;
temp->left = NULL;
temp->right = NULL;
flag = false; //use break instead
}
here the previous node pointer left or right is not updated and when you search you can't find any node.