473,465 Members | 4,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding functionality to linked list implementation

1 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. #ifndef _DIRECTORY_H
  2. #define _DIRECTORY_H
  3.  
  4. /* tree node data structure declaration */
  5.  
  6. struct entryNode {
  7.     char * name;
  8.     struct entryNode * next;        /* sibling */
  9.     struct entryNode * previous;    /* sibling */
  10.     int isDirectory;
  11.     struct entryNode * parent;
  12.     union {
  13.         char * contents; /* if this node is a file, this contains the content of the file */
  14.         struct entryNode * entryList; /* if this node is a directory,
  15.                                          this contains a pointer to the first child inside the directory */
  16.     } entry;
  17. };
  18.  
The rest of the file contains the function prototypes.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "directory.h"
  6.  
  7. #define TRUE 1
  8. #define FALSE 0
  9.  
  10. #define MAX_TEXT_SIZE 8192
  11.  
  12. /* used as a temp storage for the text content of a file */
  13. char tmpbuf[MAX_TEXT_SIZE];
  14.  
  15. /* pointer to the root node of the file system */
  16. struct entryNode * root;
  17.  
  18. /* Helper functions */
  19. void pwdHelper (struct entryNode *); /* don't worry about this one */
  20.  
  21.  
  22. /* Return a pointer to the entry with the given name in the given list,
  23.    or NULL if no such entry exists. */
  24. /* basically it searches for the file/dir name at the current level:
  25.  * it does not go into directories */
  26. struct entryNode * located (char * name, struct entryNode * list) {
  27.     if (list == NULL) {
  28.         return NULL;
  29.     } else if (strcmp (list->name, name) == 0) {
  30.         return list;
  31.     } else {
  32.         return located (name, list->next);
  33.     }
  34. }
  35.  
  36. /* implements the "touch" command (one argument; not in standard UNIX) */
  37. /* wd is a pointer to the current working directory */
  38. void touchFile (struct entryNode * wd, char * fileName)
  39. {
  40.  
  41.     struct entryNode * newFile;
  42.  
  43.     if (located (fileName, wd->entry.entryList))
  44.     {
  45.         printf ("touch: %s: File exists\n", fileName);
  46.         return;
  47.     }
  48.  
  49.     /* initialize temporary text buffer to zeros */
  50.     bzero(tmpbuf,MAX_TEXT_SIZE);
  51.  
  52.     /* HELP NEEDED HERE*/
  53. }
  54.  
  55. /* implements the "mkdir" command (one argument; no options) */
  56. void createDir (struct entryNode * wd, char * dirName) {
  57.     struct entryNode * newDir;
  58.  
  59.     if (located (dirName, wd->entry.entryList))
  60.     {
  61.         printf ("mkdir: %s: File exists\n", dirName);
  62.         return;
  63.     }
  64.     /*struct entryNode * newDir = (char *) malloc(sizeof(newDir));*/
  65.     newDir->entry.entryList;
  66.     newDir->next = NULL;
  67.     /*newDir->previous = dirName;*/
  68.     /*dirName->next = newDir;*/
  69.     struct entryNode * entryList = NULL;
  70.                 /*HELP NEEDED WITH BOLDED TEXT*/
  71. }
  72.  
  73. /* implements the "rm" command (one argument, unlike standard UNIX; no options) */
  74. void removeFile (struct entryNode * wd, char * fileName) {
  75.     struct entryNode * file;
  76.     file = located (fileName, wd->entry.entryList);
  77.  
  78.     if (file == NULL)
  79.     {
  80.         printf ("rm: %s: No such file or directory.\n", fileName);
  81.         return;
  82.     }
  83.  
  84.     else if (file->isDirectory)
  85.     {
  86.         printf ("rm: %s: is a directory.\n", fileName);
  87.         return;
  88.     }
  89.     /*HELP NEEDED HERE*/
  90. }
  91.  
  92. /* implements the "rmdir" command (one argument, unlike standard UNIX; no options) */
  93. void removeDir (struct entryNode * wd, char * dirName) {
  94.     struct entryNode * dir;
  95.     struct entryNode * tmp;
  96.  
  97.     dir = located (dirName, wd->entry.entryList);
  98.     if (dir == NULL)
  99.     {
  100.         printf ("rmdir: %s: No such file or directory.\n", dirName);
  101.         return;
  102.     }
  103.  
  104.     else if (!dir->isDirectory)
  105.     {
  106.         printf ("rmdir: %s: Not a directory.\n", dirName);
  107.         return;
  108.     }
  109.  
  110.     else if (dir->name[0] =='/')
  111.     {
  112.         printf("rmdir: Cannot delete root directory.\n");
  113.         return;
  114.     }
  115.                 [b]else
  116.     {
  117.         previousfile = file->previous;
  118.         nextfile = file->next;
  119.         if (previousfile==NULL)
  120.         {
  121.             printf("%s: No Previous File");
  122.             if (nextfile == NULL)
  123.             {
  124.                 printf("%s: No Next File");
  125.             }
  126.             free(file);
  127.             wd->entry.entrylit = NULL;
  128.         }
  129.         else if
  130.         {
  131.             if (nextfile == NULL)
  132.             {
  133.                 printf("%s: No Next File");
  134.                 if (previousfile==NULL)
  135.                 {
  136.                     printf("%s: No Previous File");
  137.                 }
  138.                 free(file);
  139.                 wd->entry.entrylit = NULL;
  140.             }
  141.         }
  142.         else
  143.         {
  144.             free(file);
  145.             wd->entry.entrylit = NULL;
  146.         }
  147.     }    
  148.  
  149.     /* HELP NEEDED WITH BOLDED TEXT */
  150.  
  151. /* implements the "mv" command (two arguments, unlike standard UNIX; no options) */
  152. void moveEntry (struct entryNode * wd, char * from, char * to) {
  153.     struct entryNode * fromNode;
  154.     struct entryNode * toNode;
  155.     fromNode = located (from, wd->entry.entryList);
  156.     if (fromNode == NULL)
  157.     {
  158.         printf ("mv: %s: No such file or directory.\n", from);
  159.         return;
  160.     }
  161.  
  162.     /*HELP NEEDED HERE */
  163. }
  164.  
THANK YOU IN ADVANCE FOR YOUR HELP ^_^
Feb 9 '08 #1
0 1806

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
8
by: farhanb | last post by:
hello, I am writing a simple linked list implementation. When I use function insert1 I must allocate to head in my main to get it to work otherwise list stays empty but when I use function...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.