Linked List using dynamic memory allocation (URGENT request for example) 
Author Message
 Linked List using dynamic memory allocation (URGENT request for example)

----------------------------------------------------------------------------
-----
Does anybody have an example of a
Linked List which uses dynamic memory allocation ???

If so, could they PRETTY PLEASE (with sugar on top)
be so kind as to post it immediately ???

If so,
            Thanks kindly (in advance)
            You've truely made my day !!!

:-)
----------------------------------------------------------------------------
-----



Fri, 09 Mar 2001 03:00:00 GMT  
 Linked List using dynamic memory allocation (URGENT request for example)

Quote:

> ----------------------------------------------------------------------------
> -----
> Does anybody have an example of a
> Linked List which uses dynamic memory allocation ???

> If so, could they PRETTY PLEASE (with sugar on top)
> be so kind as to post it immediately ???

> If so,
>             Thanks kindly (in advance)
>             You've truely made my day !!!

> :-)

Do your own homework.

/ Gunnar



Fri, 09 Mar 2001 03:00:00 GMT  
 Linked List using dynamic memory allocation (URGENT request for example)
Quote:


> > ----------------------------------------------------------------------------
> > -----
> > Does anybody have an example of a
> > Linked List which uses dynamic memory allocation ???

> > If so, could they PRETTY PLEASE (with sugar on top)
> > be so kind as to post it immediately ???

> > If so,
> >             Thanks kindly (in advance)
> >             You've truely made my day !!!

> > :-)

> Do your own homework.

> / Gunnar

Or more constructively try a web search on those topics, the
theory behind them and example data structures in C are
all over the place.
Or alternatively try the Computer Science section of the library
at deakin.edu.au and find a book on Data Structures.

Ed
.
.
.
.
.
.

..
.
.
.
.



Fri, 09 Mar 2001 03:00:00 GMT  
 Linked List using dynamic memory allocation (URGENT request for example)
Hi all !!!

Thankyou all for helping me in my time of dispair
(especially to those who gave tips and web sites).
I have now finished.

Yay!

For those who are curious (& for those who are
interested in seeing an example), the (properly
working) program's as follows.

Thanks again,

Kelly.

/*
Purpose:
To implement a LINKED LIST using
  dynamic memory allocation
  ...where an item of the list is used
  to store a person's identification
  number, first name, surname, and age.

Files:
main.c
list.c
list.h
people.dta
*/

/*************** main.c **************/

#include <stdio.h>
#include "list.h"

extern void initialise(struct list *);
extern int empty(struct list *);
extern struct item * search(struct list *, int);
extern struct item * insert(struct list *, struct person);
extern int delete(struct list *, int);
extern void print(struct list *);

main()
{
FILE *peoplefile;
char transaction;
int id;
struct list l;
struct item *d;
struct person p;

if((peoplefile = fopen("people.dta", "r")) == NULL)
{
  printf("Error: 'fopen' failed\n");
  exit(1);

Quote:
}

initialise(&l);

while(fscanf(peoplefile, "%c %d", &transaction, &id) != EOF)
{
  switch(transaction)
  {
   case 'd':
    if(delete(&l, id))
    {
     print(&l);
    }
    else
     printf("Error: Delete failed\n");
    break;
   case 'i':
    d = search(&l, id);

    if(d == NULL)
    {
     fscanf(peoplefile, "%s %s %i",
      p.firstname,
      p.surname,
      &p.age);
     p.id = id;
     insert(&l, p);
    }
    else
     printf("Error: Duplicate ID\n");

    print(&l);

    break;
   default:
    printf("Error: Invalid Transaction '%c'\n",
        transaction);
    exit(1);
  }

  /* gets rid of the rest of the line and \n, if any */
  fscanf(peoplefile, "%*[^\n]");
  fscanf(peoplefile, "%*c");

Quote:
}

fclose(peoplefile);

Quote:
}

/*************** list.c **************/
#include <stdio.h>
#include "list.h"

void initialise(struct list *);
int empty(struct list *);
int delete(struct list *, int);
struct item * search(struct list *, int);
struct item * insert(struct list *, struct person);
void print(struct list *);
void print_list(struct item *);

void initialise(struct list *list)
{
list->head = NULL;
list->tail = NULL;

Quote:
}

int empty(struct list *list)
{
if(list->head == list->tail)
  return(1);
else
  return(0);

Quote:
}

struct item * insert(struct list *list, struct person person)
{    
struct item *temp, *i;

temp = (struct item *)malloc(sizeof(struct item));
temp->data = person;
temp->next = NULL;

if(list->head == NULL)
{
  list->head = temp;
  list->tail = temp;
  return;

Quote:
}

i = list->tail;
i->next = temp;
list->tail = temp;

Quote:
}

int delete(struct list *list, int id)
{
struct item *x, *y, *temp;

if(empty(list))
{
  printf("Error: Cannot delete, list is empty\n");
  return(0);

Quote:
}

x = list->head;
y = x->next;

while(y != NULL)
{
  if(x->data.id == id)
  {
   temp = x;
   x = x->next;
   list->head = x;
   free(temp);

   return(1);
  }

  if(y->data.id == id)
  {
   temp = y;

   if(y->next == NULL)
   {
    x->next = NULL;
    list->tail = x;
    free(temp);

    return(1);
   }

   if(y->next != NULL)
   {
    x->next = y->next;
    free(temp);

    return(1);
   }
  }

  x = y;
  y = y->next;

Quote:
}
}

struct item * search(struct list *list, int id)
{
struct item *y;

y = list->head;

while(y != NULL)
{
  if(id == y->data.id)
   return(y);
  y = y->next;

Quote:
}

return(NULL);  

Quote:
}

void print(struct list *list)
{
print_list(list->head);
printf("\n");

Quote:
}

void print_list(struct item *item)
{
if(item == NULL)
  return;

printf("(%i %c %s %i) ",
  item->data.id,
  item->data.firstname[0],
  item->data.surname,
  item->data.age);

print_list(item->next);

Quote:
}

/*************** list.h **************/
#define FIRSTNAME_SIZE 20
#define SURNAME_SIZE 30

struct person
{
int id;
char fi
rstname[FIRSTNAME_SIZE];
char surname[SURNAME_SIZE];
int age
;

Quote:
};

struct item
{
struct person data;
struct item *next;

Quote:
};

struct list
{
struct item *head;
struct item *tail;

Quote:
};

/*************** people.dta **************/
i 111 Angela Adams 19
i 222 Erin Burns 21
i 333 John Smith 20
i 334 John Smith 22
i 444 Bill Yard 20
i 111 Lizz Ard 22
i 123 Anne Telope 17
d 111
d 334
d 123
i 234 Jack Aroo 18
i 345 Jill Aroo 19


Sat, 10 Mar 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Binary Search Tree using dynamic memory allocation (URGENT request for example)

2. Help Please!(linked lists/dynamic memory allocation etc.)

3. Dynamic Memory Allocation and Linked Lists

4. Multi-dimensional array using dynamic memory allocation

5. Multi-dimensional array using dynamic memory allocation

6. Clearing Memory || Linked List INSIDE of a Linked List

7. Dynamic Array in C, using Linked Lists.

8. dynamic linked list + dynamic struct members

9. Memory Corruption using linked lists???

10. Memory leak using template linked list????

11. Memory leak using template Linked List???

12. Tree memory allocation, pool memory allocation

 

 
Powered by phpBB® Forum Software