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");
}
ilovephil
0
Light Poster
Recommended Answers
Jump to Post>>#define p printf
>>#define s scanfHorrible 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 …
Jump to Post>>#define p printf
>>#define s scanfHorrible misuse of macros. Don't be so lazy and just type out the word printf and scanf when needed.
Amen.
All 7 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
VernonDozier
2,218
Posting Expert
Featured Poster
ilovephil
0
Light Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
zeroliken
79
Nearly a Posting Virtuoso
ilovephil
0
Light Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
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.