Create and use lists in C... 
Author Message
 Create and use lists in C...

Hallo,
I need some help about pointers and structures.

I want create a List of record such as the following one:

        +--------+    +--------+    +--------+    +--------+
     +->|   Ptr -+----+-> Ptr -+----+-> Ptr -+----+-> Ptr -+-->NULL
     |  |  Memo  |    |  Memo  |    |  Memo  | +->|  Memo  |
     |  +--------+    +--------+    +--------+ |  +--------+
     |                                         |
   FirstPtr                                  LastPtr

I've defined the following structure :

   structure MemoElement
    {
     char Memo[20];           // field for data...
     MemoElement *ptr;        // pointer to the next record of the list
    };

and the pointers to the first and the last record of the list:    

    MemoElement *FirstPtr, *FirstPtr;

Is it right?
Does exist a better way for use lists?

Thanks in advice,
                 Stefano



Fri, 23 May 1997 21:11:08 GMT  
 Create and use lists in C...

Quote:
>    structure MemoElement
>     {
>      char Memo[20];           // field for data...
>      MemoElement *ptr;        // pointer to the next record of the list
>     };

    That should probably be "struct MemoElement *ptr". It may also
    be more efficient (depends on your system) to put ptr at the start
    of the MemoElement structure.

Quote:
> and the pointers to the first and the last record of the list:    

>     MemoElement *FirstPtr, *FirstPtr;

    I think this line might be mispelt. A more correct spelling might
    be...

        struct MemoElement *FirstPtr, *LastPtr;

Quote:
> Does exist a better way for use lists?

    It depends on what you want to do with the list. Small lists may
    sometimes be more efficiently implemented as an array. Even larger
    lists are sometimes more efficiently implemented with several
    elements per list node, for example...

        typedef struct MemoElement {
            struct MemoElement *pNext;
            int     count;
            char    memo[100][20];
        } MEMOEL;

    Some people like to declare the head of the list as an element
    rather than just a pointer...

        MEMOEL  FirstNode;

    so the list always starts with an empty element. It's one way of
    getting around having special-case code for handling the first
    element in the list. There are other ways of avoiding special-
    case code that I think are better.

    Some people like to implement a list as an array of pointers to
    elements, where empty elements have NULL pointers. For example...

        int     nMemo;
        char    *MemoList[128];

    then to add a new node would be something like...

            if (nMemo < sizeof(MemoList)/sizeof(MemoList[0])) {
                MemoList[nMemo] = malloc(20);
                if (MemoList[nMemo]) {
                    nMemo++;
                    return(1);
                }
            }
            return(0);

    My own preference for small lists with small nodes is to use a
    simple array...

        char    MemoList[128][20];      /* Max 128 items */

Quote:
>                  Stefano

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Sun, 01 Jun 1997 10:06:06 GMT  
 Create and use lists in C...
|> I need some help about pointers and structures.
|>
|> I want create a List of record such as the following one:
|>
|>         +--------+    +--------+    +--------+    +--------+
|>      +->|   Ptr -+----+-> Ptr -+----+-> Ptr -+----+-> Ptr -+-->NULL
|>      |  |  Memo  |    |  Memo  |    |  Memo  | +->|  Memo  |
|>      |  +--------+    +--------+    +--------+ |  +--------+
|>      |                                         |
|>    FirstPtr                                  LastPtr
|>  
|> I've defined the following structure :
|>
|>    structure MemoElement
|>     {
|>      char Memo[20];           // field for data...
|>      MemoElement *ptr;        // pointer to the next record of the list
|>     };
|>
|> and the pointers to the first and the last record of the list:    
|>    
|>     MemoElement *FirstPtr, *FirstPtr;
|>
|>
|> Is it right?
|> Does exist a better way for use lists?

No it is not right...You cannot use // as a comment and you have not defined
a type called MemoElement. Also 'structure' is not a valid keyword in C.
Assuming everything was correct you defined FirstPtr twice! The correct way
is:

struct MemoElement
{
  char Memo[20];             /* field for data... */
  struct MemoElement *ptr;   /* pointer to the next record of the list */

Quote:
};

struct MemoElement *FirstPtr, *LastPtr;

Now it should be a legal segment of code that can be compiled portable and
without syntax errors.
--





Mon, 02 Jun 1997 00:12:45 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. How to write c program to create linked list of staff records using ADT

3. Creating A DOS Directory File List Without Using SYSTEM

4. Compile CS source code using ICodeCompiler

5. Two CS files (using namespaces)

6. Survey on using C as intro CS langauge (?)

7. Survey on using C as intro CS langa

8. Using a list box to edit labels in a list control

9. Creating edit boxes using create function gives wincore 628 error

10. Help - Using Create to dynamiclly create a CFormView view

11. Attributes not created when ConstructorInfo.Invoke used to create object

12. Creating a window using FormView - without using CMultiDocTemplate

 

 
Powered by phpBB® Forum Software