Monash University > CSSE > CSE1303 > Part A > Lectures > Lecture A05 notes

CSE1303 Computer Science
Semester 2 2003
Part A
Lecture A05 notes: Basic Data Structures - Lists

In this lecture

Reading: Abstract Data Types (ADTs) Programming styles List operations: Note: The position of an item in a list, may change after an insertion or deletion operation.

Comparison with Stacks and Queues:

Simple Implementation Array Implementation of a Linked List

Code for this lecture

/* 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;
}


[ Top | Home ]
Last Updated: Thursday 26 June 2003 11:01:46