Reputation: 59
This function doesn't work if I make a modification in while
condition and change temp != NULL
to temp->next = NULL
. Why is that?
void Print() {
printf("\n");
printf("The Library data is as follows: \n");
struct Node *temp = head; // where head is a global variable
printf("\n");
while (temp != NULL) {
printf("%25s", temp->name);
printf("%25s", temp->author);
temp = temp->next;
printf("\n");
}
}
Plus if I modify the while
loop condition under else
block, from temp1->next = NULL
to temp1 != NULL
, it doesn't work. Why is that?
void Insert(char q[50], char r[50]) {
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->next = NULL; // Since we are adding a node to the end, we are linking it to NULL.
strcpy(temp->name, q); // copying the contents of "q" to "temp->name"
strcpy(temp->author, r); // same
if (head == NULL) {
head = temp;
} else {
struct Node *temp1 = head;
while (temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp;
}
}
Upvotes: 0
Views: 6925
Reputation: 1
Its very simple
print()
when you use while(temp->next!=null)
then it will not print last
node because when pointer temp is at last node temp->next
is null and while condition
become false and it come out of while loop.insert()
if you change while(temp1->next!=NULL)
to while(temp1!=NULL)
then
when it come from while loop temp1 points to null means no memory is allocated .
hence writing temp1->next in after while loop shows error as you cannot excess next
part of temp because memory is not allocated
.Hope this will help you!
Upvotes: 0
Reputation: 144770
In the Print
function, you want to enumerate all nodes in the list, and you want to handle an empty list (head = NULL
) gracefully. Hence the loop iterates while (temp == NULL)
and temp
skips to the next node with temp = temp->next;
.
In the Insert
function, a new node is allocated and initialized. There is a special case for the empty list (head == NULL
) where head
just receives the pointer to the new node. Conversely, if the list is not empty, you want to find the last node of the list to append the new node by setting its next
pointer to point to the new node. The loop iterates from the first to the last node, the test temp1->next != NULL
succeeds if the node is not the last one. Hence temp
points to the last node after the while
loop and the new node can simply be appended with temp1->next = temp;
.
Note the following remarks:
void Insert(char q[50], char r[50])
should be improved as int Insert(const char *name, const char *author)
because:
strcpy
will cause undefined behavior if the arguments strings are too long.malloc
in C.Here is an improved version:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char name[50];
char author[50];
struct Node *next;
};
struct Node *head = NULL;
void Print(void) {
printf("\nThe Library data is as follows:\n\n");
struct Node *temp = head; // where head is a global variable
while (temp != NULL) {
printf("%25s %-25s\n", temp->name, temp->author);
temp = temp->next;
}
}
int Insert(const char *name, const char *author) {
struct Node *temp = malloc(sizeof(struct Node));
if (temp == NULL)
return -1;
temp->next = NULL;
snprintf(temp->name, sizeof(temp->name), "%s", name);
snprintf(temp->author, sizeof(temp->author), "%s", author);
if (head == NULL) {
head = temp;
} else {
struct Node *temp1 = head;
while (temp1->next != NULL)
temp1 = temp1->next;
temp1->next = temp;
}
return 0;
}
int main() {
if (Insert("Flowers for Algernon", "Daniel Keyes")
|| Insert("Blade Runner", "Philip K. Dick")
|| Insert("2001, a Space Odyssey", "Arthur C. Clarke")) {
printf("out of memory\n");
return 1;
}
Print();
return 0;
}
Upvotes: 2
Reputation: 3355
In the first case You are traversing through the entire linked list.End of linked list is denoted by NULL
pointer as per your code so while(temp!=NULL)
is used.This will work with temp1->next!=NULL
if there are more than 2 nodes by skipping the last one.
In second case you add new node to the end of the last node.So you have to use temp1->next!=NULL
. so that the last node's next
pointer can point to the newly added node.If you use temp!=NULL
in this case, Then temp becomes NULL
. So the next statement temp1->next=temp;
cannot work.
If you make change in first case, it will only cause program to skip printing the last node in the linked list.
If you make change in second case, this will cause your code to get
terminated (runtime error)
.
Upvotes: 0
Reputation: 636
When change that to node->next in while loop its mean this loop loops while node->next is null so u miss last node of your linked array because this node's-next is null so loop is break in this time.And only write temp to your while u dont need temp != NULL this loop breaks when temp is null so only put
while(temp){
//your code
}
I hope this will be help you. :-)
Upvotes: 1