Linked List not working for insertion - c

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

Related

Free malloc, using Linus method of deletion in a linked list

I been stuck in this problem for hours and I still cannot free the malloc of my program. for some reason it says I have 16 bytes in 1 blocks, I have been trying to free the malloc using a function freeList() in the program but it seems to not be working, if you can catch what is my error I will really appreciate it!
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
void add( int num )
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node)); //*
temp->data=num;
if (head== NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}
void insert(int num)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}
int delete(int num)
{
struct node **temp;
temp=&head;
while(*temp!=NULL)
{
if((*temp)->data==num)
{
*temp= (*temp)->next;
return 1;
}
else
{
*temp= &((*temp)->next);
}
}
return 0;
}
void freeList(){
struct node *temp;
int i=0;
while(head!=NULL){
temp=head;
head=head->next;
free(temp->data);
free(temp);
i++;
}
printf("free %d records...\n",i);
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf("\n");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Size\n");
printf("4.Delete\n");
printf("5.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);
insert(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: printf("Size of the list is %d\n",count());
break;
case 4: if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully\n",num);
else
printf("%d not found in the list\n",num);
}
break;
case 5: freeList();
return 0;
default: printf("Invalid option\n");
}
}
}
return 0;
}
My main problem is in the function freeList() when running the code freeList does not free the malloc.
void freeList(){
struct node *temp;
int i=0;
while(head!=NULL){
temp=head;
head=head->next;
free(temp->data);
free(temp);
i++;
}
printf("free %d records...\n",i);
}

simple linked list program in c

//I implemented simple linked list progam in c
here i implement insert at first,last,particular position..
these error occured on execution :expected '=', ',', ';', 'asm' or 'attribute' before '*' token
..Anyone can please clarify what is these error
#include<stdio.h>
#include<malloc.h>
#define ISEMPTY printf("\n empty list");
struct node{
int value;
struct node *next;
}
startnode * create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_position();
typedef struct node start_node;
start_node *newnode,*ptr,*prev,*temp;
start_node *first=NULL,*last=NULL;
int main()
{
int ch;
char ans='Y';
while(ans == 'Y' || ans == 'y')
{
printf("\n Single linked list \n");
printf("\n1.INSERT NODE AT FIRST");
printf("\n2.INSERT NODE AT LAST");
printf("\n3.INSERT NODE AT POSITION");
printf("\n ENTER YOUR CHOICE");
scanf("%d",&ch);
switch(ans)
{
case 1:printf("INSERT NODE AT FIRST");insert_node_first();break;
case 2:printf("INSERT NODE AT LAST");insert_node_last();break;
case 3:printf("INSERT NODE AT POSITION");insert_node_position;break;
default:printf("U DIDN'T SELECT ANYTHING SO EXCEED");
}
printf("DO YOU WANT TO CONTINUE");
scanf("%c",&ans);
}
return 0;
}
start_node* create_node(int val)
{
newnode=(start_node *)malloc(sizeof(start_node));
if(newnode==NULL)
{
printf("\n Memory not allocated");
return 0;
}
else{
newnode->value=val;
newnode->next=NULL;
return newnode;
}
}
void insert_node_first()
{
int val;
printf("Enter the value for the node");
scanf("%d",&val);
newnode=create_node(val);
if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else{
temp=first;
first=newnode;
first->next=temp;
}
printf("\n inserted");
}
void insert_node_last()
{
int val;
printf("Enter the value insert at last");
scanf("%d",&val);
newnode=create_node(val);
if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else{
last->next=newnode;
last=newnode;
last->next=NULL;
}
}
void insert_node_position()
{
int val,position,count,i;
printf("Enter the value to insert");
scanf("%d",&val);
newnode=create_node(val);
printf("Enter the position you want to ");
scanf("%d",&position);
ptr=first;
while(ptr !=NULL)
{
ptr=ptr->next;
count++;
}
if(position == 1)
{
if(first == last && first == NULL)
{
first=last=newnode;
first->next=NULL;
last->next=NULL;
}
else{
temp=first;
first=newnode;
first->next=temp;
}
}
else if(position > 1 && position<=count)
{
ptr=first;
for(i=1;i<position;i++)
{
prev=ptr;
ptr=ptr->next;
}
prev->next=newnode;
newnode->next=ptr;
printf("inserted at position");
}else{
printf("position is out of range");
}
}
Change startnode with start_node and move
start_node * create_node(int);
After
typedef struct node start_node;
Otherwise the compiler does not know, at that point, what is start_node;
Moreover you forgot a ; on struct declaration
struct node{
int value;
struct node *next;
};

AddNew Function using Linked List In C

Question: Create a linked list containing values in the ascending values. Then write functions addNew() which will accept a value from the user and then call addBegin() and addafterValue() functions to add the input value in the appropriate place
e.g. consider the list is like this:
12,15,20,26 then if the user enters values 8, 16 & 30 the list will look like this: 8,12,15,16,20,26,30.
My program:
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *start=NULL;
void append()
{
NODE *temp,*ptr;
temp=(NODE *)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
void display()
{
NODE *ptr=start;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void addBegin(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=start;
start=temp;
}
unsigned int addAfterValue(int val,NODE *ptr)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
return temp;
}
void addNew()
{
int val;
unsigned int loc;
NODE *ptr=start;
printf("Enter value to add:");
scanf("%d",&val);
if(val<ptr->data) {
addBegin(val);
ptr=NULL;
}
while(ptr!=NULL) {
if(ptr->next!=NULL)
{
if(val<ptr->next->data)
{
addAfterValue(val,ptr);
ptr->next=loc;
ptr=NULL;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next==NULL)
{
loc=addAfterValue(val,ptr);
ptr=NULL;
}
}
}
int main()
{
int ans;
do
{
printf("Enter [1]To append\n[2]To add new node\n[3]To display\n[0]To exit\n");
printf("Enter your choice:");
scanf("%d",&ans);
switch(ans)
{
case 1:
append();
break;
case 2:
addNew();
break;
case 3:
display();
break;
case 0:
break;
default:
printf("Wrong Input.Try again.");
}
}while(ans);
}
My doubt: The addBegin() function works perfectly. I think there's something wrong with addafterValue(). Can anyone help me by finding out my mistake?
Instead of passing the current pointer address.
Use the previous node pointer and assign to its next node.
void addAfterValue(int val,NODE *ptr)
{
NODE *temp = (NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
ptr->next = temp;
}
Change the addNew function
void addNew()
{
int val;
NODE *ptr=start;
NODE *prev= NULL;
printf("Enter value to add:");
scanf("%d",&val);
if( ptr == NULL || val < ptr->data)
{
addBegin(val);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
while( ptr != NULL)
{
if( val <= ptr->data)
{
addAfterValue(val,prev);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
}
/* Control comes here if the entire list is scanned.... Now append it to the end using prev pointer, as the new node is greater than all of the existing nodes */
}

How to insert node at begin ,end and selected position in singly linked list?

I am new for c programming ,i have tried myself inserting node in singly linked list program but i didn't get a proper output and i dont have any idea to correct my program if anybody knows please help.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head=NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
void pos(int num,int loc)
{
int length();
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void display()
{
struct node *temp=NULL;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
int num;
head=NULL;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
pos(num,loc);
break;
case 4: printf("display the values");
display();
break;
case 5: printf("exit");
display();
}
}
return 0;
}
i think the error is in my main function but am not clear in that please help
In your addbeginmethod there's at least one obvious error:
if(head=NULL)
should be
if (head == NULL)
as you need to compare, not assign.
In your posmethod you have a function declaration: int length(); which shouldn't be there, but rather at the top, before main.
Another issue, this time in the display method:
void display()
{
struct node *temp=NULL;
if(temp==NULL) {
printf("List is empty:");
}
while(temp!=NULL) {
printf("%d",temp->data);
temp=temp->next;
}
}
Here temp will always be NULL, I guess you meant to assign headto the temppointer, otherwise it will never traverse the list.
And finally, in the insert at specific position choice you need to ask for a location value and pass that along too the function call, so add a declaration for int loc;in main, and change the third case to this:
case 3:
printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&loc);
pos(num,loc);
break;
to the
Finally I'm going to quote from the C99 standard, section 5.1.2.2.1 Program startup:
The function called at program startup is named main. The
implementation declares no prototype for this function. It shall be
defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as
argc and argv, though any names may be used, as they are local to the
function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
So, please, change your declaration of mainand include a returnline at the end (possibly return 0;indicating successful program exit).
This became rather lengthy. After the suggested changes your program should look something like this:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void addbegin(int num)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) {
head=temp;
head->next=NULL;
} else {
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1, *temp2;
temp1 = malloc(sizeof(struct node));
temp1->data = num;
temp2 = head;
if(head == NULL) {
head = temp1;
head->next = NULL;
} else {
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr = head;
while(cur_ptr != NULL) {
cur_ptr = cur_ptr->next;
count++;
}
return (count);
}
void pos(int num, int loc)
{
struct node *temp, *cur_ptr, *prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0) {
printf("it is illegal call:");
} else {
if(loc == 1) {
addbegin(num);
} else {
for(i=1; i<loc; i++) {
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp = malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp = head;
if(temp == NULL) {
printf("List is empty:");
}
printf("The list contains the following values:\n");
while(temp!=NULL) {
printf("%d\n",temp->data);
temp=temp->next;
}
}
int main()
{
int choice, num, loc;
head = NULL;
while(1) {
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.Insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0) {
printf("Enter only an Integer\n");
}
switch(choice) {
case 1:
printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2:
printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3:
printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&loc);
pos(num,loc);
break;
case 4:
printf("Display the values\n");
display();
break;
case 5:
printf("exit");
exit(0); // maybe you should exit here.
display();
}
}
return 0;
}
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
and
void display()
{
struct node *temp=NULL;
temp = head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) //this not assign, you need == to compare
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void pos(int num,int loc)
{
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp=head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int num;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
pos(num,loc);
break;
case 4: printf("\ndisplay the values: ");
display();
break;
case 5: printf("exit");
display();
}
}
return 0;
}
Try this
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) //this not assign, you need == to compare
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void pos(int num,int loc)
{
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp=head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int num;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position to insert: 1 for insert at begin, so on: ");
scanf("%d",&loc);
pos(num,loc);
break;
case 4: printf("\ndisplay the values: ");
display();
break;
case 5:
display();
printf("\n exiting program \n");
exit(0);
}
}
return 0;
}
Try this:
#include<iostream>
using namespace std;
struct stu{
int id;
stu *next = NULL;
};
stu *first = NULL;
stu *last = NULL;
int opt;
void insert_end();
void display();
int main(){
do{
cout<<"\n\n0.Exit";
cout<<"\n1.Insert at end in linked list";
cout<<"\n2.Display linked list";
cout<<"\n\nEnter your choice: ";
cin>>opt;
switch(opt){
case 1:{
insert_end_list1();
break;
}
case 2:{
display_list1();
break;
}
}
}
while(opt != 0);
return 0;
}
void insert_end(){
stu *current = new stu;
cout<<"\n\nEnter the student id:";
cin>>current->id;
if(first == NULL){
cout<<"\n\nEmpty linked list";
first = last = current;
}
else{
last->next = current;
last = current;
}
}
void display(){
stu *p = first;
while(p != NULL){
cout<<p->id<<" ";
p = p->next;
}
}
This code uses structure for creation of a new node in singly linked list.

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

Resources