Removing Node from LinkedList - c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct _listnode
{
int item;
struct _listnode *next;
} ListNode;
int removeNode(ListNode **ptrHead, int index);
void printList(ListNode *head);
ListNode * findNode(ListNode *head, int index);
int main()
{
ListNode *head = NULL, *temp=NULL;
int i = 0;
int index = 0;
while (1)
{
printf("Enter a integer: ");
scanf("%d", &i);
if (i == -1)
break;
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
}
removeNode(&head, index);
return 0;
}
void printList(ListNode *head)
{
int i = 0;
if (head == NULL)
return;
while (head != NULL)
{
printf("%d ", head->item);
head = head->next;
}
printf("\n");
}
ListNode * findNode(ListNode *head, int index)
{
if (head == NULL || index < 0)
return NULL;
while (index > 0){
head = head->next;
if (head == NULL)
return NULL;
index--;
}
return head;
}
int removeNode(ListNode **ptrHead, int index)
{
ListNode *pre, *cur,*temp;
if (index >= 0)
{
printf("Enter index to remove: ");
scanf("%d", &index);
if ((pre = findNode(*ptrHead, index - 1)) != NULL)
{
cur = pre->next;
temp = cur;
pre->next = cur->next;
free(temp);
printList(*ptrHead);
}
}
return -1;
}
I successfully revamp my code and now I am able to remove the node and display out, but the whole program just crash after my printList function. It does not go back to remove node and I cant continue removing other indexes.
Output:
Enter a value: 2
Enter a value: 4
Enter a value: 6
Enter a value: 8
Enter a value: -1
Enter index to remove: 2
Current list: 2 4 8
Enter index to remove: 0
Current list: 4 8
Enter index to remove: -1

when you enter index = 0, the inner if block of removenode function will not be executed as the findnode functin returns NULL. But in your output for index = 0 you got node 2 removed. How did u get that?

removeNode(head, index); should be removeNode(&head, index);
And printList(ptrHead); should be printList(*ptrHead); (in removeNode) I have the feeling this piece of code goes crazy that's why your app is not responding anymore.
What compiler do you use? It should have warned you.

In your updated code:
You still do not set next to NULL.
You never increment index when you fill list.
You do not check if scanf() succeeds.
There is no need to use pointer to pointer in removeNode(), you can use:
int removeNode(ListNode *ptrHead, int index);
You have added removeNode() in printList() which is really out of place.
You do not have any free function for the list.
...
Original answer to original code:
Missing header file.
Miss match between function signatures in declaration and definition.
You never set next to NULL.
You do not free list before exit.
...
Code with some comments:
/* MISSING: <stdio.h> */
#include <stdlib.h>
typedef struct _listnode
{
int item;
struct _listnode *next;
} ListNode;
/* Signature miss-match */
int removeNode(ListNode **ptrHead, int index);
void printList(ListNode *head);
ListNode *findNode(ListNode *head, int index);
int main()
{
ListNode *head = NULL, *temp=NULL;
int i = 0;
int index = 0;
while (i != -1)
{
printf("Enter a integer: ");
scanf("%d", &i);
/* If -1 entered, -1 will be added to list. */
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
/* temp->next never set to NULL */
}
/* Miss match between function signature and call. */
removeNode(head, index);
/* No freeing of list before exit. */
return 0;
}
void printList(ListNode *head)
{
/* Redundant check of head != NULL */
if (head == NULL)
return;
while (head != NULL)
{
printf("%d", head->item);
head = head->next;
}
}
ListNode *findNode(ListNode *head, int index)
{
if (head == NULL || index < 0)
return NULL;
while (index > 0){
head = head->next;
if (head == NULL)
return NULL;
index--;
}
return head;
}
/* Why pass index as argument when it is not used? */
int removeNode(ListNode **ptrHead, int index)
{
ListNode *pre, *cur,*temp;
printf("Enter index to remove: ");
scanf("%d", &index);
/* Here you should check < 0, not != -1.
What if user enters -9999 ? */
if (index != -1)
{
if ((pre = findNode(*ptrHead, index - 1)) != NULL)
{
cur = pre->next;
temp = cur;
pre->next = cur->next;
free(temp);
/* Bad return statement, should be int */
return;
}
/* You only print list if none was removed. */
/* Miss match between function signature and call. */
printList(ptrHead);
}
return -1;
}

Related

Why is function exiting if the initial input is -1?

#include <stdio.h>
#include <stdlib.h>
/*EVERYTHING WORKS EXCEPT FOR INITIAL -1*/
// Implementing a Node Structure - This represents a node of data
typedef struct _listnode
{
int item; // Data
struct _listnode *next; // Linkage
} ListNode;
// Core Functions of a Linked List
void printList(ListNode *head);
ListNode* findNode(ListNode *head, int index);
int insertNode(ListNode **ptrHead, int index, int value);
void removeNode(ListNode **ptrHead, int index);
int main()
{
// Instantiate a Linked List
ListNode *head = NULL, *temp;
/*
head is a pointer variable that will eventually point to the firstNode.
temp is a pointer variable that points to the lastNode. This is used in from Linked List functions.
*/
int num_to_store;
printf("Enter an Integer to Store (-1 to end):\n");
scanf("%d", &num_to_store);
while (num_to_store != -1)
{
// Initialize a Linked List
if (head == NULL)
{
head = malloc(sizeof(ListNode));
temp = head;
}
else
{
temp->next = malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = num_to_store;
printf("Enter an Integer to Store (-1 to end):\n");
scanf("%d", &num_to_store);
}
temp->next = NULL;
// Menu-Driven Application
int user_choice, index, value, *p, option_3;
puts("");
printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
scanf("%d", &user_choice);
while (user_choice != -1)
{
switch(user_choice)
{
case 1:
printList(head);
break;
case 2:
printf("Enter index to search:\n");
scanf("%d", &index);
p = findNode(head, index);
if (p != NULL) printf("Node Item: %d\n", *p);
break;
case 3:
printf("Enter index to insert:\n");
scanf("%d", &index);
printf("Enter value to insert:\n");
scanf("%d", &value);
option_3 = insertNode(&head, index, value);
if (option_3 == 0) printf("NODE INSERTED!\n");
break;
case 4:
printf("Enter index to remove:\n");
scanf("%d", &index);
removeNode(&head, index);
break;
}
// Prompt User to Make Another Selection
puts("");
printf("----------------\n 1: printList()\n 2: findNode()\n 3: insertNode()\n 4: removeNode()\n-1: End\n----------------\n");
scanf("%d", &user_choice);
}
return 0;
}
void printList(ListNode *head)
{
// Linked List is Empty
if (head == NULL)
{
printf("LINKED LIST IS EMPTY!\n");
}
// Linked List is Not Empty
else
{
printf("LINKED LIST: ");
while (head != NULL)
{
printf("%d ", head->item);
head = head->next;
}
puts("");
}
}
ListNode* findNode(ListNode *head, int index)
{
// Linked List is Empty or Index is Invalid
if (head == NULL || index < 0)
{
printf("INVALID!\n");
return NULL;
}
// Linked List is not Empty, Check if Index > len(Linked List)
else
{
while (index > 0)
{
head = head->next;
if (head == NULL)
{
printf("INVALID!\n");
return NULL;
}
index--;
}
printf("INDEX FOUND!\n");
return head;
}
}
int insertNode(ListNode **ptrHead, int index, int value)
{
ListNode *cur, *pre;
// Linked List is Empty || Insert at Index 0
if ((*ptrHead) == NULL || index == 0)
{
cur = *ptrHead;
*ptrHead = malloc(sizeof(ListNode));
(*ptrHead)->item = value;
(*ptrHead)->next = cur;
return 0;
}
// Insert in the Middle
else
{
pre = findNode(*ptrHead, index-1);
if (pre != NULL)
{
cur = pre->next; // This is temporary
pre->next = malloc(sizeof(ListNode)); // L connects to nN
pre->next->item = value;
pre->next->next = cur; // nN connects to R
}
return 0;
}
return -1;
}
void removeNode(ListNode **ptrHead, int index)
{
ListNode *cur, *pre;
// Linked List is Empty
if ((*ptrHead == 0) || index < 0)
{
printf("INVALID!\n");
}
// Remove firstNode
else if (index == 0)
{
cur = findNode(*ptrHead, index);
*ptrHead = cur->next;
free(cur); // Unallocate the memory
}
// Remove Middle
else
{
pre = findNode(*ptrHead, index-1);
cur = findNode(*ptrHead, index);
pre->next = cur->next;
cur->next = NULL;
}
}
Hi all, I am writing a Linked List program in C with the 4 core functions. The functions and everything else works except for when I compile the code and type -1. The program exits. I wonder what is wrong. I tried debugging on code::blocks but nothing showed up.
What am I trying to achieve?
Create a Linked List with no nodes.
Code the 4 core functions of a Linked List (insert/remove/print/search).
What have I tried?
Scattering printf() statements to debug.
Checked on stackoverflow for similar problems.
Viewed the code on geeksforgeeks.
Any idea the cause of the problem might be? And any possible solutions for this type of problem?
When you type -1, the whole while (num_to_store != -1) loop will be skipped, and temp will remain uninitialized, that will make temp->next = NULL; invoke undefined behavior.

Linked List making segmentation fault

Below I have made a simple Linked List in C. The code is currently producing a segmentation fault which I find odd because I was copying an example from our current book. The only thing I did to the code was put the code into the method "addToList". I'm aware the segmentation fault is coming from the method addToList but I do not know where I made a mistake.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} Node;
void addToList(Node *, int);
void printList(Node *);
void main() {
int x;
Node *head = malloc(sizeof(Node));
for (x = 1; x < 4); x++) {
printf("Enter an integer: ");
x = scanf("%d");
addToList(head, x);
}
printList(head);
}
void addToList(Node *head, int val) {
Node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof(Node));
current->next->val = val;
current->next->next = NULL;
}
void printList(Node *head) {
Node *current = head;
while (current != NULL) {
printf("%d->", current->val);
current = current->next;
}
printf("\n");
}
Any help with telling me what is wrong or where I'm making the mistake would be greatly appreciated.
Look carefully at your code:
int main(void) {
int x;
Node *head = malloc(sizeof(Node));
for (x = 1; x < 4); x++) {
...
addToList(head, x);
}
...
}
You are not initializing the memory, so head->val and head->next are not
initialized. Because of that
while (current->next != NULL) {
current = current->next;
}
will loop an undefined amount of times. The first current->next is most
probably not NULL, so current = current->next get executed. At that point current is pointing to nowhere, hence the undefined behaviour which in your case leads to a segfault.
You have to initialized the memory like this:
Node *head = malloc(sizeof *head);
if(head == NULL)
// error handling
head->next = NULL;
But you could also use calloc, which also sets the memory to 0, thus you don't have to initialize the values (in this case):
Node *head = calloc(1, sizeof *head);
if(head == NULL)
// error handling
You should always check for the return value of malloc/calloc/realloc.
Also note that the signature of the main function can be one of these:
int main(void);
int main(int argc, char **argv);
int main(int argc, char *argv[]);
edit
Another error I've noticed right now:
x = scanf("%d");
That's not how scanf works. You have to pass a pointer, scanf saves the
scanned value through the passed pointer. scanf returns the number of matched
values on success, in this case, success would be 1:
int num;
int ret = scanf("%d", &num);
if(ret != 1)
{
fprintf(stderr, "Could not read value from the user\n");
continue; // to contiune looping
// you could also do a break; and stop the looping, or
// exit(1), etc.
}
// error with scanf
Also don't use the same variable x for the loop iteration and user input,
otherwise you are messing with the loop.
edit
User user3629249 wrote in the comment
good information, however the result will be the first entry in the linked list will contain garbage.
Better to declare head via: Node *head = NULL; and the function addToList() check for NULL and proceed accordingly.
That's right, the head element doesn't save any number in this way.
Option 1: double pointer
Here addToList receives a double pointer. The initialization of head occurs
when *head points to NULL. The function allocates memory for it, initializes
the memory, saves the value and returns. In the concurrent calls of addToList
*head won't be NULL, so addToList looks for the end of the list.
I've made small changes in the way you do malloc and realloc. Also I added
an implementation of freeList which should be used to free the memory:
void addToList(Node **head, int val) {
if(head == NULL)
{
fprintf(stderr, "head cannot be NULL\n");
return;
}
if(*head == NULL)
{
*head = calloc(1, sizeof **head);
head[0]->val = val;
head[0]->next = NULL;
return;
}
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof *current->next);
if(current->next == NULL)
return;
current->next->val = val;
current->next->next = NULL;
}
int main(void)
{
int x;
Node *head = NULL;
for (x = 1; x < 4; x++)
{
int val;
printf("Enter an integer: ");
if(scanf("%d", &val) != 1)
{
fprintf(stderr, "Could not read from user. Skipping entry\n");
continue;
}
addToList(&head, val);
}
printList(head);
freeList(head);
return 0;
}
void freeList(Node *head)
{
if(head == NULL)
return;
Node *current = head;
Node *next;
while(next = current->next)
{
free(current);
current = next;
}
free(current); // the last one
free(head);
}
Option 2: addToList returns a pointer to the head
Here addToList takes a pointer to the head. If it's NULL, it allocates
memory and initializes like in the shown above. If head is not NULL, the
functions looks for the last element and the returns the head. On error the
function returns NULL.
Node *addToList(Node *head, int val) {
if(head == NULL)
{
head = calloc(1, sizeof **head);
head->val = val;
head->next = NULL;
return head;
}
Node *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = malloc(sizeof *current->next);
if(current->next == NULL)
return NULL;
current->next->val = val;
current->next->next = NULL;
return head;
}
int main(void)
{
int x;
Node *head = NULL, *tmp;
for (x = 1; x < 4; x++)
{
int val;
printf("Enter an integer: ");
if(scanf("%d", &val) != 1)
{
fprintf(stderr, "Could not read from user. Skipping entry\n");
continue;
}
tmp = addToList(head, val);
if(tmp == NULL)
{
fprintf(stderr, "Not enough memory\n");
freeList(head);
return 1;
}
head = tmp;
}
printList(head);
freeList(head);
return 0;
}

