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
}
Related
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 ;)
#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 found this on Internet to reverse a list using recursion and applied it in codeblocks but the output only reverse prints last two Insert call from main function. It skips the first three Insert calls. Why? I did search for this problem here but I failed to understand them as I'm a beginner. Kindly help
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
struct Node* Insert (struct Node* head, int data)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
struct Node* head = NULL;
head = Insert(head,2);
head = Insert(head,7);
head = Insert(head,3);
head = Insert(head,1);
head = Insert(head,4);
reversePrint(head);
return 0;
}
O/P : 4 1
NOTES:
Don't cast the return of value of malloc
You declared two *head and confused yourself
No need to pass pointer to function and return pointer when you have head declared as global. Which is not a good idea but I followed your code.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node * head;
void Insert (int data)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = temp;
return;
}
struct Node* temp2 = head;
while(temp2->next != NULL)
{
temp2 = temp2->next;
}
temp2->next = temp;
}
void reversePrint(struct Node* head)
{
if(head == NULL)
{
printf("\n");
return;
}
reversePrint(head->next);
printf(" %d ", head->data);
return;
}
int main()
{
Insert(2);
Insert(7);
Insert(3);
Insert(1);
Insert(4);
reversePrint(head);
return 0;
}
OUTPUT:
4 1 3 7 2
I got this code on codeblocks 13.12 in lubuntu 16.04
The program is running but the problem is that the first insertion is duplicated that is,
lets say i first insert integer "4" to the linked list. But i get the output as:
4 ,4 ,
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next != NULL) {
temp1 = temp1->next;
}
temp1 -> next = temp;
}
void print() {
struct node* temp = head;
printf("list is: \n");
while (temp != NULL) {
printf( "%d ,",temp->data);
temp = temp->next;
}
}
int main () {
printf("How Many Numbers?\n");
int a ,b ,c;
scanf("%d" , &b);
for(a = 0;a<b;a++) {
printf("Enter the numbers \n");
scanf("%d",&c);
Insert(c);
print();
}
return 0;
}
the problem is that the first insertion is duplicated
Reason:
You've mentioned if(head==NULL) correctly to check whether inserted node is first node or not but after this you did not mention else to restrict compiler.
So the compiler compiles both the if block and the code following it
So another node gets created after head with the same value c
that's the reason when you insert c=4, you get 4,4, as output
Solution
Try using your insert() with an else condition as
void Insert(int c)
{
if(head == NULL)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
}
else
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}
suggestion : you've mentioned this twice in you insert() function
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
just allocate temp once and then insert it at approprite position using if-else conditions. This reduces the number of lines of code too. do it this way:
void Insert(int c)
{
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
if(head == NULL)
{
head = temp;
}
else
{
struct node* temp1 = head;
while (temp1->next!= NULL)
temp1 = temp1->next;
temp1 -> next = temp;
}
}
That is because the Insert function "falls through" after initialising the first item, and inserts it again. Add return at the end of the code block.
void Insert(int c)
{
if(head == NULL) {
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
head = temp;
return; //<-- add this line
}
...
I want to create a linked list of numbers from 1 to 1000 and print the numbers.
Im using the function createList() to create the list and printList() to print the elements.
But the following code is crashing.
Can anybody please rectify. I'm new to linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* head;
void deleteNode()
{
}
void createList()
{
int i;
struct node* temp = (struct node*)malloc(sizeof(struct node));
head = temp;
struct node* temp1 = (struct node*)malloc(sizeof(struct node));
for(i=0;i<10;i++)
{
temp->data = i+1;
temp->link = temp1;
temp1->link = temp++;
temp1++;
}
}
void printList()
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp = head;
while(temp != NULL)
{
printf("%d ", temp->data);
temp = temp->link;
}
}
int main()
{
head = NULL;
createList();
printList();
return 0;
}
void createList(){
int i, size = 10;
struct node* temp = malloc(sizeof(struct node));
head = temp;
for(i=0;i<size;i++){
temp->data = i+1;
temp->link = i < size - 1 ? malloc(sizeof(struct node)) : NULL;
temp = temp->link;
}
}
void createList(){
int i, size = 10;
struct node* temp = malloc(size*sizeof(struct node));
head = temp;
if(temp){
for(i=0;i<size;i++){
temp->data = i+1;
temp->link = temp + 1;
++temp;
}
temp[-1].link = NULL;
}
}
void createList()
{
int i;
struct node *temp, *loc_head;
loc_head = head;
for(i=0;i<10;i++)
{
struct node* newnode = malloc(sizeof(struct node));
newnode->data = i+1;
newnode->link = NULL;
if(head == NULL) {
head=newnode;
loc_head = newnode;
}
else {
head->link = newnode;
head = newnode;
}
}
head = loc_head;
}
don't typecast the result of malloc
Given an array of elements, create a linked list from the array (one new node per
element, using the function that adds nodes to the end of a list).