insert end linked list - c

I am having problem in inserting data at the end of the linked list. I have tried everything but no help. It is only showing first and the last no but the numbers in between.
Following is my code.
struct node
{
int data;
struct node *next;
} *start;
add()
{
int a,b,c=0;
scanf("%d",&a);
struct node *new,*new1;
new=(struct node *)malloc(sizeof(struct node));
new1=(struct node *)malloc(sizeof(struct node));
while(a!=0)
{
c=a%10;
a=a/10;
if(start==NULL)
{
new->data=c;
start=new;
printf("%d\n",start->data);
}
else
{
new1->data=c;
new->next=new1;
new=new->next;
}
}
new->next=NULL;
}

you are re-writing 2nd node every-time. move to the last of linked list and then add
add()
{
int a,b,c=0;
scanf("%d",&a);
struct node *new,*new1;
while(a!=0)
{
c=a%10;
a=a/10;
new=(struct node *)malloc(sizeof(struct node));
if(start==NULL)
{
new->data=c;
new->next = NULL;
start=new;
printf("%d\n",new->data);
}
else
{
new1=(struct node *)malloc(sizeof(struct node));
new1 = start;
while(new1->next != NULL) {
new1 = new1->next;
}
new->data=c;
new->next = NULL;
printf("%d\n",new->data);
}
}
}

void print(struct node *p){
while(p){
printf("%d\n", p->data);
p = p->next;
}
}
void add(void){
int a;
scanf("%d", &a);
struct node *new, *curr;
for( ;a!=0; a /= 10){
new = (struct node *)malloc(sizeof(struct node));
new->data = a % 10;
new->next = NULL;
if(start==NULL)
start = curr = new;
else
curr = curr->next = new;
}
print(start);
}

Solved it. The problem was that I was using the same new1 again and again. When the satisfying case appears it should be always allocated memory. The modified code is as below:-
int a,b,c=0;
scanf("%d",&a);
while(a!=0)
{
struct node *new,*new1;
c=a%10;
a=a/10;
if(start==NULL)
{
new=(struct node *)malloc(sizeof(struct node));
new->data=c;
start=new;
printf("%d\n",start->data);
}
else
{
new1=(struct node *)malloc(sizeof(struct node));
new1->data=c;
new->next=new1;
printf("%d\n",new->next->data);
new=new->next;
new->next=NULL;
}
}

Related

How to use the variable globally in the function

I am writing a C code to insert an element to a sorted linked list. In this code I am using the variable "place" for deciding the position to insert the new element. But here it is getting error that "place" variable is to declared first. But I need the place globally for using in other functions. So what can I do to make it work? Also there is some problem with the function calling in the last part of main function. Please help me to correct it. Please let me know if there are any other errors. Thank You
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
int calcSize(struct Node* node){
int size=0;
while(node!=NULL){
node = node->next;
size++;
}
return size;
}
void insertPosition(int pos, int data, struct Node** head)
{
int size = calcSize(*head);
if(pos < 1 || size < pos)
{
printf("Can't insert, %d is not a valid position\n",pos);
}
else
{
struct Node* temp = *head;
struct Node* newNode = (struct Node*)
malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
while(--pos)
{
temp=temp->next;
}
newNode->next= temp->next;
temp->next = newNode;
}
}
void insertStart(struct Node** head, int data){
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
void insertLast(struct Node** head, int data){
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if(*head==NULL){
*head = newNode;
return;
}
struct Node* temp = *head;
while(temp->next!=NULL)
temp = temp->next;
temp->next = newNode;
}
void display(struct Node* node){
while(node!=NULL){
printf("%d ",node->data);
node = node->next;
}
printf("\n");
}
void check(struct Node* node,int ele){
while(node!=NULL){
int i=0;
if(ele>node->data)
{ place=i; break;}
node= node->next;
}
}
int main()
{ int ele; int place=0;
struct Node* head = NULL;
struct Node* node2 = NULL;
struct Node* node3 = NULL;
struct Node* node4 = NULL;
struct Node* node5 = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
node4 = (struct Node*)malloc(sizeof(struct Node));
node5 = (struct Node*)malloc(sizeof(struct Node));
head->data = 5; head->next = node2;
node2->data = 10; node2->next = node3;
node3->data = 18; node3->next = node4;
node4->data = 26; node4->next = node5;
node5->data = 32; node5->next = NULL;
printf("Current Items in Linked List\n");
display(head);
printf("Enter the element to insert into LL\n");
scanf("%d",&ele);
printf("Inserting into Linked List....");
if(place=1)
{ void insertStart(&head, ele); }
else if(place=5)
{ void insertLast(&head, ele); }
else
{ void insertPosition(place, ele, &head); }
return 0;
}

Linked list value pointed only changing inside function in C

I am trying to implement a linked list in C:
struct Node{
struct Node *next;
void *data;
};
With an insert function:
void insert(void *p2Node, void *data)
{
struct Node *newNode;
struct Node **p2p2Node= (struct Node **)p2Node;
if (newNode = malloc(sizeof(struct Node))) /* if successfully allocated */
{
newNode->data = data;
if ((*p2p2Node) != NULL) /* if the list is not empty */
{
newNode->next = (*p2p2Node)->next;
(*p2p2Node)->next = newNode;
}
else
(*p2p2Node) = newNode;
p2Node = p2p2Node;
}
printf("Inside the insert: %s\n", (*p2p2Node)->data);
}
I called insert in main():
int main()
{
char *setA = "liquid ";
char *setB = " lgd";
char *setC = "sample";
struct Node *nList = malloc(sizeof(struct Node));
insert(nList, setC);
printf("2Get %s\n", nList->data);
return 0;
}
No error or warning was reported, but the value was only changed inside the insert. Back to main() the linked list is still empty.
I do not understand: nList in main() is a void pointer. Inside insert(), *p2Node is not altered, I used p2p2Node to change the value p2Node points to, why is it not working? Did I mismatch the pointers? Is there a way I can make it work without modifying the parameter of insert()?
Thank you.
Use this code to insert values to the linked list.
struct node{
int data;
struct node* link;
};
struct node *root = NULL;
int len;
int main()
{
append();
display();
addatbegin();
display();
addatafter();
display();
}
Add values to the end of the list.
void append(){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter the data: ");
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL) //list is empty
{
root=temp;
}else
{
struct node* p;
p=root;
while(p->link != NULL)
{
p = p->link;
}
p->link = temp;
}
}
Add values to the beginning of the list.
void addatbegin()
{
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d", &temp->data);
temp->link = NULL;
if(root == NULL)
{
temp = root;
}
else
{
temp->link = root;
root = temp;
}
}
Add value after a node
void addatafter()
{
struct node* temp, *p;
int loc, i=1;
printf("Enter the location : ");
scanf("%d", &loc);
if(loc > len)
{
printf("Invalid input.");
}
else
{
p = root;
while(i > loc)
{
p = p->link;
i++;
}
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d", &temp->data);
temp->link = NULL;
temp->link = p->link;
p->link = temp;
}
}
To display the linked list
void display(){
struct node* temp;
temp = root;
if(temp == NULL)
{
printf("List id empty.\n");
}
else
{
while (temp != NULL){
printf("%d -> ", temp->data);
temp = temp->link;
}
printf("\n\n");
}
}

Insertion at end in circular linked list not working in C

Please point out the error in the code.
The function insertatend() inserts for the first time but not again.
I'm trying to insert a node at the end of a circular linked list, but after inserting an element for the first time, it gets stuck in the while loop if we try to enter data again.
struct node {
int data;
struct node *next;
};
typedef struct node node;
node *head = NULL;
node *insertatend(node *head, int value)
{
node *temp, *p;
p = head;
temp = (node *)malloc(sizeof(node));
temp->data = value;
temp->next = head;
if (head == NULL)
{
head = temp;
}
else
{
while (p->next != head)
p = p->next;
p->next = temp;
}
return head;
}
void display(node *head)
{
node *p = head;
if (head == NULL)
{
printf("\nlinked list is empty\n");
return;
}
while (p->next != head)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
int ch = 1, value;
while (ch)
{
printf("1.Insert 2.Display");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("enter an element:");
scanf("%d", &value);
head = insertatend(head, value);
break;
case 2:
display(head);
break;
}
}
return 0;
}
I think the mistake is here:
temp->next=head;
if(head==NULL){
head=temp;
}
When you enter your first element, head is null. So temp->next is set to NULL and head is set to temp.
When you enter your second element, it does this:
else{
while(p->next!=head)
p=p->next;
p->next=temp;}
Where p->next is null, so you will never have the situation that p->next == head and you will always be in the loop!
Edit:
So the solution aproach would be to change it to:
if(head==NULL){
head=temp;
}
temp->next=head;
Edit: second mistake in the display function: the loop doesn't print the last element. I just tested it and it is working fine.
So the complete code woud look like:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
typedef struct node node;
node *head = NULL;
node *insertatend(node *head, int value)
{
node *temp, *p;
p = head;
temp = (node *)malloc(sizeof(node));
temp->data = value;
if (head == NULL)
{
head = temp;
}
else
{
while (p->next != head)
p = p->next;
p->next = temp;
}
temp->next = head;
return head;
}
void display(node *head)
{
node *p = head;
if (head == NULL)
{
printf("\nlinked list is empty\n");
return;
}
do
{
printf("%d ", p->data);
p = p->next;
} while (p != head);
printf("\n");
}
int main()
{
int ch = 1, value;
while (ch)
{
printf("1.Insert 2.Display");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("enter an element:");
scanf("%d", &value);
head = insertatend(head, value);
break;
case 2:
display(head);
break;
}
}
return 0;
}
Alternate version, using tail pointer instead of head pointer, for faster appends.
#include <stdlib.h>
#include <stdio.h>
struct node {
struct node *next;
int data;
};
typedef struct node node;
node *insertatend(node *tail, int value)
{
node *p;
p = malloc(sizeof(node));
p->data = value;
if(tail == NULL){
p->next = p;
} else {
p->next = tail->next;
tail->next = p;
}
return p;
}
void display(node *tail)
{
node *p = tail;
if (p == NULL)
{
printf("\nlinked list is empty\n");
return;
}
do{
p = p->next;
printf("%d ", p->data);
}while(p != tail);
printf("\n");
}
int main()
{
node *tail = NULL;
int i;
for(i = 0; i < 8; i++)
tail = insertatend(tail, i);
display(tail);
return 0;
}

segmenation fault error in my linkedList -C

I put together a few pieces of code to make a linked list that adds to head(Has a special function) and in the middle(also special function).
my problem is, i need to provide the program with numbers and insert them as nodes in my LINKEDLIST. However, my display function(to display the tree of nodes) gives back segmentation fault and so does just taking values in without any display function.
I'm fairly new to malloc so i suspect the problem is there?
Thanks
#include<stdio.h>
#include<stdlib.h>
/*LINKEDLIST STRUCT*/
struct node {
int data;
struct node *next;
};
/*Inserting head-Node*/
struct node *insert_head(struct node *head, int number)
{
struct node *temp;
temp = malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Not enough memory\n");
exit(1);
}
temp->data = number;
temp->next = head;
head = temp;
return head;
}
/*Inserting inside a list*/
void after_me(struct node *me, int number)
{
struct node *temp;
temp = malloc(sizeof(struct node));
if(temp == NULL)
{
printf("Not enough memory\n");
exit(1);
}
temp->data = number;
temp->next = me->next;
me->next = temp;
}
/*PRINTING LIST*/
void display(struct node *head)
{
struct node *moving_ptr = head;
while(moving_ptr != NULL)
{
printf("%d-->",moving_ptr->data);
moving_ptr = moving_ptr->next;
}
}
int main()
{
int index;
struct node *head;
struct node *previous_node;
scanf("%d", &index);
while(index > 0)
{
/*allocating in List */
if(head == NULL)
head = insert_head(head,index);
else
if((head != NULL) && (index <= (head->data)))
{
struct node *temp;
head->next = temp;
temp->next = head;/*TRY INSERT HEAD FUNC.*/
}
else
if((head != NULL) && (index > (head->data)))
{
previous_node->data = index-1;
after_me(previous_node,index);
}
scanf("%d", &index);
}
display(head);
}
I suggest as follows.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
//aggregated into one place
struct node *new_node(int number){
struct node *temp;
if(NULL == (temp = malloc(sizeof(*temp)))){
printf("\nNot enough memory\n");
exit(1);
}
temp->data = number;
temp->next = NULL;
return temp;
}
struct node *insert_head(struct node *head, int number) {
struct node *temp = new_node(number);
temp->next = head;
return temp;
}
void after_me(struct node *me, int number){
struct node *temp = new_node(number);
temp->next = me->next;
me->next = temp;
}
void display(struct node *head){
struct node *moving_ptr = head;
while(moving_ptr != NULL){
printf("%d", moving_ptr->data);
if(moving_ptr = moving_ptr->next)
printf("-->");
}
putchar('\n');
}
struct node *insert(struct node *me, int number){
if(me){
if(number <= me->data){
me = insert_head(me, number);
} else {
me->next = insert(me->next, number);
}
} else {
me = new_node(number);
}
return me;
}
void release(struct node *list){//Of course, you will be able to replace a simple loop(e.g while-loop).
if(list){
release(list->next);
free(list);
}
}
int main(void){
struct node *head = NULL;
int index;
while(1==scanf("%d", &index) && index > 0){
head = insert(head, index);
}
display(head);
release(head);
return 0;
}

convert an ordered linked list to binary search tree

Here's my code. Please let me know the bug.The question is to convert a given ordered linked list into a binary search tree.
Thanks.
struct treenode* makeBSTnode(struct node* head)
{
struct treenode *root=(struct treenode*)malloc(sizeof(struct treenode));
root->data = head->data;
root->leftchild = NULL;
root->rightchild = NULL;
return root;
}
struct treenode* makeBST(struct node **head,int iterations)
{
if(iterations==0)
{
/*this is the code for the base condition*/
struct treenode *root=(struct treenode*)malloc(sizeof(struct treenode));
root = makeBSTnode(*head);
return root;
}
else
{
/*this is for the case when the node is not single but should be broken down into parts*/
struct node *leftchildnode=NULL,*rightchildnode=NULL,*rootnode=NULL,*temp;
temp = (struct node*)malloc(sizeof(struct node));
struct treenode *root = (struct treenode*)malloc(sizeof(struct treenode));
int limit = (int)ceil((float)(3*iterations)/2);
for(int i=1;i<=limit;i++)
{
if(i==(int)ceil((float)iterations/2))
{
leftchildnode = (struct node*)malloc(sizeof(struct node));
leftchildnode = *head;
}
else if(i==iterations)
{
rootnode = (struct node*)malloc(sizeof(struct node));
rootnode = *head;
}
else if(i==(int)ceil((float)(3*iterations)/2))
{
rightchildnode = (struct node*)malloc(sizeof(struct node));
rightchildnode = *head;
}
if(*head)
(*head) = (*head)->next;
else
break;
}
root = makeBSTnode(rootnode);
root->leftchild = makeBST(&temp,(int)ceil((float)iterations/2));
root->rightchild = makeBST(&rootnode,(int)ceil((float)(3*iterations/2)));
return root;
}
}
I called this makeBST function in main as:
root = makeBST(&head,(int)ceil((float)length/2));

Resources