Linked List Error in C programming - c

i had write a code in C, but when execute, it an error.
May i know how to combine a correct structure ? kindly advise, thank you
Output results:
Enter integers: 23 12 34 56 78 12
Traversing the list : 23->12->34->56>78->12
Minimum value : 12
Reversing the list: 12->78->56->34->12->23
The code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
} *head;
void insert_data(int value)
{
struct node *var,*temp;
temp=head;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
void reverse_list()
{
struct node *temp,*temp1,*var;
temp=head;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;
}
head=var;
}
void display()
{
struct node *var;
var=head;
printf("\nlist of elments are \n");
while(var!=NULL)
{
printf(" %d ->",var->data);
var=var->next;
}
}
int main()
{
int i,value;
char ch='y';
head=NULL;
printf("\nEnter Integers: ");
scanf("%d",&value);
insert_data(value);
display();
getch();
return 0;
}

Can't understand the problem, but your code asks for only one element.
Make a loop for ability to enter more elements:
int main()
{
int i,value = 0;
char ch='y';
head=NULL;
printf("\nEnter Integers: ");
// here it is
while (value != -1) {
scanf("%d",&value);
insert_data(value);
display();
}
getch();
return 0;
}

Related

Singly linked list program error while using malloc function(updated one)

I am having trouble inserting the second element the program just waits and exits, the first element is printing correctly, the problem occurs when I try to enter 2nd element using function Insert.
please help
case 7: uses the function Insert which passes a "d" which is data,and Display
prints the list.
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
void Insert(int d){
struct node *temp,*newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}
#include <stdio.h>
#include <stdlib.h>
int count=0;
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*temp;
void Insert(int d){
struct node *newnode;
newnode= (struct node*) malloc(sizeof(struct node));
if(newnode==NULL)
{
printf("not there\n");
}
else{newnode->data= d;
newnode->next=NULL; }
if(head==NULL)
{
head=newnode;temp=newnode; count++;
}
else
{
temp->next=newnode;
temp=newnode;
count++;
}
}
void Display(){
struct node*temp;
temp=head;
while(temp!=NULL){
printf("%d,",temp->data);
temp=temp->next;
}
printf("\n", );
}
void main()
{ int c=0;int d;
do{
printf("choose an option: 1. Insert at begining 2. Insert at end 3. Insert at specified position\n 4.Delete from begining 5.Delete from end 6. Delete from specified position 7.Insert\n 8. Exit\n");
scanf("%d",&c);
switch (c) {
case 7: printf("enter element\n");
scanf("%d",&d);Insert(d);Display();break;
default : c=8; break;
}
}while(c!=8);
}

making a phonebook system using linked list

