Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This file is to demonstrate the use of the functions
- * This has to be done N ascedning integers to the start and end of the list
- * The structure defined in the header file contains a void pointer rather than a pointer to specific data type
- * This means our implementation should allow you to add any data type with appropriate casting of the data types.
- * Then once added the program should remove the elements one by one from the beginning of the list
- * AND display the data value for each element on the screen whilst removing it (first for FIFO, then for LIFO)
- */
- #include "linkedlist.c"
- #include <stdio.h>
- #include <stdlib.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 = malloc(sizeof(struct element));
- // struct element * h;
- struct element ** pTail = malloc(sizeof(struct element));
- //struct element * t;
- //pHead = &h;
- //pTail = &t;
- int i= 2;
- oTemp = &i;
- addFirst(oTemp,pHead,pTail);
- addFirst(oTemp,pHead,pTail);
- addLast(oTemp,pHead,pTail);
- }
- /*Operating Systems and Concurrency Lab 1*/
- /*If on school PC's go on PUTTY and connect to bam.cs.nott.ac.uk *
- *This file is for the implementation of the function prototypes from linkedlist.h
- *The code needs to implement functions to:
- * - Add elements at the beginning of the list (addFirst)
- * - Add elements at the end of the list (addLast)
- * - Remove the first element from the list (removeFirst)
- *
- *
- *In order to use the structure the header file needs to be included*/
- #include "linkedlist.h"
- #include <stdio.h>
- #include <stdlib.h>
- /*Functions from LinkedList.h*/
- void addLast(void * oTemp, struct element ** pHead, struct element ** pTail){
- /*struct element * new1 = malloc(sizeof(struct element));
- if(pTail)
- {
- new1 = oTemp;
- pTail-> = *new1;
- printf("LL.C val 2\n");
- printf("Value in new1: %d\n",*(int *) new1);
- //pHead->pNext = NULL;
- }*/
- }
- void addFirst(void * oTemp, struct element ** pHead, struct element ** pTail){
- /*
- *New element is created and data is allocated. Then set as head.
- */
- printf("Seg Fault test 1\n");
- struct element * new2 = 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
- printf("Seg Fault test 2\n");
- //need to test if there is data in the head
- //valgrind for testing memory leaks
- if(*pHead == NULL){
- printf("Seg Fault test 3\n");
- new2->pData = oTemp;
- printf("Seg Fault test 3.1\n");
- new2->pNext = NULL;
- printf("Seg Fault test 3.2\n");
- *pHead = new2;
- printf("Seg Fault test 3.3\n");
- }
- else{
- printf("Seg Fault test 4\n");
- head = *pHead;
- new2->pData = oTemp;
- new2->pNext = head;
- *pHead = new2;
- printf("Seg Fault test 4.1\n");
- }
- }
- void * removeFirst(struct element ** pHead, struct element ** pTail){
- }
- /*Summarising notes for when we come back
- *Finish modifying first year append_linked_list code
- *Fully find out what double pointer (** pHead and ** pTail) exactly means
- *Figuring out HOW data is assigned
- *Work on the task1.c file after Linkedlist.c is fully implemented
- *So when we come back we should fully implement addFirst, addLast, * removeFirst
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement