Why my linked list is displaying garbage values - c

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

Related

Why is this code not running on vs code while it's running fine on onlinegdb

It stops while giving input in vs code.... while it works on online c compiler, onlinegdb.
What should I do so it works in vs code too, all the settings in vs code also seem to be fine but somehow this code is not working
enter image description here
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
int exp;
struct node *next;
};
int main()
{
int n,i=0;
struct node *head,*newnode,*temp;
head=0;
printf("Enter number of elements:");
scanf("%d",&n);
while(i<n)
{
newnode= (struct node*)(malloc(sizeof(newnode)));
printf("Enter data:");
scanf("%d",&newnode->data);
printf("Enter Power:");
scanf("%d",&newnode->exp);
newnode -> next=0;
if(head==0)
{
head=temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
temp->next=head;
i++;
}
struct node *temp1;
if(head==0)
{
printf("Empty list");
}
else
{
temp1=head;
while(temp1->next !=head)
{
printf("%d(x)^%d",temp1->data,temp1->exp);
temp1=temp1->next;
if((temp1->data)<0)
{
printf(" ");
}
else
{
printf(" + ");
}
}
printf("%d(x)^%d",temp1->data,temp1->exp);
}
}
newnode= (struct node*)(malloc(sizeof(newnode)));
is wrong. This line ls allocating only for one pointer while a space for the structure is required. Also casting the result of malloc() is considered as a bad practice.
It should be
newnode= malloc(sizeof(*newnode));
or
newnode= malloc(sizeof(struct node));

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.

Singly Linked List in C (Warning)

I am trying to implement singly linked list. Is this correct? Getting this warning "non portable pointer conversion". How do i check if the SLL is working? Is it connected to each other? By the way, I am using Turbo C. I am still in this creating and inserting the nodes part.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void creat(int *num)
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
new_node->data=num;
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
}
main()
{
int binrange,max=100,n,i,divi;
clrscr();
printf("enter range: ");
scanf("%d",&binrange);
n=max/binrange;
divi=max/n;
for(i=0;i<=max;i++)
{
if(i%divi==0 && i>0)
{
//create nodes here
//store i into nodes
creat(&i);
}
}
getch();
}
new_node->data=num;
should be
new_node->data=*num;
num is a pointer and *num gives the value to be stored in the new_node->data
Assigning pointer to a variable of type int is giving a valid warning.

creating a binary search tree with strings

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
struct node{
char *name;
struct node *lchild;
struct node *rchild;
}*root;
void find(char *str,struct node **par,struct node **loc)
{
struct node *ptr,*ptrsave;
if(root==NULL)
{
*loc=NULL;
*par=NULL;
return;
}
if(!(strcmp(str,root->name)))
{
*loc=root;
*par=NULL;
return;
}
if(strcmp(str,root->name)<0)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(!(strcmp(str,ptr->name)))
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(strcmp(str,ptr->name)<0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}
void insert(char *str)
{
struct node *parent,*location,*temp;
find(str,&parent,&location);
if(location!=NULL)
{
printf("Name already present\n");
return;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->name=str;
temp->lchild=NULL;
temp->rchild=NULL;
if(parent==NULL)
root=temp;
else
if(strcmp(str,parent->name)<0)
parent->lchild=temp;
else
parent->rchild=temp;
}
void displayin(struct node *ptr)
{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
displayin(ptr->lchild);
printf("%s -> ",ptr->name);
displayin(ptr->rchild);
}
}
int main()
{
root=NULL;
char str[20];
while(1)
{
printf("Enter name: ");
fflush(stdin);
gets(str);
insert(str);
printf("Wants to insert more item: ");
if(getchar()=='y')
insert(str);
else
break;
}
displayin(root);
getch();
getchar();
return 0;
}
If i run this piece of code with following input
rakesh
rajesh
bimal
then,it is displaying the output as "bimal->" which is wrong. I don't know where the logic is going wrong. I crosschecked but not able to find mistake. Can somebody take a look on this.
One of the issue:
In your insert() function you are doing
temp=(struct node*)malloc(sizeof(struct node));
temp->name=str; //this is not correct,
//do
temp=malloc(sizeof(struct node)); // no type cast for malloc
temp->name = strdup(str); //allocate memory too
//also check you NULL and free the allocated memory.
Your are just setting the pointer location in the node you created for string to store, but it points to str array from main(). So all nodes will point to same location, which will have last value entered. In your case its "bimal".
Your find function is in any case quite obscure. This is the improved version.
void find(char *str,struct node **par,struct node **loc)
{
*par = NULL;
*loc = NULL;
struct node *ptr,*ptrsave;
if(root==NULL) return;
if(!(strcmp(str,root->name)))
{
*loc=root;
return;
}
ptrsave = NULL;
ptr = root;
while(ptr!=NULL) {
if(!(strcmp(str,ptr->name))) break;
ptrsave = ptr;
if(strcmp(str,ptr->name)<0)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=ptr;
*par=ptrsave;
}

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