Simple program with list

This is a discussion on Simple program with list within the C Programming forums, part of the General Programming Boards category; Who can qiuckly write me a simple program with list based on smth like that: Code: struct s { double ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Unhappy Simple program with list

    Who can qiuckly write me a simple program with list based on smth like that:

    Code:
    struct s {
           double nr;
           struct s *next;
    };
    and with functions to:
    - print the list
    - add element at the beggining
    - add element at the end
    - add element after given value
    - add element as sorted
    - delete one element
    - delete all elements of given value
    - delete all elements

    with some easy to understand names (of functions, variables etc) and with some comments to help me to understand program.

    And i have a question? Why the hell is it double, and it doesn't work with int?

    Please help me i have to show and it and explain to my teacher in few hours... :/

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    How not to get replies:
    Quote Originally Posted by SebastianB
    Who can qiuckly write me a simple program with list based on smth like that:

    <snip>

    Please help me i have to show and it and explain to my teacher in few hours... :/
    http://cboard.cprogramming.com/annou...ouncementid=39
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    Well, okay then - if you want - here is my program. I wrote it but I don't understand it so who can help me (maybe by changing names and putting some comments)?

    Code:
    // Labs005.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    struct s {
           double d;
           struct s *p;
    };
    
    struct s *inite(struct s *h);
    struct s *pute(struct s *h);
    void gete(struct s *h);
    void removee(struct s *h);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	struct s *h;
    
      h=NULL;
    
      h=inite(h);
      pute(h);
      gete(h);
      removee(h);
    
      system("PAUSE");   
      return 0;
    
    
    }
    
    
    
    struct s *inite(struct s *h) {
         struct s *p;           
         
         p = (struct s *) malloc(sizeof(struct s));
         if (p==NULL) printf("Pointer error");
         printf("Enter value: ");
         scanf("%lf", &(p->d));
         p->p=h;
         return p;
    }
    
    struct s *pute(struct s *h) {
         struct s *p, *q;
    
         p = (struct s *) malloc(sizeof(struct s));
         if (p==NULL) printf("Pointer error");
         
         p->p=NULL;
         printf("Enter value: ");
         scanf("%lf", &(p->d));
         
         if(h==NULL) return p;
         else {
             q=h;
             while  (q->p!=NULL) q=q->p;
             q->p=p;
             return h;
              }
    }
    
    void gete(struct s *h) {
         while (h!=NULL) {
               printf("\n%lf", h->d);
    		   h=h->p;
         }
         printf("\n");
    }
    
    void removee(struct s *h) {
         struct s *p;
    
         while (h!=NULL) {
               p=h;
    		   h=h->p;
    		   free(p);
    		   }
         free(h);
    }
    I know it seems strange that i wrote it without understanding it but it's true... :?
    Last edited by SebastianB; 01-22-2006 at 09:14 PM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    I have only a few questions. Anybody can help me? Maybe by some chat or smth like that? Please - only 1 hour left :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Replies: 6
    Last Post: 03-02-2005, 01:45 AM

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