How can i store strings in my program.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define p printf
#define s scanf
struct node
{
char nbook;
struct node *next;
};
typedef struct node *nodepointer;
void addbook(nodepointer &head,char book)
{
nodepointer newnode;
newnode=(nodepointer)malloc(sizeof(struct node));
newnode->nbook=book;
newnode->next=head;
head=newnode;
}
void display(nodepointer head)
{
if(head==NULL)
{
p("\nNothing to display");
}
else if(head!=NULL)
{
while(head->next!=NULL)
{
p("\n%c",head->nbook);
head=head->next;
}
p("\n%c",head->nbook);
}
}
main()
{
nodepointer head=NULL;
int choice;
char book;
do
{
p("\n[1]Add book");
p("\n[2]Display book");
p("\nKey choice: ");
s("%i",&choice);
switch(choice)
{
case 1:
p("\nEnter title of book");
s("\n%c",&book);
addbook(head,book);
break;
case 2:
display(head);
break;
}
}while(choice!=4);
system("pause");
}

>>#define p printf
>>#define s scanf

Horrible misuse of macros. Don't be so lazy and just type out the word printf and scanf when needed.

>>Link list strings stored rewrite this program

The structure is incorrect. You need to add the star to the declaration making nbook a pointer so that its length can be allocated to fit the size of the string you want to store there.

struct node
{
char* nbook;
struct node *next;
};

>>#define p printf
>>#define s scanf

Horrible misuse of macros. Don't be so lazy and just type out the word printf and scanf when needed.

Amen.

how about returning books using CHAR not int it is poissible?

how about returning books using CHAR not int it is poissible?

Huh? I have no idea what you mean.

how about returning books using CHAR not int it is poissible?

I don't see any books... variable in the code
And if you meant the similar variable, which function are you talking about?

I mean im using this
but im not ABLE To return the value of KEY
Invalid conversion from char to int
and i think it is not possible to return value using char data type?


int searching_book(nodepointer head,char *key)
{
nodepointer here;
here=head;
while(here!=NULL)
if(here->data==key)
{
return here->data;
}
else
{
here=here->next;
p("\nKey not found");
}
}

According to your original post, struct node does not contain an item named "data". Post your current, and accurate, code so that we know what the hell you are talking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.