Script List


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script List
# 1  
Old 10-22-2014
Script List

I am looking at these two scripts and was wondering what would the list look like in each?

Code:
int main()
{
        int i;
        int rV = 0;
        List l = NULL;
        char* input[] = { "06", "24", "3" };
        sNode *s, *t;

        for( i=0; i<3; ++i )
        {
                t = (sNode*)malloc( sizeof( sNode ));
                if( t == NULL )
                {
                        fprintf( stderr, "Couldn't get memory for a node!  Exiting." );
                        rV = 1;
                        break;
                }
                t->data = input[i];
                t->next = l;
                l = t;
        }

        /* What does this list look like here? */

        s = l;
        while( s != NULL )
        {
                t = s->next;
                free( s );
                s = t;
        }

        return rV;
}

Code:
import sys

main( args=sys.argc ) :

        L = [24, None]

        t = [13, None]
        t[1] = L
        L = t

        t = [28, None]
        t[1] = L[1]
        L[1] = t

        t = [3, None]
        p = L
        while p != None :
                q = p
                p = p[1]

        if p == L :
                L = t
        else :
                q[1] = t

        print L

# 2  
Old 10-22-2014
Your first program is incomplete, List and sNode aren't defined anywhere, which is why it looks so weird. I can probably assume list and snode are the same, so they look like this:

Code:
typedef struct sNode {
        struct sNode *next;
        char *data;
};

#define List sNode

It is a linked list, where each element holds a reference to the next one, or a NULL reference if the list is ending.

After the first loop it looks like this:

Code:
L -> ["06"] -> NULL

Second loop:
Code:
L -> ["24"] -> ["06"] -> NULL

etc.

You can see how they create a new node with "malloc", set it to point to the old node t->next=l; and then make the root point to it l=t;

I cannot grasp that python code at all, so I can't tell how or if it's related to this, though I suspect python doesn't actually have a linked list -- it's a higher level language, operating above the level where you'd know or care how it remembers what elements go where.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-22-2014
Quote:
Originally Posted by Corona688
Your first program is incomplete, List and sNode aren't defined anywhere, which is why it looks so weird. I can probably assume list and snode are the same, so they look like this:

Code:
typedef struct sNode {
        struct sNode *next;
        char *data;
};

#define List sNode

It is a linked list, where each element holds a reference to the next one, or a NULL reference if the list is ending.

After the first loop it looks like this:

Code:
L -> ["06"] -> NULL

Second loop:
Code:
L -> ["24"] -> ["06"] -> NULL

etc.

You can see how they create a new node with "malloc", set it to point to the old node t->next=l; and then make the root point to it l=t;

I cannot grasp that python code at all, so I can't tell how or if it's related to this, though I suspect python doesn't actually have a linked list -- it's a higher level language, operating above the level where you'd know or care how it remembers what elements go where.
Thank you! That helps a lot.
I am a little confused on this one also. What would this display?

Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#define TABLE_SIZE  7
#define NUM_INPUTS  7

int hash( char *s )
{
        return strlen( s ) % TABLE_SIZE ;
}

typedef struct entry
{
        char *key;
        int     val;
        struct entry *next;
} entry;

entry* table[ TABLE_SIZE ] = { NULL };

void insert( char *s, int v )
        /* this insert is NOT checking for duplicates.  :/ */
{
        int h = hash( s );
        entry *t = (entry*) malloc( sizeof( entry ));

        t->key = s;
        t->val = v;
        t->next = table[h];
        table[h] = t;
}

void clean_table()
{
        entry *p, *q;
        int i;

        for( i=0; i<TABLE_SIZE; ++i )
        {
                for( p=table[i]; p!=NULL; p=q )
                {
                        q = p->next;
                        free( p );
                }
        }       // for each entry
}       // clean_table


int main()
{
        char* keyList[] = { "Jaga", "Jesse", "Cos", "Kate", "Nash", "Vera",
                "Bob" };

        int valList[] = { 24, 78, 86, 28, 11, 99, 38 };

        int i;

        for( i=0; i<NUM_INPUTS; ++i )
                insert( keyList[i], valList[i] );

        /* what does the table look like here? */

        clean_table();

        return( 0 );
}

# 4  
Old 10-23-2014
This is a simplified representation of how the table array would look:

Code:
table[0] (Undefined)
table[1] (Undefined)
table[2] (Undefined)
table[3] => {"Bob", 38} -> {"Cos", 86}
table[4] => {"Vera", 99} -> {"Nash", 11} -> { "Kate", 28 } -> {"Jaga", 24 }
table[5] => {"Jesse", 78}
table[6] (Undefined)


Last edited by Chubler_XL; 10-23-2014 at 01:39 AM.. Reason: Values inserted at front of lists - not end
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 10-25-2014
Quote:
Originally Posted by Chubler_XL
This is a simplified representation of how the table array would look:

Code:
table[0] (Undefined)
table[1] (Undefined)
table[2] (Undefined)
table[3] => {"Bob", 38} -> {"Cos", 86}
table[4] => {"Vera", 99} -> {"Nash", 11} -> { "Kate", 28 } -> {"Jaga", 24 }
table[5] => {"Jesse", 78}
table[6] (Undefined)

Thank you ^^ but can you explain how you got that?
Also, I was wondering if it was possible to write a function that takes a key and a reference to an integer and fills in the reference with the appropriate value, while returning true? If possible, how would you write it?

Last edited by totoro125; 10-25-2014 at 11:42 PM..
# 6  
Old 10-26-2014
entry* table[ TABLE_SIZE ] = { NULL }; this statement initializes the "table" array as a TABLE_SIZE (7) element array with NULL pointers. In C arrays are indexed from zero so we end up with:

Code:
table[0] (Undefined)
table[1] (Undefined)
table[2] (Undefined)
table[3] (Undefined)
table[4] (Undefined)
table[5] (Undefined)
table[6] (Undefined)

I use (Undefined) here, as the table array element contains a NULL pointer. In the context of this code (empty) could be a better analogy.

The insert code calculates a hash value of the input string being the length of the string modulo the TABLE_SIZE (7). This is a pretty simple hash which results in strings of various lengths being inserted into the table array as follows:

Code:
table[0] ~ lengths     7, 14, 21, 28, etc
table[1] ~ lengths 1,  8, 15, 22, 29, etc
table[2] ~ lengths 2,  9, 16, 23, 30, etc
table[3] ~ lengths 3, 10, 17, 24, 31, etc
table[4] ~ lengths 4, 11, 18, 25, 32, etc
table[5] ~ lengths 5, 12, 19, 26, 33, etc
table[6] ~ lengths 6, 13, 20, 27, 34, etc

Looking at this code in the insert function:

Code:
        t->key = s;
        t->val = v;
        t->next = table[h];
        table[h] = t;

we can see that each element of table is a linked list and new values are inserted at the front of the list.

So "Jaga" is inserted into the table[4] list first, then later "Kate" is inserted in front of "Jaga" then "Nash", and so forth.

Here is the update code you requested:

Code:
int update( char *s, int v )
/* Update 1st found entry with a key matching s assign new val of v
    returns 1 if an entry is updated and 0 otherwise */
{
        int h = hash( s );
        entry *t;

        for(t=table[h]; t!=NULL; t=t->next)
            if (!strcmp(s, t->key)) {
                 t->val = v;
                 return 1;
            }
        return 0;
}


Last edited by Chubler_XL; 10-26-2014 at 05:30 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 10-26-2014
Quote:
Originally Posted by Chubler_XL
entry* table[ TABLE_SIZE ] = { NULL }; this statement initializes the "table" array as a TABLE_SIZE (7) element array with NULL pointers. In C arrays are indexed from zero so we end up with:

Code:
table[0] (Undefined)
table[1] (Undefined)
table[2] (Undefined)
table[3] (Undefined)
table[4] (Undefined)
table[5] (Undefined)
table[6] (Undefined)

I use (Undefined) here, as the table array element contains a NULL pointer. In the context of this code (empty) could be a better analogy.

The insert code calculates a hash value of the input string being the length of the string modulo the TABLE_SIZE (7). This is a pretty simple hash which results in strings of various lengths being inserted into the table array as follows:

Code:
table[0] ~ lengths     7, 14, 21, 28, etc
table[1] ~ lengths 1,  8, 15, 22, 29, etc
table[2] ~ lengths 2,  9, 16, 23, 30, etc
table[3] ~ lengths 3, 10, 17, 24, 31, etc
table[4] ~ lengths 4, 11, 18, 25, 32, etc
table[5] ~ lengths 5, 12, 19, 26, 33, etc
table[6] ~ lengths 6, 13, 20, 27, 34, etc

Looking at this code in the insert function:

Code:
        t->key = s;
        t->val = v;
        t->next = table[h];
        table[h] = t;

we can see that each element of table is a linked list and new values are inserted at the front of the list.

