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;
}
Related
I have tried to create and display singly linked linked list using separate functions. the elements would be entered by the user. my program is taking input but not displaying those elements. any help would be greatly appreciated
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void create(int n, struct node *newnode, struct node *prev, struct node *head){
for(int i=0; i<n; i++){
printf("Enter the data for node %d: ", i+1);
scanf("%d", &newnode-> data);
if(head==NULL){
head = newnode;
head-> next = NULL;
prev = head;
}
else{
prev->next= newnode;
newnode->next= NULL;
prev=newnode;
}
}
}
void display(int n, struct node *temp){
printf("%d ", temp->data);
temp=temp->next;
}
int main()
{
struct node *newnode, *prev, *head, *temp;
newnode = (struct node*)malloc(sizeof(struct node *));
head=NULL;
int n, choice;
printf("Number of elements in your linked list: ");
scanf("%d", &n);
create(n, newnode, prev, head);
printf("\n1.Display");
printf("Enter the operation you would like to perform: ");
scanf("%d",&choice);
if(choice==1){
temp=head;
display(n, temp);
}
return 0;}
#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;
}
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node* HEAD;
void Deleteatn(int c)
{
struct node *store,*store1,*temp=HEAD;
if(c==1)
{
store=temp->next;
store->prev=NULL;
HEAD=store;
free(temp);
}
else
{
for(int i=1;i<c-1;i++)
temp=temp->next;
store=temp->next;
store1=store->next;
temp->next=store->next;
//store1->prev=temp;//DOUBT
free(store);
}
}
void print()
{
struct node *temp=HEAD;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void Insertatend(int b)
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
n->data=b;
n->prev=NULL;
n->next=NULL;
if(HEAD==NULL)
HEAD=n;
else
{
struct node *store,*temp=HEAD;
while(temp!=NULL)
{
store=temp;
temp=temp->next;
}
store->next=n;
n->prev=store;
}
}
int main()
{
int a,b,c;
printf("How many Numbers need to be inserted?\n");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
printf("Enter a number\n");
scanf("%d",&b);
Insertatend(b);
}
printf("The List is\n");
print();
printf("Enter which node need to be deleted?\n");
scanf("%d",&c);
Deleteatn(c);
printf("The List After Deletion is\n");
print();
return 0;
}
Here I have written a program to delete a nth node in a doubly linked list ,I have a doubt that without making a Backward link,how does the output come correctly.So in doubly linked list is it not mandatory to make both forward and backward link?
I have mentioned the code line as doubt,without that code,how is it working???
You can find the issue when you traverse back.
Since you are traversing in forward direction its working fine like a singly linked list.
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();
}
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;
}