Adding functionality to linked list implementation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NatsoumiMaya
    New Member
    • Feb 2008
    • 1

    Adding functionality to linked list implementation

    hi ppl,
    i have a code to write in C where i have to implement the mkdir, rm, rmdir and touch functions using double linked lists...im haveing trouble with it...here is what i have so far:
    [code=c]
    #ifndef _DIRECTORY_H
    #define _DIRECTORY_H

    /* tree node data structure declaration */

    struct entryNode {
    char * name;
    struct entryNode * next; /* sibling */
    struct entryNode * previous; /* sibling */
    int isDirectory;
    struct entryNode * parent;
    union {
    char * contents; /* if this node is a file, this contains the content of the file */
    struct entryNode * entryList; /* if this node is a directory,
    this contains a pointer to the first child inside the directory */
    } entry;
    };
    [/code]
    The rest of the file contains the function prototypes.


    [code=c]

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include "directory. h"

    #define TRUE 1
    #define FALSE 0

    #define MAX_TEXT_SIZE 8192

    /* used as a temp storage for the text content of a file */
    char tmpbuf[MAX_TEXT_SIZE];

    /* pointer to the root node of the file system */
    struct entryNode * root;

    /* Helper functions */
    void pwdHelper (struct entryNode *); /* don't worry about this one */


    /* Return a pointer to the entry with the given name in the given list,
    or NULL if no such entry exists. */
    /* basically it searches for the file/dir name at the current level:
    * it does not go into directories */
    struct entryNode * located (char * name, struct entryNode * list) {
    if (list == NULL) {
    return NULL;
    } else if (strcmp (list->name, name) == 0) {
    return list;
    } else {
    return located (name, list->next);
    }
    }

    /* implements the "touch" command (one argument; not in standard UNIX) */
    /* wd is a pointer to the current working directory */
    void touchFile (struct entryNode * wd, char * fileName)
    {

    struct entryNode * newFile;

    if (located (fileName, wd->entry.entryLis t))
    {
    printf ("touch: %s: File exists\n", fileName);
    return;
    }

    /* initialize temporary text buffer to zeros */
    bzero(tmpbuf,MA X_TEXT_SIZE);

    /* HELP NEEDED HERE*/
    }

    /* implements the "mkdir" command (one argument; no options) */
    void createDir (struct entryNode * wd, char * dirName) {
    struct entryNode * newDir;

    if (located (dirName, wd->entry.entryLis t))
    {
    printf ("mkdir: %s: File exists\n", dirName);
    return;
    }
    /*struct entryNode * newDir = (char *) malloc(sizeof(n ewDir));*/
    newDir->entry.entryLis t;
    newDir->next = NULL;
    /*newDir->previous = dirName;*/
    /*dirName->next = newDir;*/
    struct entryNode * entryList = NULL;
    /*HELP NEEDED WITH BOLDED TEXT*/
    }

    /* implements the "rm" command (one argument, unlike standard UNIX; no options) */
    void removeFile (struct entryNode * wd, char * fileName) {
    struct entryNode * file;
    file = located (fileName, wd->entry.entryLis t);

    if (file == NULL)
    {
    printf ("rm: %s: No such file or directory.\n", fileName);
    return;
    }

    else if (file->isDirectory)
    {
    printf ("rm: %s: is a directory.\n", fileName);
    return;
    }
    /*HELP NEEDED HERE*/
    }

    /* implements the "rmdir" command (one argument, unlike standard UNIX; no options) */
    void removeDir (struct entryNode * wd, char * dirName) {
    struct entryNode * dir;
    struct entryNode * tmp;

    dir = located (dirName, wd->entry.entryLis t);
    if (dir == NULL)
    {
    printf ("rmdir: %s: No such file or directory.\n", dirName);
    return;
    }

    else if (!dir->isDirectory)
    {
    printf ("rmdir: %s: Not a directory.\n", dirName);
    return;
    }

    else if (dir->name[0] =='/')
    {
    printf("rmdir: Cannot delete root directory.\n");
    return;
    }
    [B]else
    {
    previousfile = file->previous;
    nextfile = file->next;
    if (previousfile== NULL)
    {
    printf("%s: No Previous File");
    if (nextfile == NULL)
    {
    printf("%s: No Next File");
    }
    free(file);
    wd->entry.entryl it = NULL;
    }
    else if
    {
    if (nextfile == NULL)
    {
    printf("%s: No Next File");
    if (previousfile== NULL)
    {
    printf("%s: No Previous File");
    }
    free(file);
    wd->entry.entryl it = NULL;
    }
    }
    else
    {
    free(file);
    wd->entry.entryl it = NULL;
    }
    }

    /* HELP NEEDED WITH BOLDED TEXT */

    /* implements the "mv" command (two arguments, unlike standard UNIX; no options) */
    void moveEntry (struct entryNode * wd, char * from, char * to) {
    struct entryNode * fromNode;
    struct entryNode * toNode;
    fromNode = located (from, wd->entry.entryLis t);
    if (fromNode == NULL)
    {
    printf ("mv: %s: No such file or directory.\n", from);
    return;
    }

    /*HELP NEEDED HERE */
    }
    [/code]
    THANK YOU IN ADVANCE FOR YOUR HELP ^_^
    Last edited by sicarie; Feb 10 '08, 01:35 PM. Reason: WTF. Code tags, mate. BOLD AND ALL CAPS EVERYWHERE IS FREAKING ANNOYING!
Working...