Tuesday, November 2, 2010

Menstruation Leg Numbness

[C] Implementation of a stack

lists. fundamental structures of C, which allow flexibility 'Code impressive. No more 'fixed values \u200b\u200band array static, no more' malloc and calloc ahead of time. New superheroes (I) have come from space, called Lists As I said, the lists allow you to manage data in a very flexible, allows us to organize data structures also did not know in advance how much data should immaganizzare. Today we see an implementation of a very basic list, the singly-linked list or simply
batteries.
The structure and 'basic, you can' graph like this:

5 and 'the figure of the first node, and the arrow' pointer to next node, which in turn has its data and '\\' indicates that there are no other nodes.

in batteries, the data will be stored in the order
LIFO ,
last in, first out
: The latest data will be the last to leave.

The initial structure will be 'like that (read the comments, I'll explain everything there!;) # include
\u0026lt;stdlib.h> # Include / * for malloc. * /
/ * extreme flexibility ': *
I simply called an "alias" with typedef of int, that henceforth we will call the
* "type". But instead of that 'int' we can '
 * be any type (char, float), it does not matter. The list will 
* trattera'allo same way.
* /
typedef int type;
typedef struct list {
type date / * given a generic node. * / Struct list * next
;
/ * a pointer to the next node.
* Warning! E '* next must define the type struct list *
' cause the alias "List" is not ' *
yet been defined with typedef.
* /} List
/ * Define the alias "List". * /



But here, we define the main functions, data entry and printing:


printList void (* List node)

{/ * there are nodes finquando ...*/
while (node! = NULL) {
/ * ... Mold node-> data ...*/
printf ("% d ->" node-> data);
 / * ... and step to the next node. * / 
node = node-> next;}

printf ("NULL \\ n");}


void AddNode (List ** node, type, date) {

List * newNode;
newNode = malloc (sizeof (List *));
if (newNode)
{puts ("error: out of memory.");
exit (EXIT_FAILURE);}

newNode-> data = date;
/ * check to newNode-> next pointer *
pointing to the top of the list.
* /
newNode-> next = * node;
/ * newNode will point 'so' to the new node will be '*
top of the list.
* /
* node = newNode;}


int main (void) {

List * node = NULL;
/ * and 'right to assign the value' NULL 'when beginning
* There are no knots.
* /
AddNode (& node, 5);
AddNode (& node, 10);
printList (node);
return 0;}




I think it is very complex. :) The batteries are many interesting applications, mention two interesting ways:




When we need to manage data, but we do not know their content 'in advance.
can avoid 'to invent methods that do not comply with the ANSI C to avoid
array to create more' big as they serve:

  • int numeroDati; printf ("How much you want to add:"); scanf (" % d ", & numeroDati);
    int mio_array[numeroDati];
      
    questo metodo e' molto comune, ma secondo lo standard ANSI le dichiarazioni di variabili vanno sempre effettuate all'inizio del loro blocco.



    Quando dobbiamo valutare delle espressioni o una determinata sintassi.
    I nodi ci possono offrire un punto di appoggio di espressioni che devono
    essere valutate, uno stack appunto. Basti pensare ai compilatori che ancora oggi utilizzano questo metodo per valutare le espressioni e la sintassi.

  • Penso di essere stato abbastanza chiaro. In ogni caso ci sono i commenti, scrivete e risponderò per quanto posso. Al prossimo tutorial,
    bye.


0 comments:

Post a Comment