Code:
/*
- Much of this code is from a nice tutorial I found on the internet.
- This tutorial can be found here: http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
- Several functions such as RetrieveList, DeleteItem, and InsertItem were written by me since
- the tutorial did not contain these functions which were what I needed in a program
- I am writing.
-
-
- Cheers, Nick - [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
char *filepath;
char *filename;
int delay;
int loop;
int xpos;
int ypos;
struct node* next;
};
//Linked List Functions
int ListLength(struct node* head);
void PushLinkedList(struct node** headRef, int delay, int xpos, int ypos, int loop, char *filename, char *filepath);
void ChangeToNull(struct node** headRef);
struct node* AppendNode(struct node** headRef, int delay, int xpos, int ypos, int loop, char *filename, char *filepath);
struct node* CopyList(struct node* head);
struct node* DeleteItem(struct node* head, int num);
void InsertItem(struct node** q, int loc, int delay, int xpos, int ypos, int loop, char *filename, char *filepath);
struct node* RetrieveList(struct node** head, int num);
//Test Functions
struct node* BuildTestList();
void LinkedListTest();
int main(int argc, char *argv[])
{
LinkedListTest();
system("PAUSE");
return 0;
}
//Test Function
void LinkedListTest()
{
struct node* head = BuildTestList();
struct node* newNode;
struct node* current;
int i=1;
newNode = malloc(sizeof(struct node));
newNode->filename=malloc(MAX_PATH);
newNode->filepath=malloc(MAX_PATH);
newNode->delay = 1;
newNode->loop = 1;
newNode->xpos = 1;
newNode->ypos = 1;
newNode->filename = "filename";
newNode->filepath = "C:\\filepath";
newNode->next = head;
head = newNode;
int len = ListLength(head);
current = head;
while(current != NULL)
{
printf("Item %d: Delay %d, Loop %d, XPOS %d, YPOS %d, Filename %s, Filepath %s\n",
i++, current->delay, current->loop, current->xpos, current->ypos, current->filename, current->filepath);
current = current->next;
}
printf("Number of items in list is: %d\n\n", len);
// Push Test
printf("Push Test...\n");
PushLinkedList(&head, 1, 0, 0, 0, "filename1", "C:\\filepath1");
PushLinkedList(&head, 13, 1, 2, 3, "filename2", "C:\\filepath2");
current = head;
len = ListLength(head);
i=1;
while(current != NULL)
{
printf("Item %d: Delay %d, Loop %d, XPOS %d, YPOS %d, Filename %s, Filepath %s\n",
i++, current->delay, current->loop, current->xpos, current->ypos, current->filename, current->filepath);
current = current->next;
}
printf("Number of items in list is: %d\n\n", len);
// Add to end test
printf("Add To End Test...\n");
AppendNode(&head, 27, 3, 2, 1, "filename3", "C:\\filepath3");
AppendNode(&head, 28, 5, 8, 9, "filename4", "C:\\filepath4");
current = head;
i=1;
while(current != NULL)
{
printf("Item %d: Delay %d, Loop %d, XPOS %d, YPOS %d, Filename %s, Filepath %s\n",
i++, current->delay, current->loop, current->xpos, current->ypos, current->filename, current->filepath);
current = current->next;
}
printf("Number of items in list is: %d\n\n", ListLength(head));
// Delete Item Test
printf("Delete Item Test...\n");
head=DeleteItem(head, 6);
current = head;
i=1;
while(current != NULL)
{
printf("Item %d: Delay %d, Loop %d, XPOS %d, YPOS %d, Filename %s, Filepath %s\n",
i++, current->delay, current->loop, current->xpos, current->ypos, current->filename, current->filepath);
current = current->next;
}
printf("Number of items in list is: %d\n\n", ListLength(head));
// Insert Item Test
printf("Insert Item Test...\n");
InsertItem(&head, 6, 120, 50, 80, 90, "filename5", "C:\\filepath5");
current = head;
i=1;
while(current != NULL)
{
printf("Item %d: Delay %d, Loop %d, XPOS %d, YPOS %d, Filename %s, Filepath %s\n",
i++, current->delay, current->loop, current->xpos, current->ypos, current->filename, current->filepath);
current = current->next;
}
printf("Number of items in list is: %d\n\n", ListLength(head));
}
//Test Function
struct node* BuildTestList()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->filename = malloc(MAX_PATH);
head->filepath = malloc(MAX_PATH);
head->delay = 1;
head->loop = 10;
head->xpos = 12;
head->ypos = 21;
strcpy(head->filename, "testname");
strcpy(head->filepath, "C:\\testpath");
head->next = second;
second->filename = malloc(MAX_PATH);
second->filepath = malloc(MAX_PATH);
second->delay = 54;
second->loop = 140;
second->xpos = 142;
second->ypos = 241;
strcpy(second->filename, "testname1");
strcpy(second->filepath, "C:\\testpath1");
second->next = third;
third->filename = malloc(MAX_PATH);
third->filepath = malloc(MAX_PATH);
third->delay = 541;
third->loop = 1401;
third->xpos = 1421;
third->ypos = 2411;
strcpy(third->filename, "testname2");
strcpy(third->filepath, "C:\\testpath2");
third->next = NULL;
return head;
}
//Linked List Code
void ChangeToNull(struct node** headRef)
{
*headRef = NULL;
}
//Linked List Code
int ListLength(struct node* head)
{
struct node* current = head;
int count = 0;
while(current != NULL)
{
count++;
current = current->next;
}
return count;
}
//Linked List Code
struct node* DeleteItem(struct node* head, int num)
{
struct node* current = head;
struct node* newList = NULL;
struct node* tail = NULL;
int count=1;
while(current != NULL)
{
if(count != num)
{
if(newList == NULL)
{
newList = malloc(sizeof(struct node));
newList->delay = current->delay;
newList->loop = current->loop;
newList->xpos = current->xpos;
newList->ypos = current->ypos;
newList->filename=current->filename;
newList->filepath=current->filepath;
newList->next = NULL;
tail = newList;
}
else
{
tail->next = malloc(sizeof(struct node));
tail = tail->next;
tail->delay = current->delay;
tail->loop = current->loop;
tail->xpos = current->xpos;
tail->ypos = current->ypos;
tail->filename=current->filename;
tail->filepath=current->filepath;
tail->next = NULL;
}
}
count++;
current = current->next;
}
return newList;
}
//Linked List Code
void InsertItem(struct node** q, int loc, int delay, int xpos, int ypos, int loop, char *filename, char *filepath)
{
struct node *temp,*n;
int c=1,flag=0;
temp=*q;
if(*q != NULL)
{
while(temp != NULL)
{
if(c == loc)
{
n = (struct node *)malloc(sizeof(struct node));
n->delay=delay;
n->xpos=xpos;
n->ypos=ypos;
n->loop=loop;
n->filename = malloc(MAX_PATH);
n->filepath = malloc(MAX_PATH);
strcpy(n->filename, filename);
strcpy(n->filepath, filepath);
n->next=temp->next;
temp->next=n;
flag=1;
}
c++;
temp=temp->next;
}
}
}
//Linked List Code
void PushLinkedList(struct node** headRef, int delay, int xpos, int ypos, int loop, char *filename, char *filepath)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->delay=delay;
newNode->xpos=xpos;
newNode->ypos=ypos;
newNode->loop=loop;
newNode->filename = malloc(MAX_PATH);
newNode->filepath = malloc(MAX_PATH);
strcpy(newNode->filename, filename);
strcpy(newNode->filepath, filepath);
newNode->next = *headRef;
*headRef = newNode;
}
//Linked List Code
struct node* AppendNode(struct node** headRef, int delay, int xpos, int ypos, int loop, char *filename, char *filepath)
{
struct node* current = *headRef;
if(current == NULL)
{
PushLinkedList(headRef, delay, xpos, ypos, loop, filename, filepath);
}
else
{
while(current->next !=NULL)
{
current = current->next;
}
PushLinkedList(&(current->next), delay, xpos, ypos, loop, filename, filepath);
}
}
//Linked List Code
struct node* CopyList(struct node* head)
{
struct node* current = head;
struct node* newList = NULL;
struct node* tail = NULL;
while (current != NULL)
{
if (newList == NULL)
{
PushLinkedList(&newList, current->delay, current->xpos, current->ypos, current->loop, current->filename, current->filepath);
tail = newList;
}
else
{
PushLinkedList(&(tail->next), current->delay, current->xpos, current->ypos, current->loop, current->filename, current->filepath);
tail = tail->next;
}
current = current->next;
}
return(newList);
}
//Linked List Code
struct node* RetrieveList(struct node** head, int num)
{
struct node* current = *head;
int counter=0;
if(current != NULL)
{
while(counter != num && current->next != NULL)
{
current = current->next;
}
}
return current;
}