Monash University > CSSE > CSE1303 > Part A > Lectures > Lecture A05 notes
Comparison with Stacks and Queues:
/* list.h */
#ifndef LISTH
#define LISTH
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define MAXLIST 10
struct ListRec
{
char list[MAXLIST];
int count;
};
typedef struct ListRec List;
void Initialise(List *lptr);
void Insert(List *lptr, char item);
void Delete(List *lptr, int pos);
bool IsFull(List *lptr);
bool IsEmtpy(List *lptr);
int Find(List *lptr, char item);
/* and any more functions you can think of) */
#endif
/* partial list.c */
#include <stdio.h>
#include "list.h"
void Initialise(List *lptr)
{
lptr->count = 0;
}
bool isFull(List * lptr)
{
if(lptr->count == MAXLIST)
return true;
else
return false;
}
bool isEmpty(List *lptr)
{
if(lptr->count == 0)
return true;
else
return false;
}
void Insert(List *lptr, char item){
int pos, current;
if(IsFull(lptr))
{
fprintf(stderr, "List is full.");
exit(1);
}
for(pos = 0; pos < lptr->count; pos++)
{
if(lptr->list[pos] > item)
break;
}
current = lptr->count - 1;
while (current >= pos)
{
/* Copy the element into the next position along */
lptr->list[current+1] = lptr->list[current];
current--;
}
lptr->list[pos] = item;
lptr->count++;
}
/* llist.h */
#ifndef LLISTH
#define LLISTH
#include <stdlib.h>
#include <stdio.h>
#define MAXLIST 10
typedef struct EntryRec
{
char ch;
int next;
} Entry;
typedef struct LinkedListRec
{
Entry list[MAXLIST];
int count;
int start;
} LList;
void Initialise(LList *lptr);
void Insert(LList *lptr, char item);
void Delete(LList *lptr, int pos);
bool IsFull(LList *lptr);
bool IsEmtpy(LList *lptr);
int Find(LList *lptr, char item);
/* and any more functions you can think of) */
#endif
/* partial llist.c */
#include <stdio.h>
#include "llist.h"
void Initialise(LList *lptr)
{
lptr->count = 0;
lptr->start = -1;
}
bool isFull(LList *lptr)
{
if(lptr->count == MAXLIST)
return true;
else
return false;
}
bool isEmpty(LList *lptr)
{
if(lptr->count == 0)
return true;
else
return false;
}
void Insert(LList *lptr, char item)
{
int current, previous;
if(IsFull(lptr))
{
fprintf(stderr, "Linked List is full.");
exit(1);
}
lptr->list[lptr->count].ch = item;
current = lptr->start;
previous = -1;
if (IsEmpty(lptr))
{
lptr->list[0].next = -1;
lptr->start = 0;
}
else
{
while(current!=-1 && lptr->list[current].ch < item)
{
previous = current;
current = lptr->list[current].next;
}
if (previous==-1)
{
lptr->list[lptr->count].next=lptr->start;
lptr->start=lptr->count;
}
else
{
lptr->list[lptr->count].next = lptr->list[previous].next;
lptr->list[previous].next = lptr->count;
}
}
lptr->count++;
}
[ Top |
Home ]
Last Updated: Tuesday 02 December 2003 22:29:28