linked list program

This is a discussion on linked list program within the C Programming forums, part of the General Programming Boards category; well.. this is the starting of my project of C introduction.. and i wanna know if there is some problem ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    8

    linked list program

    well.. this is the starting of my project of C introduction..
    and i wanna know if there is some problem with the code.. and how can it be optimized or sumthing..
    thanks
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    line 12, usually I like to approach this by using a pointer rather than a student, but it's up to you. Also, usually I like to use a void pointer, since then you can store anything in the linked list.
    line 28, 59, and 67, you don't need to cast malloc.
    line 57 and 60, consider a different variable name from new since that is a C++ keyword. Obviously it doesn't really matter until you randomly decide to use a c++ compiler, but it's just nice to avoid.
    line 60 and 61, correct me if I'm wrong, but can't you use curr->data and curr->next instead of having to dereference it? Also look at 69, 71, 82, 83, 97 (2x), and 98


    Everything I say are only suggestions and do not need to be followed by any means.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    The code, so you don't have to download it just to see.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct student
    	{
    	char name[15];
    	char surname[15];
    	};
    
    struct node
    	{
    	struct student data;
    	struct node* next;
    	};
    
    
    struct node* head=NULL;
    struct node* curr;
    void output();
    void input();
    void append();
    void save();
    
    int main(void)
    {	
    	int option;
    	FILE *fl;
    	struct student* temp = (struct student*)malloc(sizeof(struct student));
    
    	fl = fopen("agenda.dat","rb");
    	if (fl != NULL)
    		while(fread(temp,sizeof(struct student),1,fl)!=(size_t)NULL)
    			{
    			append(temp);
    			}
    
    	do 
    		{
    		//system("cls");															//Windows Only...
    		printf("Menu\n1: Add\n2: View\n3: Save\n0: Exit\n\nOption: ");
    		scanf("%d",&option);
    		if(option==1)
    			input();
    		else if(option==2)
    			output();
    		else if(option==3)
    			save();
    			
    		}
    	while(option);
    	if (head != NULL)
    		save();
    	
    	
    	return(0);
    }
    void append(struct student* new)
    	{
    	curr = (struct node*)malloc(sizeof(struct node));
    	(*curr).data = *new;
    	(*curr).next = head;
    	head = curr;
    	}
    
    void input(void)
    	{
    	struct student* temp = (struct student*)malloc(sizeof(struct student));
    	printf("Name: ");
    	scanf("%s",(*temp).name);
    	printf("Surname: ");
    	scanf("%s",(*temp).surname);
    	append(temp);
    	free(temp);
    	}
    
    void output()
    	{
    	curr = head;
    	if (head!=NULL)
    		while(curr)
    			{
    			printf("\nName: %s\n",(*curr).data.name);
    			printf("Surname: %s\n",(*curr).data.surname);
    			curr=(*curr).next;
    			}
    	else
    		printf("Noobs\n");
    	}
    
    void save()
    	{
    	FILE *fl;
    	fl = fopen("agenda.dat","wb");
    	curr = head;
    	while(curr)
    		{
    		fwrite(&(*curr).data, sizeof((*curr).data), 1 , fl );
    		curr=(*curr).next;
    		}
    	fclose(fl);
    	}
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    8
    there is some diference betwen (*ptr).data and ptr->data?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by dlcp View Post
    there is some diference betwen (*ptr).data and ptr->data?
    No. They're exactly the same. However, the latter is much more commonly used.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:13 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  5. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 09:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21