Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "linkedlist.h"
- int main(int argc, char **argv)
- {
- void * oTemp;
- //With a double pointer the word itself (ie 'pHead') can be thought of as a pointer itself
- //to get the data you just need pHead
- struct element ** pHead;
- struct element ** pTail;
- struct element * h;
- struct element * t;
- int cycle;
- int removeData;
- int loadData [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
- int loadData2 [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
- t = NULL;
- h = NULL;
- pHead = &h;
- pTail = &t;
- for( cycle = 0; cycle<20; cycle++){
- oTemp = &loadData[cycle];
- addFirst(oTemp, pHead, pTail);
- h = *pHead;
- }
- for( removeData = 0; removeData <20; removeData++){
- removeFirst(pHead, pTail);
- }
- t = NULL;
- h = NULL;
- pHead = &h;
- pTail = &t;
- for( cycle = 0; cycle<20; cycle++){
- oTemp = &loadData2[cycle];
- addLast(oTemp, pHead, pTail);
- t = *pTail;
- printf("Value in pTail: %d\n", *(int *)t->pData);
- }
- for( removeData = 0; removeData <20; removeData++){
- removeFirst(pHead, pTail);
- }
- }
- #include "linkedlist.h"
- #include <stdio.h>
- #include <stdlib.h>
- /*Functions from LinkedList.h*/
- void addLast(void * oTemp, struct element ** pHead, struct element ** pTail){
- /*
- *New element is created and data is allocated. Then set as head.
- */
- struct element * new2Last= (struct element*)malloc(sizeof(struct element));
- struct element *headLast;
- //By passing in **pHead we are passing in a POINTER to a POINTER
- //So now to manipulate this pointer we need to have access to it
- //need to test if there is data in the head
- //valgrind for testing memory leaks
- if(*pTail == NULL){
- new2Last->pData = oTemp;
- new2Last->pNext = NULL;
- *pTail = new2Last;
- *pHead = new2Last;
- }
- else{
- headLast = *pTail;
- new2Last->pData = oTemp;
- headLast->pNext = new2Last;
- new2Last->pNext = NULL;
- *pTail = new2Last;
- }
- }
- void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail){
- /*
- *New element is created and data is allocated. Then set as head.
- */
- struct element * new2 = (struct element*)malloc(sizeof(struct element));
- struct element *head;
- //By passing in **pHead we are passing in a POINTER to a POINTER
- //So now to manipulate this pointer we need to have access to it
- //need to test if there is data in the head
- //valgrind for testing memory leaks
- if(*pHead == NULL){
- new2->pData = oTemp;
- new2->pNext = NULL;
- *pHead = new2;
- *pTail = new2;
- }
- else{
- head = *pHead;
- new2->pData = oTemp;
- new2->pNext = head;
- *pHead = new2;
- }
- }
- void * removeFirst(struct element ** pHead, struct element ** pTail){
- /*We need to return a void pointer that has the data of the element we are removing
- *To do this we need to store this data in a void pointer, so we can return it
- *For correct memeory allocation we also need to free the entire element
- *So first we must save the data in the element the head is pointing to and then free the element
- */
- void *remove;
- struct element *head;
- head = *pHead;
- remove = head->pData;
- *pHead = head -> pNext;
- free(head);
- return remove;
- //Save the element after the head
- //free the head
- //make the pointer to the head eqaul to the saved node
- }
- /**************************************************************************************************************************
- DO NOT CHANGE THE CONTENTS OF THIS FILE FOR YOUR COURSEWORK. ONLY WORK WITH THE OFFICIAL VERSION
- **************************************************************************************************************************/
- #include <stdio.h>
- struct element
- {
- void * pData;
- struct element * pNext;
- };
- void addLast(void * oTemp, struct element ** pHead, struct element ** pTail);
- void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail);
- void * removeFirst(struct element ** pHead, struct element ** pTail);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement