How to add to end in linked list C - c

I wrote a simple program that should add some numbers inside a linked-list, then remove a specific one and print the elements at the end but it doesn't add all the elements, it simply adds the first one and the last one. And also the delete function gives me problems because it doesn't delete anything but it keep's saying that the list's empty. Thanks for the help everybody
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int info;
struct node *next;
}node;
typedef node *list;
list L;
list del(list L, int elem)
{
node *current, *temp;
current=L;
if(L==NULL)
{
printf("there's nothign to delete\n");
}
if((L->next!=NULL)&&(L->info==elem))
{
current=L->next;
free(L);
L=current;
return(L);
}
else
{
temp=del(L->next, elem);
return(L);
}
}
list addEnd(list L, float elem)
{
node *punt, *curr, *new_node;
if(L==NULL)
{
punt=malloc(sizeof(node));
punt->info=elem;
punt->next=NULL;
L=punt;
return(L);
}
else
{
punt=L;
curr=L;
while(punt->next!=NULL)
{
curr=punt;
punt=punt->next;
}
new_node=malloc(sizeof(node));
new_node->info=elem;
new_node->next=NULL;
curr->next=new_node;
return(L);
}
}
void print(list L)
{
node *current = L;
if(current==NULL)
{
printf("list's empty");
}
while (current != NULL)
{
printf("%d\n", current->info);
current = current->next;
}
}
int main()
{
int n1,n2,n3,n4;
n1=1;
n2=10;
n3=23;
n4=45;
L=addEnd(L, n1);
L=addEnd(L, n2);
L=addEnd(L, n3);
L=addEnd(L, n4);
L=del(L,23);
print(L);
return(0);
}

Try this correction, you have two mistake one in delete and another one in add:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int info;
struct node *next;
}node;
typedef node *list;
list L;
list del(list L, int elem)
{
// no need to use recursive function
node *current, *temp, *prev;
temp = L;
prev = L;
if(temp==NULL)
{
printf("there's nothign to delete\n");
}
while((temp!=NULL))
{
if(temp->info==elem)
{
current=temp->next;
if(L == temp)
L = current;
else
prev->next=current;
free(temp);
return(L);
}
// you should keep the previous node
prev = temp;
temp = temp->next;
}
return(L);
}
list addEnd(list L, int elem)
{
node *punt/*, *curr*/, *new_node;
if(L==NULL)
{
punt=malloc(sizeof(node));
punt->info=elem;
punt->next=NULL;
L=punt;
return(L);
}
else
{
punt=L;
// curr=L;
while(punt->next!=NULL)
{
//curr=punt;
//no needed
punt=punt->next;
}
new_node=malloc(sizeof(node));
new_node->info=elem;
new_node->next=NULL;
// this instruction is wrong curr->next=new_node; it erase the last value
punt->next=new_node;
return(L);
}
}
void print(list L)
{
node *current = L;
if(current==NULL)
{
printf("list's empty");
}
while (current != NULL)
{
printf("%d\n", current->info);
current = current->next;
}
}
int main()
{
int n1,n2,n3,n4;
n1=1;
n2=10;
n3=23;
n4=45;
L=addEnd(L, n1);
L=addEnd(L, n2);
L=addEnd(L, n3);
L=addEnd(L, n4);
L=del(L,23);
print(L);
return(0);
}

Related

I am trying to reverse a linked list using recursion in C, I have some doubts on my recursive function

Below is the program
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;
struct node* reverse_ll(struct node* hnode)
{
if(hnode == 0)
{
return 0;
}
if(hnode->next == 0)
{
head=hnode;
return hnode;
}
struct node* ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next=0;
//return hnode;
}
void display()
{
struct node *ptr;
ptr=head;
if(ptr==0)
{
printf("empty");
}
else
{
while(ptr!=0)
{
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("null");
}
}
int main()
{
struct node* h;
lastinsert(1);
lastinsert(2);
lastinsert(3);
lastinsert(4);
lastinsert(5);
display();
h=reverse_ll(head);
display();
return 0;
}
In function reverse_ll() even if I comment "return hnode" I am getting the right output How is it possible where does ptr receives its address from when I comment "return hnode"?
output: 1->2->3->4->5->null
5->4->3->2->1->null
reverse_ll() must return a struct node * in the recursive case:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;
void lastinsert(int data) {
struct node **c = &head;
for(; *c; c = &(*c)->next);
*c = malloc(sizeof(*head));
if (!(*c)) {
printf("malloc failed\n");
return;
}
(*c)->data = data;
(*c)->next = NULL;
}
struct node *reverse_ll(struct node* hnode) {
if(!hnode)
return NULL;
if(!hnode->next) {
head = hnode;
return hnode;
}
struct node *ptr=reverse_ll(hnode->next);
ptr->next=hnode;
hnode->next = NULL;
return hnode;
}
void display() {
if(!head) {
printf("empty");
return;
}
for(struct node *ptr = head; ptr; ptr = ptr->next) {
printf("%d->",ptr->data);
}
printf("null\n");
}
int main() {
for(int i = 1; i <= 5; i++) {
lastinsert(i);
}
display();
reverse_ll(head);
display();
// It's good practice to implement a function that frees you list
// which you would call here.
return 0;
}
and example run:
$ ./a.out
1->2->3->4->5->null
5->4->3->2->1->null

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;
}

Linked List not working for insertion

I have written a linked list code to insert a element in the node. But the problem is when i want to insert first element using function, the output is coming empty. But when i insert first element inside the main function (see comment line), it gives the correct output. How to solve it ?
Here is my C code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int val;
struct node *next;
}node;
void print(node *head){
if(tem == NULL){
printf("List is Empty\n");
return;
}
node *tem= head;
while(tem != NULL){
printf("%d ", tem->val);
tem= tem->next;
}
}
void insert(node *head, int val){
if(head == NULL){
node *tem= malloc(sizeof(node*));
tem->val= val;
tem->next= NULL;
head= tem;
return;
}
node *tem= head;
while(tem->next != NULL){
tem= tem->next;
}
tem->next= malloc(sizeof(node*));
tem->next->val = val;
tem->next->next= NULL;
}
int main()
{
node *head= NULL;
/*
head = malloc(sizeof(node*));
head->val= 5;
head->next= NULL;
*/
insert(head, 15);
print(head);
return 0;
}
Thanks
Try sending the address of the head instead of head as shown below:
insert(&head, 15);
void insert(node **head, int val){
if(*head == NULL){
node *tem= malloc(sizeof(node*));
tem->val= val;
tem->next= NULL;
*head= tem;
return;
}
This is because when you are sending the head, any changes made will be local to that function (insert in this case) and won't be reflected outside that function. Hence, you have to send the address of head (&head) so that changes made to head are reflected outside the function as well. Cheers
Try this completely implemented singly linked list:
#include <stdio.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void insert(int data, int position)
{
struct node *newNode=malloc(sizeof(struct node));
newNode->data=data;
if(position<1)
{
printf("Invalid Insertion Position \n");
return;
}
if(head==NULL && position==1)
{
newNode->next=NULL;
head=newNode;
}
else if(head==NULL && position!=1)
{
printf("Invalid Insertion Position \n");
}
else if(position==1)
{
newNode->next=head;
head=newNode;
}
else
{
int i=0;
struct node *temp=head;
while(temp->next!=NULL && i<position-2)
{
i++;
temp=temp->next;
}
if(i<position-2)
{
printf("Invalid Insertion Position \n");
}
else
{
newNode->next=temp->next;
temp->next=newNode;
}
}
}
void delete(int position)
{
int i=0;
if(position<1)
{
printf("Invalid Position of Deletion \n");
return;
}
if(head==NULL)
{
return;
}
if(position==1)
{
head=head->next;
}
else
{
struct node *temp=head;
while(temp->next->next!=NULL && i<position-2)
{
i++;
temp=temp->next;
}
if(i<position-2)
{
printf("Invalid Position of Deletion \n");
return;
}
else
{
temp->next=temp->next->next;
}
}
}
void printlist()
{
if(head==NULL)
{
printf("Empty List!! \n");
return;
}
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
printf("\t");
temp=temp->next;
}
printf("\n");
}
int main()
{
int t;
printf("Enter number of Test Cases: \t");
scanf("%d", &t);
printf("\nEnter Queries in this format: \n");
printf("For Insertion: \t I data position \n");
printf("\tEx:\t I 25 5 \n");
printf("For Deletion: \t D position \n");
printf("\tEx:\t D 2 \n\n");
while(t--)
{
char c;
int a,b;
printf("Enter query: \t");
scanf("%c", &c);
scanf("%c", &c);
if(c=='I')
{
scanf("%d %d", &a,&b);
insert(a,b);
}
else if(c=='D')
{
scanf("%d", &a);
delete(a);
}
printlist();
}
}

Linked list program in C hangs

I am writing a linked list program
to add items and display those items.
I am able to add first item successfully,
but error occurs while adding the second item.
I tried to figure out the error,but
everything looks fine to me.
The program hangs,here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct link_list
{
int number;
struct link_list *next;
};
typedef struct link_list node;
node *head;
void add(int num)
{
node *newnode,*current;
newnode = (node *)malloc(sizeof(node));
newnode->number = num;
newnode->next = NULL;
if(head == NULL)
{
head = newnode;
current = newnode;
}
else
{
current->next = newnode;
current = newnode;
}
}
void display(node *list)
{
list = head;
if(list == NULL)
{
return;
}
while(list != NULL)
{
printf("%d",list->number);
list = list -> next;
}
printf("\n");
}
int main()
{
int i,num;
node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{
switch(i)
{
case 1:
printf("Enter the number to insert : ");
scanf("%d",&num);
add(num);
break;
case 2:
if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3: return 0;
default: printf("Invalid option\n");
}
}
}
return 0;
}
The issue is with the value of current not persisting across function calls. Either move it outside of the function (i.e. right below the head declaration), or declare it as static.
just one change , define your current pointer outside the scope of void add
node *head,*current;
here is the correct code
struct link_list
{
int number;
struct link_list *next;
};
typedef struct link_list node;
node *head,*current;
void add(int num)
{
node *newnode;
newnode = (node *)malloc(sizeof(node));
newnode->number = num;
newnode->next = NULL;
if(head == NULL)
{
head = newnode;
current = newnode;
}
else
{
current->next = newnode;
current = newnode;
}
}

Delete all items from my queue

I can't delete all my items from my queue. Here is what I am trying to do and here is all my code. Please I can't see what I am doing wrong and also I want to store the count of how many items where in my queue.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int count = 0;
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *var=rear;
while(var!=NULL)
{
free(var);
var = var->next;
count = count + 1;
}
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
front->next=temp;
front=temp;
front->next=NULL;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("\nElements in queue are: ");
while(var!=NULL)
{
printf("\t%d",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nQueue is Empty\n");
}
problem is simply here:
free(var);
var= var->next;
You can do like this:
struct Node* buf=var->next;
free(var);
var=buf;

Resources