So "Jaga" is inserted into the table[4] list first, then later "Kate" is inserted in front of "Jaga" then "Nash", and so forth.

Here is the update code you requested:

Code:
int update( char *s, int v )
/* Update 1st found entry with a key matching s assign new val of v
    returns 1 if an entry is updated and 0 otherwise */
{
        int h = hash( s );
        entry *t;

        for(t=table[h]; t!=NULL; t=t->next)
            if (!strcmp(s, t->key)) {
                 t->val = v;
                 return 1;
            }
        return 0;
}

Thank you so much! This helps a lot =D
One last thing, would you happen to know how to run the python code? I know it's incomplete but I am not sure what to add so I can make it run and see the output.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Specifying a list name as argument and using that list in script.

Is there a way I can specify the name of a list as an argument to a shell script and then use the values of that list name in the script? I need to do this WITHOUT using case statements. Something like this: check.sh list1 #!/bin/bash list1="www.amazon.com www.google.com"... (9 Replies)
Discussion started by: gctaylor
9 Replies

2. Shell Programming and Scripting

Creating IN list in PLSQL script dynamically by using shell script

Hi all, I have a PLSQL script which has a IN list where it takes some ids as input. For example SELECT * FROM EMPLOYEE WHERE EMPLOYEE_ID IN (comma separated list ) I want to run this quest inside a shell script but I would like to prepare the IN list dynamically where the employee ids... (1 Reply)
Discussion started by: LoneRanger
1 Replies

3. Shell Programming and Scripting

List Files script

Hello everyone - I have the task to create a file list script that will list files in directory based on the parameters passed to the program. It has to be a C Shell - I mentioned that before but I got closed :) - the company only allows this shell for security purposes I guess. Anyway, here is... (4 Replies)
Discussion started by: adrianvas12
4 Replies

4. Shell Programming and Scripting

Find and list script

Hello All, ksh script. Please consider the following case . $ ls -l -rw-r----- 1 100 400 405143552 Mar 21 2010 bz_1_0000063547_561428818.arc -rw-r----- 1 100 400 404148224 Feb 19 09:55 bz_1_0000079359_561428818.arc -rw-r----- 1 100 400 405625856 Feb 19 14:30... (2 Replies)
Discussion started by: yoavbe
2 Replies

5. Shell Programming and Scripting

Splitting a list @list by space delimiter so i can access it by using $list[0 ..1..2]

EDIT : This is for perl @data2 = grep(/$data/, @list_now); This gives me @data2 as Printing data2 11 testzone1 running /zones/testzone1 ***-*****-****-*****-***** native shared But I really cant access data2 by its individual elements. $data2 is the entire list, while $data,2,3...... (1 Reply)
Discussion started by: shriyer
1 Replies

6. Shell Programming and Scripting

please help - script to list and rename

Hi Friend, I have a small script to list all file FFAAAAABBBBB00001 and FFAAAAABBBBB00001.repaired (when I run another script, the orginal file will output another *.repaired file) in my unix directory, and reaname the output file FFAAAAABBBBB00001.repaired back to FFAAAABBBBB00001. However, it... (2 Replies)
Discussion started by: happyv
2 Replies

7. Shell Programming and Scripting

help with script to list directories

Hi All I have two scripts which i used to try and list all the directories one using 'function', which only lists the first directory and does not show directories within directories. function ListDir () { for arg in $(ls $HOME) do if then echo $arg ... (2 Replies)
Discussion started by: chassis
2 Replies

8. Shell Programming and Scripting

List script problem

Hi all, First of all compliments on the forum here. Looks great and has lots of information, some of which I have been able to use. I am a relativ noob when it comes to Unix but already I have been fooling around with scripts for the company I work for here in switzerland! Thanks to this... (2 Replies)
Discussion started by: swissman24
2 Replies

9. UNIX for Dummies Questions & Answers

script that will list .c programs

hey, im trying to write a script that will list all the .c files, and give me the first 10 lines of code in them. I think ive got that bit working, but i want to make it use friendly so i can select whether i want to modify a .c file or delete it. (7 Replies)
Discussion started by: Saggas
7 Replies

10. Shell Programming and Scripting

Script to list changes in Directories

Hello guys it's me again, I need some help. What I'm doing is listing all the file and directories Recusively and using it a a master file. Then I need to do the same the nest day to make sure nothing was deleted or modified. What happen is file in one of out major directories was deleted without... (2 Replies)
Discussion started by: aojmoj
2 Replies
Login or Register to Ask a Question