i'm creating a phonebook system in c using single linked list it seems that everything is working well but the delete option always giving me an error and i don't know how to fix it so i hope someone can tell what is problem in the code and show me a code that can modify the name or the number in this code
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char *first,*last;
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",&first);
scanf("%s",&last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
}
void del()
{
struct node *pretemp,*temp;
char *f,*l;
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",&f);
scanf("%s",&l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%s ",temp->number);
temp=pretemp->next;
delete pretemp;
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}
this is the error
I had made the following changes in your search and delete function
The first and last buffer in both search and delete function were not allocated memory before using them in scanf. This will cause run-time error
The way you were catching user input for first and last name in scanf was also improper
In search and delete function I modified the string comparison. To compare to string you need use strncmp function. Using == will check the address of first byte.
In search function you were not checking end of list.
In del function I have changed printf("%s ", temp->number) to printf("%d ", temp->number)
void search()
{
struct node *temp;
char first[20], last[20];
temp = head;
printf("Enter name to be searched:\n");
scanf("%s", first);
scanf("%s", last);
while (temp != NULL && ((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0)))
{
temp = temp->next;
}
if (temp != NULL) {
printf("%s\n", temp->firstname);
printf("%s\n", temp->lastname);
printf("%d\n", temp->number);
}
}
void del()
{
struct node *pretemp, *temp;
char first[20], last[20];
temp = head;
pretemp = head->next;
printf("Enter name :");
scanf("%s", first);
printf("Enter Last name:");
scanf("%s", last);
while (temp != NULL) {
if((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0))
{
printf("%s ", temp->firstname);
printf("%s ", temp->lastname);
printf("%d ", temp->number);
temp = pretemp->next;
delete pretemp;
break;
}
else
{
temp = temp->next;
pretemp = pretemp->next;
}
}
}
I made some changes in your code, you can find comment in code bellow.
It is compiled and likend under linux ubuntu 18.04 and it works now.
Basicaly, when you use scanf(" %s ", astr), 'astr' should have enough space to accept input.
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char first[20], last[20]; /* space for input */
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",first); /* you dont need '&' operator for string*/
scanf("%s",last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
}
void del()
{
struct node *pretemp,*temp;
char f[20],l[20]; /* you need a space to store input */
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",f); /* you dont need '&' operator to access a string */
scanf("%s",l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%ld ",temp->number); /* 'number' is long int */
temp=pretemp->next;
free(pretemp); /* 'delete' is c++ operator, not C */
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
} /* missing curly bracket */
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}

Why my linked list is displaying garbage values

My doubly Linked list in C is displaying garbage value instead of value that I have entered, I have run this code on Turbo C++.
the code compiled correctly with 0 errors and 0 warnings, but still it is displaying some garbage values. I have included the libraries (stdio.h,conio.h,stdlib.h,malloc.h)
here is the code :
struct dlist
{
int data;
struct dlist *next;
struct dlist *prev;
};
struct dlist *head, *end;
void create_dlist()
{
char k='y';
struct dlist *new_node;
while(k=='y'||k=='Y') {
if(head==NULL) {
new_node=(struct dlist *)malloc(sizeof(struct dlist));
printf("Enter the integer value -> ");
new_node->data=0;
new_node->next=NULL;
new_node->prev=NULL;
scanf("%d",&new_node->data);
head=new_node;
end=new_node;
} else {
new_node=(struct dlist *)malloc(sizeof(struct dlist));
printf("Enter the integer value -> ");
new_node->data=0;
new_node->next=NULL;
new_node->prev=end;
scanf("%d",&new_node->data);
end->next=new_node;
end=new_node;
}
printf("Do you want to continue (y/n) ; ");
scanf(" %c",&k);
}
}
void display()
{
struct dlist *pointer;
pointer=head;
if(pointer!=NULL) {
while(pointer->next!=NULL) {
printf("%d\t",&pointer->data);
pointer=pointer->next;
}
} else {
printf("list is empty");
}
}
int main()
{
clrscr();
head=NULL;
end=NULL;
create_dlist();
display();
getch();
return 0;
}
A coded solution will be a great help
Whole problem is in display(). First you print address and not value of data. Also, you are not printing last element. I made some changes. Ask me if you need any explanations.
void display()
{
struct dlist *pointer;
pointer=head;
if(pointer!=NULL)
{
while(pointer!=NULL)
{
printf("%d\t",pointer->data);
pointer=pointer->next;
}
}
else
{
printf("list is empty");
}
}

Palindrom check of linkedlist

Here is the code i wrote to check if a singly linked list of integers is a palindrome or not.
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
struct list *insert(int data,struct list *node)
{
if(node==NULL)
{
node=(struct list *)malloc(sizeof(struct list));
node->data=data;
node->next=NULL;
}
else
{
struct list *newnode=(struct list *)malloc(sizeof(struct list));
newnode->data=data;
newnode->next=node;
node=newnode;
}
return node;
}
int palindrome(struct list *node,int n)
{
int i=0;int j=0;
int arr1[n],arr2[n];
struct list *current;
current=node;
while(current!=NULL)
{
arr1[i]=current->data;
i++;
current=current->next;
}
i=0;j=0;
for(i=n-1;i>=0;i--)
{
arr2[j]=arr1[i];
j++;
}
for(i=0;i<n;i++)
{
if(arr1[i]!=arr2[i])
{
return 0;
}
}
return 1;
}
void main()
{
int n;
scanf("%d",&n);
struct list *node=NULL;
int i=1;int value;
for(i=1;i<=n;i++)
{
scanf("%d",&value);
insert(value,node);
}
int status=palindrome(node,n);
printf("%d",status);
}
But the code returns 0 even in case of valid palindrome inputs like "121" and also in non - palindrome inputs like "154". Please help. Thanks
You need to write
node = insert(value,node);
in main. Otherwise the head node is not changed because the function insert deals with a copy of the node.

Doubly Linked List Delete Function Error

I have written the following program but the problem is in Delete function.Whenever i am trying to delete the value at first position.The whole list is lost don't know why.If i try to display list after that then some garbage values are printed.The function is working perfectly for other positions.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
struct Student
{
int rno;
char name[20];
struct Student *next;
struct Student *prev;
};
void Display(struct Student *head)
{
assert(head!=NULL);
while(head!=NULL)
{
printf("%d\t%s\t",head->rno,head->name);
head=head->next;
}
}
struct Student *Insert(struct Student *head,const int position,const int rno,const char name[])
{
//printf("%s\n",__FUNCTION__);
struct Student *temp=(struct Student *)malloc(sizeof(struct Student));
struct Student *traverse=head;
int pos=position;
if(temp==NULL)
exit(-1);
temp->rno=rno;
strcpy(temp->name,name);
temp->next=NULL;
temp->prev=NULL;
// printf("%s\n",__FUNCTION__);
if(pos==1)
{
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
else
{
for(traverse=head,pos=position;traverse->next!=NULL&&pos-2!=0;traverse=traverse->next,pos--);
if(traverse==NULL || pos-2!=0)
{
printf("Invalid Position");
}
else
{
temp->next=traverse->next;
if(temp->next!=NULL)
temp->next->prev=temp;
temp->prev=traverse;
traverse->next=temp;
}
}
return head;
}
void DeleteAll(struct Student *head)
{
struct Student *temp=head;
while(temp->next!=NULL)
{
head=head->next;
free(temp);
temp=head;
}
free(temp);
}
void Delete(struct Student *head,int pos)
{
assert(head!=NULL);
struct Student *temp=head;
struct Student *traverse=head;
int position=pos;
if(position==1)
{
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
}
else
{
while(traverse->next!=NULL&&position-1!=0)
{
traverse=traverse->next;
position--;
}
if(traverse==NULL || position-1!=0)
{
printf(".............Invalid position..........\n");
}
else
{
traverse->prev->next=traverse->next;
if(traverse->next)
traverse->next->prev=traverse->prev;
}
}
}
struct Student *CreateStudentList(const int no_of_students)
{
struct Student *head=NULL;
int i;
int rno;
char name[20];
for(i=0;i<no_of_students;i++)
{
printf("Enter roll number and name:");
scanf("%d%s",&rno,name);
head=Insert(head,i+1,rno,name);
}
return head;
}
void SimulateDoublyLinkedList()
{
struct Student *cdscpp2013=NULL;
int no_of_students;
int choice,rno,position;
char name[20];
while(choice!=5)
{
if(NULL==cdscpp2013)
{
printf("Enter number of students:");
scanf("%d",&no_of_students);
cdscpp2013=CreateStudentList(no_of_students);
}
else
{
printf("\nMenu Operations\nPress 1 for Insert\nPress 2 for Delete\nPress 3 for DeleteAll\nPress 4 for Display\nPress 5 for Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter roll number and name to ininsert:");
scanf("%d%s",&rno,name);
printf("Enter position to insert:");
scanf("%d",&position);
cdscpp2013=Insert(cdscpp2013,position,rno,name);
break;
case 2:
printf("Enter position to delete:");
scanf("%d",&position);
Delete(cdscpp2013,position);
break;
case 3:
DeleteAll(cdscpp2013);
break;
case 4:
Display(cdscpp2013);
break;
case 5:
exit(1);
default:
printf("Invalid choice....Please Enter proper option.....");
}
}
}
}
int main()
{
SimulateDoublyLinkedList();
}
When you remove the first element of a list, the caller must update its reference to the list. So your Delete function must return the pointer to the first element of the resulting list.
In this kind of operation I also find useful to use pointers to pointers... this would simplify a lot your code.
try this for delete this should work -
as temp = head , the temp->next =null will make the whole list disappear
//just removed temp->next = null;
if(position==1) {
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
free(temp);
}
Edited --
make delete function to return the head pointer and in the switch case use this
cdscpp2013=Delete(cdscpp2013,position);
instead of this
Delete(cdscpp2013,position);
then change the same for deleteall function - it should work :)

Resources