This is a simple program to add elements at the end of the Linklist using C.
void main() {
int i, n, x;
struct Node* Head = NULL;
struct Node* temp1;
printf("Enter the no. of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for (i = 0; i < n; i++) {
temp1 = Head;
struct Node* temp = (Node*)malloc(sizeof(struct Node));
scanf("%d", &x);
temp->data = x;
temp->next = NULL;
if (Head != NULL) {
while (temp1 != NULL) // This part is not working properly
temp1 = temp1->next;
temp1->next=temp;
} else {
Head = temp;
}
}
temp1 = Head;
while (temp != NULL) {
printf("temp=%d tempdata=%d \n",temp,temp->data);
temp = temp->next;
}
}
The while part is not linking the new elements with the previous elements.
As #Groo pointed out, temp1 is null at the end of the while loop so you cannot call temp1->next.
So Just replace the line with
while(temp1->next!=NULL)
But you don't have to traverse all the list elements every time you do an insert to the linked list. As the temp1 would be pointing to the last element in every iteration you just have to make it next pointer to point to the newly allocated node. As done in the below code.
#include<stdlib.h>
#include<stdio.h>
struct Node{
int data;
struct Node *next;
};
void main(){
int i,n,x;
struct Node* Head=NULL;
struct Node* temp1;
printf("Enter the no. of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++){
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&x);
temp->data=x;
temp->next=NULL;
if(Head!=NULL){
temp1->next=temp;
temp1 = temp;
}
else {
Head=temp;
temp1 = Head;
}
}
temp1=Head;
while(temp1!=NULL){
printf("temp=%d tempdata=%d \n",temp1,temp1->data);
temp1=temp1->next;
}
}
Changing
while(temp1!=NULL)
temp1=temp1->next;
to
while(temp1!=NULL && temp1->next != null)
temp1=temp1->next;
should fix this ;)
Related
After I enter numbers in the terminal, the program gets stuck at the free(temp) command line as shown.
https://i.stack.imgur.com/BycHf.png
The program does not work properly until the "Continue" button is clicked twice.
https://i.stack.imgur.com/RDO2b.png
https://i.stack.imgur.com/7gqzN.png
The source code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head;
void Insert(int n){
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = n;
temp->next = head;
head = temp;
}
void Delete(int z){
struct Node* temp = head;
if(z == 1){
head = temp->next;
free(temp);
return;
}
int i;
for(i=0;i<z-2;i++){
temp = temp->next;
}
struct Node* temp1;
temp1 = temp->next;
temp->next = temp1->next;
free(temp);
}
void print(){
printf("List is:");
int x;
struct Node* temp1;
temp1 = head;
while(temp1 != NULL){
x = temp1->data;
printf("%d ",x);
temp1 = temp1->next;
}
printf("\n");
}
int main()
{
head = NULL;
int k;
Insert(2);
Insert(5);
Insert(4);
Insert(7);
Insert(0);
Insert(8);
Insert(5);
Insert(6);
print();
printf("you want to delete:");
scanf("%d",&k);
Delete(k);
print();
system("pause");
return 0;
}
I am trying to reverse a linked by iterative method. Magically, after watching tutorial and trying to recode myself, the program works successfully. However, when I review the code, I hit a question: in line 23, why we must use temp1->next instead of temp1? When traversing to the end of the linked list, which case we use the condition (the node != NULL)? In which case we use (the link of the node ! = NULL)? I fully appreciate it if anyone can enlighten me.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* Insert(struct Node* head, int data)
{
struct Node* temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
//If the list is empty
if (head == NULL)
{
head = temp;
}
else //The list is not empty
{
struct Node* temp1 = head;
while (temp1->next != NULL)
{
temp1 = temp1->next;
}
temp1->next = temp;
}
return head;
}
void Print(struct Node* head)
{
struct Node* temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
struct Node* Reverse(struct Node* head)
{
struct Node* *prev, *current, *next;
current = head;
prev = NULL;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
int main()
{
struct Node* head = NULL;
printf("Enter the length of the linked list you want to create: ");
int length;
scanf("%d", &length);
printf("Enter the value you want to input: ");
int i;
for (i = 0; i < length; i++)
{
int x;
scanf("%d", &x);
head = Insert(head, x);
}
printf("Given linked list\n");
Print(head);
head = Reverse(head);
printf("\nReversed linked list \n");
Print(head);
return 0;
}
In that case, inside the while condition, on line 23, you can notice that the program is using temp1 = temp1->next, if you change the condition to temp1 != NULL when it reaches the last element of linked list it'll collect trash from memory or even result in an error, because NULL don't have a next position.
So, you use temp1 != NULL if you are accessing the data inside the list and temp1->next != NULL if you are manipulating the next positions of the linked list.
Because temp1->next at the end of the linked list is NULL, however, the last node has data, if you use temp1 == NULL you would say that the last node is NULL, which is not the case. So you want to end the loop when the "pointer to the next node" is NULL, not when the next node is NULL.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* head;
void insertAtFront(int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
temp->next = head;
head = temp;
return ;
}
void displayAll()
{
struct Node* temp = head;
while(temp != NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
printf("\n");
}
void insertAtNthPostion(int data,int key)
{
int i;
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
struct Node* temp1 = head;
for(i=1;i<key;i++)
temp1 = temp1->next;
temp->next = temp1->next;
temp1=temp;
displayAll();
return;
}
int main()
{
int num, i, data, key;
head= NULL;
printf("\nHow many data u want to insert? ");
scanf("%d", &num);
for(i=0; i < num; i++)
{
printf("\nEnter the data u want to insert:");
scanf("%d", &data);
insertAtFront(data);
}
displayAll();
printf("Enter the data and position u want to insert:\n");
scanf("%d%d", &data, &key);
insertAtNthPostion(data,key);
return 0;
}
I have tried with this implementation but the values are not changing in the and my output appears as below:
Enter the data u want to insert:
5
5 3 2 1 2
Enter the data and position u want to insert:
265498
2
5 3 2 1 2
You needed to set the previous node's ->next to the new node.
You also forgot the curly brackets on your for loop {}.
You are also forgetting to delete the original node, this won't change the result, but it will waste memory..
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* head;
void insertAtFront(int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
temp->next = head;
head = temp;
return ;
}
void displayAll()
{
struct Node* temp = head;
while(temp != NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
printf("\n");
}
void insertAtNthPostion(int data,int key)
{
int i;
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
struct Node* current = head;
struct Node* previous = NULL;
struct Node* temp = NULL;
for(i=1;i<key;i++) {
previous = current;
current = current->next;
}
new_node->next = current->next;
if (previous != NULL)
previous->next = new_node;
else
head = new_node;
free(current); //Release the old node's memory
displayAll();
return;
}
int main()
{
int num, i, data, key;
head= NULL;
printf("\nHow many data u want to insert? ");
scanf("%d", &num);
for(i=0; i < num; i++)
{
printf("\nEnter the data u want to insert:");
scanf("%d", &data);
insertAtFront(data);
}
displayAll();
printf("Enter the data and position u want to insert:\n");
scanf("%d%d", &data, &key);
insertAtNthPostion(data,key);
return 0;
}
Better Solution:
Although a much better solution would be to change the data of the selected node, without creating a new one:
void insertAtNthPostion(int data,int key)
{
struct Node* current = head;
int i;
for(i=1;i<key;i++) {
current = current->next;
}
current->data = data;
displayAll();
}
Results:
The results with either method will give you this:
Enter the data u want to insert:3
3 2 1
Enter the data and position u want to insert:
4
1
4 2 1
I'm very new to programming and I started to learn C. Now I just cant understand why my node structure is not visible to my functions.
I try to get some help on http://www.cprogramming.com/tutorial/c/lesson7.html
but with no luck in using code blocks 13.12
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct ptr * next;
};
struct node* head;
void Insert(int x)
{
struct node *temp;
temp = (node*)malloc(sizeof(struct node));
if(head == NULL)
head = temp;
temp->data = x;
temp->data = x;
temp->next = NULL;
struct node* temp1 = head;
while(temp1-> != NULL;) {
temp1 = temp1->next;
}
temp1->next = temp;
}
void print() {
struct node* temp = head;
while(temp != NULL) {
printf("the data is %d", temp->data);
temp = temp->next;
}
}
int main ()
{
head = NULL;
int a,c;
printf("How many numbers ? : \n");
scanf("%d",&a);
for(i = 0; i<a; i++); {
printf("Enter a number:\n");
scanf("%d",&c);
Insert(c);
print();
}
}
There are quite a few lets go one by one
number 1
struct node {
int data;
struct node *next; // chagnge ptr -> node
};
number 2
struct node *temp;
temp = malloc(sizeof(struct node)); // struct node casting
number 3
while(temp1->next != NULL) { // remove semi colum and put a next
temp1 = temp1->next;
}
number 4
int i; // for while loop
for(i = 0; i<a; i++) {
Now hopefully it compiles well, check runtime errors ( if any )
and yes
return 0; // just before main
you are building an infinite loop with your first element:
temp = (struct node*)malloc(sizeof(struct node));
if(head == NULL)
head = temp;
temp->data = x;
temp->next = NULL;
struct node* temp1 = head;
while(temp1->next != NULL) { // nothing to do
temp1 = temp1->next;
}
temp1->next = temp; //temp is head and temp1 is head, so head->next points to head
you should do something like
if (head == NULL) {
//fill head and leave
} else {
//traverse to the last element and concatenate the new element
}
I am having some trouble adding integers to the end of my linked list. I am very new to C and had part of my program working properly (the push function). I want to return a pointer to a struct node, and I am not quite sure where I am going wrong in my append function.
~Thanks.
enter code here
//node.h
#ifndef NODE_H
#define NODE_H
struct node{
int val;
struct node *next;
};
int length(struct node *);
struct node* push(struct node *, int); //adds integer to front of list.
struct node* append(struct node *, int); //adds integer to back of list.
void print(struct node *, int);
#endif
//node.c
#include "./node.h"
#include<stdlib.h>
#include<stdio.h>
int length(struct node *current){
if(current->next != NULL)
return 1 + length(current->next);
else
return 1;
}
struct node* push(struct node *head, int num){
struct node *temp = malloc(sizeof(struct node));
temp->val = num;
temp->next = head;
head = temp;
temp = NULL;
return head;
}
struct node* append(struct node *current, int num){
if(current != NULL){
append(current->next, num);
}
else{
struct node* temp = malloc(sizeof(struct node));
temp->val = num;
temp->next = NULL;
current = temp;
return current;
}
}
void print(struct node* head, int size){
printf("The list is %i", size);
printf(" long \n");
struct node* temp;
temp = head;
while(temp != NULL){
printf("%d", temp->val);
printf(" ");
temp = temp->next;
}
printf(" \n");
}
//Main
#include "./node.h"
#include<stdlib.h>
#include<stdio.h>
int main(){
char ans[2];
int num;
struct node* head = NULL;
do{
printf("Enter a integer for linked list: ");
scanf("%d", &num);
head = append(head, num);
printf("Add another integer to linked list? (y or n) ");
scanf("%1s", ans);
}while(*ans == 'y');
print(head, length(head));
return 0;
}
I think what is missing is that the recursive part of the function needs to set current->next. This has the effect of setting every node's next pointer to what it was until you get to the end of the list, when it is set to the newly malloced node.
struct node* append(struct node *current, int num){
if(current != NULL){
current->next = append(current->next, num);
return current;
}
else {
struct node* temp = malloc(sizeof(struct node));
if (temp == NULL) abort();
temp->val = num;
temp->next = NULL;
return temp;
}
}