LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-25-2006, 04:32 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
C linked list


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;


	}

Last edited by exvor; 04-28-2006 at 05:24 AM.
 
Old 04-25-2006, 05:21 PM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
whats at
Code:
  
&(listnode->data)+1
after
Code:
scanf("%s",datenter);
   sscanf(datenter,"%c",&dat);
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.


and the following can be done in one go
Code:
typedef struct listnode LISTNODE;
	   	typedef LISTNODE *LISTNODEPTR;
Code:
typedef struct listnode
{
    char[10] data;
    struct listnode *nextptr;
}LISTNODE, *LISTNODEPTR;
Code:
newptr->nextptr = current;
current = newptr;
the above is correct, but i just prefer (it just seems clearer)
Code:
newptr->nextptr = 0;//just a good idea
current->nextptr = newptr;
current = newptr;

Last edited by dmail; 04-25-2006 at 05:38 PM.
 
Old 04-27-2006, 05:52 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
Old 04-27-2006, 06:10 PM   #4
Vagrant
Member
 
Registered: Nov 2001
Posts: 75

Rep: Reputation: 15
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.
 
Old 04-28-2006, 05:25 AM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
I posted the finshed code if anyone was intrested. Its more of a stack then a list tho. Im trying a list now but im getting stuck even with that.

Last edited by exvor; 05-06-2006 at 09:28 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Linked List question lowpro2k3 Programming 3 06-16-2005 10:15 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM
C++ linked list fun chens_83 Programming 2 08-04-2003 07:40 AM
book linked list in C jetfreggel Programming 14 03-16-2003 10:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration