I have created here a C Program in TurboC++. This simple program goal is to just create a linked list, insert every element at the end of the list and then print the value of the nodes.(Edited The program is somewhat changed to make it easier for me to understand but still the problem of nodes not being priniting exists)
The problem is, some of the node elements are not printing
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<ctype.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node ND;
void main()
{
ND *start,*current,*temp;
start=NULL;
do
{
temp=(ND*)malloc(sizeof(ND));
printf("\n Enter Node Value");
scanf(" %d",&temp->data);
temp->link=NULL;
if(start==NULL)
{
start=current=temp;
}
else
{
current->link=temp;
current=temp;
}
fflush(stdin);
printf("\nDo You Want TO Continue?(Y/N)");
}while(toupper(getchar())!='N');
current=start;
printf("\nThe Elements OF Linked List ARE:");
while(current!=NULL)
{
printf(" %d",current->data);
current=current->link;
}
}
You printing list from wrong element. You should start from head.
like:
temp1 = head;
while(temp1!=NULL)
{
printf(" %d",temp1->data);
temp1=temp1->link;
}
By the way your head element will be always NULL.
Here is correct way to add elements to list:
if (head == NULL)
{
head = temp;
}
else
{
temp1 = head;
while (temp1->link != NULL)
{
temp1 = temp1->link;
}
temp1->link = temp;
}
The program is now working correctly i have made some changes to make this program easier to understand.Here's the code:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node ND;
void main()
{
ND *head,*tail,*temp1;
int n;
char ch;
printf("\nEnter Data?(y/n)\n");
scanf(" %c",&ch);
fflush(stdin);
if(ch=='y' || ch=='Y')
{
tail=(ND*)malloc(sizeof(ND));
printf("\n Enter Node Value");
scanf(" %d",&n);
tail->data=n;
tail->link=NULL;
head=tail;
printf("\nDo You Want TO Continue?(Y/N)");
scanf(" %c",&ch);
fflush(stdin);
}
while(ch=='y' || ch=='Y')
{
printf("\n Enter Node Value");
scanf(" %d",&n);
tail->link=(ND*)malloc(sizeof(ND));
tail->link->data=n;
tail->link->link=NULL;
tail=tail->link;
printf("\nEnter More Data?(y/n)\n");
scanf(" %c",&ch);
fflush(stdin);
}
printf("\nElements Of Linked List Are:");
temp1=head;
while(temp1!=NULL)
{
printf(" %d",temp1->data);
temp1=temp1->link;
}
printf("\n");
fflush(stdout);
getch();
}
Related
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;
}
Can you please help me to understand why the while loop does not work in the following code:
#include<stdio.h>
#include<stdlib.h>
typedef struct node_type{
int data;
struct node_type *next;
}node;
typedef node *list;
list head;
void main()
{
list temp,new;
head=NULL;
int n;
char ch;
temp=(list) malloc(sizeof(node));
printf("\nGive data: ");
scanf("%d",&temp->data);
head=temp;
printf("\n");
printf("Enter Data? (y/n): ");
scanf("%c",&ch);while(getchar()!='\n');
while(ch=='y'||ch=='Y')
{
new=(list) malloc(sizeof(node));
printf("Give data: ");
scanf("%d",&new->data);while(getchar()!='\n');
temp->next=new;
temp=new;
printf("\nEnter more data(y/n): ");
scanf("%c",&ch);while(getchar()!='\n');
}
temp->next=NULL;
traverse(head);
}
Output is as follows:
Give data: 2
Enter more data(y/n): y
2
and the program terminates here.
You Don't Need to use while(getchar()!='\n'); everywhere before scanf(). This has lead to termination of your program. You could use scanf("\n%c", &ch); instead.Also it is recommended to write new->next = NULL; after new = (list)malloc(sizeof(node));.
Additionally you miss traverse() function.
Try this modified code :-
#include <stdio.h>
#include <stdlib.h>
typedef struct node_type
{
int data;
struct node_type *next;
} node;
typedef node *list;
list head;
void traverse(node *head) // This is simple traverse function just to demonstrate code.
{
while (head != NULL)
{
printf("%d ", head->data);
head = head->next;
}
}
void main()
{
list temp, new;
head = NULL;
int n;
char ch;
temp = (list)malloc(sizeof(node));
temp->next = NULL;
printf("\nGive data: ");
scanf("%d", &temp->data);
head = temp;
printf("\n");
printf("Enter Data? (y/n): ");
scanf("\n%c", &ch);
while (ch == 'y' || ch == 'Y')
{
new = (list)malloc(sizeof(node));
new->next = NULL;
printf("Give data: ");
scanf("%d", &new->data);
new->next = NULL;
temp->next = new;
temp = new;
printf("\nEnter more data(y/n): ");
scanf("\n%c", &ch);
}
temp->next = NULL;
traverse(head);
}
Output :-
Give data: 2
Enter Data? (y/n): y
Give data: 22
Enter more data(y/n): y
Give data: 32
Enter more data(y/n): n
List is : 2 22 32
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main()
{
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf("%c",&ch);
while(ch=='y'||ch=='Y')
{
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=head;
head=temp->next;
printf("Want to add more data:\n");
scanf("%c",&ch);
}
printlist(head);
return 0;
}
this is my code.
my problem is here that I cannot and data in my list but the node is added ...
I think there is something wrong in my "scanf" function....
please help me to solve this problem and send me the corrected code
thank u...hope I can get a reply soon
Try changing head=temp->next to head=temp. You are assigning head to itself again.
In addition to the above answers, if you wish to maintain the order in which the elements are added to a linked list (the head always remains fixed, only changes to point to the first element if it was NULL initially), the following adjustments take care of that. Any new element is always added to the end of the linked list with the head fixed at the first element.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main(){
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf("%c",&ch);
while(ch=='y'||ch=='Y'){
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=NULL;
if(head == NULL){
head = temp;
}
else{
list temp2 = head;
while(temp2->next != NULL){
temp2 = temp2->next;
}
temp2->next = temp;
}
printf("Want to add more data:\n");
scanf(" %c",&ch);
}
printlist(head);
return 0;
}
Change your code as below. scanf("%c",&ch); to scanf(" %c",&ch); and head=temp->next; to head=temp; For Scanf see below link
See link scanf() function doesn't work?
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node* next;
}Node;
typedef Node* list;
void printlist(list n)
{
while(n!=NULL)
{
printf("%d ",n->info);
n=n->next;
}
}
int main()
{
printf("Hello world!\n");
list head,temp;
char ch;
head=NULL;
printf("Want to add data:\n");
scanf(" %c",&ch);
while(ch=='y'||ch=='Y')
{
temp=(list)malloc(sizeof(Node));
scanf("%d",&temp->info);
temp->next=head;
head = temp;
printf("Want to add more data:\n");
scanf(" %c",&ch);
}
printlist(head);
return 0;
}
I am only able to view the very first element of the input.The code is given as follows:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct d_list{
int data;
struct d_list *next;
struct d_list *prev;
}node;
// Without recursively calling the insert function.
typedef node *list;
main(){
printf("Enter data(y/n)?");
char ch;
scanf("%s",&ch);
int n;
list head;
list temp;
list tail;
head=tail=temp=NULL;
if(ch=='y'||ch=='Y'){
printf("Enter data");
scanf("%d",&n);
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=temp->prev=NULL;
head=temp;
tail=temp;
}
printf("Enter more data..?(y/n)");
scanf("%s",&ch);
while(ch=='y'||ch=='Y'){
printf("Enter data");
scanf("%d",&n);
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->prev=tail;
temp->next=NULL;
tail->next=temp;
tail=temp;
printf("Enter more data..?(y/n)");
scanf("%s",&ch);
}
temp=head;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
getch();
}
I want to display all the inputs in the order they were input.What is the problem here?
Your logic of the code is correct but here is the error: make scanf("%s",&ch); as scanf(" %c",&ch);
Try this, simplest code for your problem, the changes I have made are,
changed scanf("%s",&ch); to scanf("%c",&ch);
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef struct d_list{
int data;
struct d_list *next;
struct d_list *prev;
}node;
typedef node *list;
int main()
{
char ch;
int n;
list head = NULL,temp = NULL,tail = NULL;
do{
printf("\nEnter data ?(y/n):\n");
scanf("%c",&ch);
if(ch=='n'||ch=='N')
break;
printf("\nEnter data :\n");
scanf_s("%d",&n);
temp=(list)malloc(sizeof(node));
temp->data=n;
temp->next=NULL;
if(head == NULL)
{
temp->prev=NULL;
head=temp;
}
else
{
temp->prev=tail;
tail->next=temp;
}
tail=temp;
}while(ch=='y'||ch == 'Y');
temp=head;
while(temp!=NULL){
printf("\n%d",temp->data);
temp=temp->next;
}
getch();
return 0;
}
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 :)