Reversing an array of linked list recursively isn't reversing all nodes in order

I have an array of linked list which I am trying to reverse recursively. When I call the function to reverse it will not reverse all of the nodes but rather a couple of the nodes.
The reverse function appears to be deleting the first node (base case) and filling its spot with the last node (end of sub case). I think that the problem lies in the calling of the for loop within the reverse_nodes function however that doesn't seem to fix it.
Here is some output..
pre-reverse function:
-----
group 0
alice, 2
-----
group 1
martin, 4
-----
group 2
keanu, 6
-----
group 3
miles, 8
post - reverse function
-----
group 0
miles, 8
-----
group 1
martin, 4
-----
group 2
keanu, 6
-----
group 3
miles, 8
I am trying to get it to reverse to that it reads: 8,6,4,2
Please note I have only included relevant code blocks such as the struct architecture, the head/tail construction, the deletion of all nodes prior to reading in the binary file, reading binary file to nodes, and the main function. Can I get some help figuring out what in the reverse function is causing it to not completely reverse? Thank for your time. See code below!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head[4]={NULL,NULL,NULL,NULL};
node* tail[4]={NULL,NULL,NULL,NULL};
void wipe_nodes()
{
int i;
node *p=NULL;
for(i=4; i>0; i--)
{
p=head[i];
if(p == NULL)
{
printf("Nodes are clear!\n");
}
while(p != NULL)
{
delete_party(p->name, p->size); // cant call name and size
p = p -> next;
}
}
}
void bin_to_list(char *filename)
{
FILE *fp;
int ret;
fp = fopen(filename, "rb");
if (fp == NULL)
{
printf("Null file!\n");
return;
}
node temp;
//temp = (node *)malloc(sizeof(node));
while((ret = fread(&temp, sizeof(node), 1, fp) > 0))
{
printf("%s %d", temp.name, temp.size);
if(temp.size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(temp.size >= 1 && temp.size <= 2)
{
add_party(0, temp.name, temp.size);
}
else if(temp.size >= 3 && temp.size <= 4)
{
add_party(1, temp.name, temp.size);
}
else if(temp.size >= 5 && temp.size <= 6)
{
add_party(2, temp.name, temp.size);
}
else if(temp.size >= 7)
{
add_party(3, temp.name, temp.size);
}
}
fclose(fp);
return;
}
void reverse_nodes(node *p, node *q)
{
int i;
for(i=0; i<4; i++)
{
//node *p=head[i];
if(p == NULL)
{
printf("Error, no nodes!\n");
return;
}
if (p->next == NULL)
{
printf("LOL, only one node! Can't reverse!\n");
head[i] = p;
}
else
{
reverse_nodes(p->next, p);
}
p->next = q;
return;
int main(int argc, char *argv[])
{
int x, i;
read_to_list(argv[1]);
bin_to_list(argv[2]);
while (1)
{
fflush(stdin);
printf("\n\nEnter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to change party size.\nEnter 5 to quit (write to .txt file).\nEnter 6 to read from bin file.\nEnter 7 to reverse the list.\n\n");
scanf("%d",&x);
char name[20];
int size;
switch(x)
{
case 1:
printf("\nParty Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(size >= 1 && size <= 2)
{
add_party(0, name, size);
}
else if(size >= 3 && size <= 4)
{
add_party(1, name, size);
}
else if(size >= 5 && size <= 6)
{
add_party(2, name, size);
}
else if(size >= 7)
{
add_party(3, name, size);
}
break;
case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
delete_party(NULL, size);
break;
case 3:
list_parties();
break;
case 4:
change_partysize(name, size);
break;
case 5:
write_to_file(argv[1]);
write_to_bin(argv[2]);
exit(0);
break;
case 6:
wipe_nodes();
bin_to_list(argv[2]);
break;
case 7:
for(i=0; i<4; i++)
{
reverse_nodes(head[i], NULL);
}
break;
default:
continue;
}
}
}
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
/* Function to reverse the linked list */
static void reverse(struct Node** head_ref)
{
struct Node* prev = NULL;
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 85);
printf("Given linked list\n");
printList(head);
reverse(&head);
printf("\nReversed Linked list \n");
printList(head);
getchar();
}
Your reverse_nodes function is just setting the next member of the node, not the current node, so when it gets to the end of the list, it will set the next of the last member to the previous member in the linked list but leave the last member untouched.
This worked for me:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head;
void reverse_nodes(node ** pphead)
{
node *prev = NULL;
node *current = *pphead;
node *next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*pphead = prev;
}
void main(void)
{
int i;
node * phead;
head = phead = (node *)malloc(sizeof(node));
phead->size = 0;
for (i = 0; i < 4; i++)
{
node * p;
phead->next = (node *)malloc(sizeof(node));
phead = phead->next;
phead->size = i + 1;
phead->next = NULL;
}
reverse_nodes(&head);
phead = head;
while (phead)
{
printf("%d\r\n", phead->size);
phead = phead->next;
}
}
Edit:
Recursion is usually a bad idea as you could end up blowing the stack - it's always best to try and do what you need to do in a single (or multiple if required) function(s) if possible, which in this case is easily possible.

Linked list not adding node until second iteration

I'm working on a FIFO page replacement algorithm and have it almost working. My code uses scanf() to read in a page number then it adds that item to a linked list, up to 16 pages. However, if the page already exists in the lined list, it does not add the page to this list again. There are three page frames (slots). Everything works properly, but, it does not add that item to the list until scanf() reads another integer to add to the list. I am utterly confused.
#include<stdio.h>
#include<stdlib.h>
typedef struct node {
int num;
struct node * next;
} node_t;
void print_list(node_t * head) {
node_t * current = head;
while (current != NULL) {
printf("%d\n", current->num);
current = current->next;
}
}
int fault(node_t * head, int check) {
node_t * current = head;
while(current->next != NULL){
if(current->num == check)
return 0;
current = current->next;
}
return 1;
}
void addNode(node_t * head, int num) {
node_t * current = head;
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = malloc(sizeof(node_t));
current->next->num = num;
current->next->next = NULL;
}
int pop(node_t ** head) {
int retnum = -1;
node_t * nextNode = NULL;
if (*head == NULL) {
return -1;
}
nextNode = (*head)->next;
retnum = (*head)->num;
free(*head);
*head = nextNode;
return retnum;
}
///////////////////////////////////////////////////////
int main(){
int num;
int faults = 0;
int n = 0;
int j = 0;
node_t * head = malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->num = -1;
printf("Input page number up to 16 pages. Enter 'q' to quit.\n");
for(j=0;j<16;j++){
if(scanf("%d\n",&num) == 1) {
if (fault(head, num) == 1 && n < 3) {
n++;
faults++;
addNode(head, num);
}
else if (fault(head, num) == 1 && n >= 3) {
pop(&head);
addNode(head,num);
faults++;
}
}
else{
int c = getchar();
if( c == 'q' ){
break;
}
}
if (n == 1 && faults == 1)
pop(&head);
printf("\nPage Table:\n");
print_list(head);
printf("\nInput page number: \n");
}
printf("Number of page faults: %d\n", faults);
}
I ran it through gdb and it doesn't even call the addNode function until the second integer has been scanned.
(And yes I know scanf is garbage, I just didn't want to bother learning how to do something else)
Thanks for the help!
Your code:
if(scanf("%d\n",&num) == 1) {
should be :
if(scanf("%d",&num) == 1) {
And head needs to be initialized.
node_t * head = malloc(sizeof(node_t));
if (head == NULL) {
return 1;
}
head->next = NULL;

C Programming Listnode insert node resulting in infinite loop

I was having some problem when trying to do a double deferencing when inserting node into ListNode. Here is the code:
#include "stdafx.h"
#include <stdlib.h>
typedef struct _listnode {
int num;
struct _listnode *next;
}ListNode;
void insertNode(ListNode **ptrHead, int index, int value);
ListNode *findNode(ListNode *head, int index);
int main()
{
int index, value, i;
ListNode **ptrHead, *head = NULL;
ptrHead = &head;
for (i = 0; i < 5; i++){
printf("Enter value: ");
scanf("%d", &value);
printf("Enter index: ");
scanf("%d", &index);
insertNode(ptrHead, index, value);
}
ptrHead = head;
while (ptrHead != NULL) {
printf("%d", head->num);
ptrHead = head->next;
}
return 0;
}
void insertNode(ListNode **ptrHead, int index, int value) {
ListNode *cur, *newNode;
if (*ptrHead == NULL || index == 0) {
newNode = malloc(sizeof(ListNode));
newNode->num = value;
newNode->next = *ptrHead;
*ptrHead = newNode;
}
else if ((cur = findNode(*ptrHead, index - 1)) != NULL) {
newNode = malloc(sizeof(ListNode));
newNode->num = value;
newNode->next = cur->next;
cur->next = newNode;
}
else printf("Cannot insert the new item at index %d!\n", index);
}
ListNode *findNode(ListNode *head, int index) {
ListNode *cur = head;
if (head == NULL || index < 0)
return NULL;
while (index > 0) {
cur = cur->next;
if (cur == NULL) return NULL;
index--;
}
return cur;
}
So basically I am taking 5 value and index inputs from the user. Then from there, I am inserting them into the ListNode. Inside insertNode(), there is a function called findNode which trying to find the cur so that I can point my cur to the next newNode.
However, with these code, when I try to print out the ListNode, it printed out the first value input infinitely. So I was thinking which part was my mistakes?
Thanks in advance.
In your main function, the following lines of code:
ptrHead = head;
while (ptrHead != NULL) {
printf("%d", head->num);
ptrHead = head->next;
}
shall be:
ListNode *cur = head;
while (cur != NULL) {
printf("%d", cur->num);
cur = cur->next;
}
EDITED:
But can I know why it does not work when I assign head to ptrHead. Is it because I am double deferencing the ptrHead?
They are of different types. ptrHead is ListNode** while head is ListNode*. Thus the assignment ptrHead = head; shall not do what you really want. Also a modern compiler shall have raised some warning on this line.

Resources