ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey im trying my luck at creating a linked list and im not sure if the program ive written is actually doing this. I find feedback a good learning expiance but being that im teaching myself and no one around me knows programming im asking here. I hope no one minds
here is the code
Code:
#include<stdio.h>
#include<stdlib.h>
struct listnode
{
char data;
struct listnode *nextptr;
};
int main()
{
char dat;
typedef struct listnode LISTNODE;
typedef LISTNODE *LISTNODEPTR;
int choice = 1;
char datenter[10];
LISTNODEPTR newptr;
LISTNODEPTR current = NULL;
do {
printf("enter 1 to add a list item 2 to quit\n");
printf(":>");
scanf("%s",datenter);
sscanf(datenter,"%d",&choice);
fflush(stdin);
if (choice == 1)
{
newptr = malloc (sizeof(LISTNODE));
if (newptr == NULL)
{
printf("Not enough memory to allocate\n");
return 1;
}
printf("Please enter a charecter to add to the list\n");
printf(":>");
scanf("%s",datenter);
sscanf(datenter,"%c",&dat);
fflush(stdin);
newptr->data = dat;
newptr->nextptr = current;
current = newptr;
}
}while(choice != 2);
do
{
if ( current != NULL)
{
printf("%c",current->data);
current = current->nextptr;
}
if (current == NULL)
printf("\nEnd of list\n");
}while(current != NULL);
return 0;
}
do you see what I'm getting at?
seeing as your working buffer is char datenter[10] why not make list.node = char [10] or malloc the space and use a pointer.
Because i dont want the structures to hold the entire string but rather just a charecter enterd. I m only doing it this way to allvate the \n that is left in the stdin. I tried just suing getc and fflush on stdin but that wasent working. So i did it this way by taking all the char entered by the user up to 10 and then parsing though the input to get the char i wanted.
Well, this isn't really a linked list, but its on its way. In order for this to be a linked list, a usable one anyway, you need to maintain "head" pointer (pointer to the first node). Also, I probably wouldn't put the struct defintion and typedefs inside main, they should really be outside of main.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.