Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- header:
- #ifndef GADT_H
- #define GADT_H
- #include <stdio.h>
- //typedef declaration
- typedef void* ELM;
- typedef void* SLN;
- typedef void* HEAD;
- typedef enum { success, outOfMem, badArgs, failure} RESULT;
- HEAD SLCreate(ELM head_val, ELM(*create_elm)(), void(*cpy_elm)(ELM, ELM),
- int(*cmp_elm)( ELM, ELM), void(*free_elm)(ELM),
- void(*print_elm)( ELM), ELM(*add_elm_to_elm)(ELM, ELM));
- void SLDestroy(HEAD head);
- RESULT SLAddListElement(HEAD* head, ELM node);
- RESULT SLRemoveElement(HEAD* head, ELM node);
- SLN SLFindElement(HEAD head, ELM node);
- void SLAddToElement(HEAD* head, ELM toEl, ELM addEl);
- void SLPrintElements(HEAD head);
- void SLSize(HEAD head);
- int SLSizeReturn(HEAD head);
- #endif
- c file:
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include "gadt.h"
- typedef struct Node{
- ELM data;
- struct Node* next;
- //Functions associated with the struct.
- ELM(*create_elm)();
- void(*cpy_elm)(ELM, ELM);
- int(*cmp_elm)( ELM, ELM);
- void(*free_elm)(ELM);
- void(*print_elm)( ELM);
- ELM(*add_elm_to_elm)(ELM, ELM);
- }Node;
- /************************************************************************
- * function name: SLCreate
- * The input: a pointer to a value(void*), pointers to functions
- that are associated with the struct.
- * The output: void*
- * the operation: creates the a pointer to a new linked list
- and initializes its functions.
- *************************************************************************/
- extern HEAD SLCreate(ELM head_val,
- ELM(*create_elm)(),
- void(*cpy_elm)(ELM, ELM),
- int(*cmp_elm)( ELM, ELM),
- void(*free_elm)(ELM),void(*print_elm)( ELM),
- ELM(*add_elm_to_elm)(ELM, ELM)){
- HEAD head;
- Node* pToFirstNode=(Node*)malloc(sizeof(Node));
- if (pToFirstNode!=NULL){ //if the allocation hasn't failed
- pToFirstNode->data=create_elm();
- cpy_elm(pToFirstNode->data, head_val);
- pToFirstNode->next=NULL;
- //initializing functions assosiated with Head.
- pToFirstNode->cpy_elm=cpy_elm;
- pToFirstNode->create_elm=create_elm;
- pToFirstNode->cmp_elm=cmp_elm;
- pToFirstNode->free_elm=free_elm;
- pToFirstNode->print_elm=print_elm;
- pToFirstNode->add_elm_to_elm=add_elm_to_elm;
- head=pToFirstNode; //so both of them point ot the same place
- return head;
- }
- return NULL;
- }
- /************************************************************************
- * function name: SLAddListElement
- * The input: the head of the list and the node to be created
- * The output: integer-RESULT (outOfMem, Succes)
- * the operation:goes to the end of the linked list, allocates memory
- at the end and copies node to it.
- *************************************************************************/
- extern RESULT SLAddListElement(HEAD* head, ELM node){
- Node* currNode;
- Node* iteratorNode=(Node*)(*head);
- Node* tempNode;
- currNode=(Node*)malloc(sizeof(Node));
- if(currNode==NULL){
- return outOfMem;
- }
- //inserting functions into currNode
- currNode->add_elm_to_elm=iteratorNode->add_elm_to_elm;
- currNode->cpy_elm=iteratorNode->cpy_elm;
- currNode->create_elm=iteratorNode->create_elm;
- currNode->free_elm=iteratorNode->free_elm;
- currNode->print_elm=iteratorNode->print_elm;
- currNode->cmp_elm=iteratorNode->cmp_elm;
- //inserting data into currNode
- currNode->data=currNode->create_elm();
- currNode->cpy_elm(currNode->data,node);//calls the function from head.
- currNode->next=NULL;
- //get the correct space to enter the node
- //exception if the list has only one item
- if(iteratorNode->next==NULL){
- if(iteratorNode->cmp_elm(iteratorNode->data,currNode->data)<0){
- currNode->next=iteratorNode;
- (*head)=currNode;
- return success;
- }
- else{
- iteratorNode->next=currNode;
- return success;
- }
- }
- //if something needs to be inserted in the first spot
- if(iteratorNode->cmp_elm(iteratorNode->data,currNode->data)<0){
- currNode->next=iteratorNode;
- (*head)=currNode;
- return success;
- }
- //else
- while(iteratorNode->next!=NULL &&
- iteratorNode->cmp_elm(iteratorNode->next->data,currNode->data)>0)
- {
- iteratorNode=iteratorNode->next;
- }
- tempNode=iteratorNode->next;
- iteratorNode->next=currNode;
- currNode->next=tempNode;
- return success;
- }
- /************************************************************************
- * function name: SLDestroy
- * The input: void* head
- * The output: none
- * the operation:removes every element from the list(destroys it)
- *************************************************************************/
- extern void SLDestroy(HEAD head){
- Node* currNode=(Node*)head;
- Node* subsequentNode=(Node*)head; //next node after previous
- //need two pointers because after i free the first one
- //i dont have access to the next node.
- while(currNode!=NULL){
- subsequentNode=subsequentNode->next;
- currNode->free_elm(currNode->data);
- free(currNode);
- currNode=subsequentNode;
- }
- }
- /************************************************************************
- * function name: SLPrintElements
- * The input: void*-the head of the list
- * The output: none
- * the operation:print the list's elements
- *************************************************************************/
- extern void SLPrintElements(HEAD head){
- Node* nodePointer=(Node*)head;
- int i=0, counter=1; //iterators
- //counter represents the amount of nodes printed allready.
- nodePointer->print_elm(nodePointer->data);
- while (nodePointer->next!=NULL){
- nodePointer=nodePointer->next;
- printf("\n");
- while(i<counter){
- printf("\t");
- i++;
- }
- counter++;
- i=0;
- nodePointer->print_elm(nodePointer->data);
- }
- printf("\n");
- }
- /************************************************************************
- * function name: SLSize
- * The input: void*-the head of the list
- * The output: integer-RESULT (outOfMem, Succes)
- * the operation:prints the amount of nodes in the list
- *************************************************************************/
- extern void SLSize(HEAD head){
- printf("%d\n",SLSizeReturn(head));
- }
- /************************************************************************
- * function name: SLSizeReturn
- * The input: the head of the list
- * The output: integer
- * the operation:returns the amount of nodes in the list
- *************************************************************************/
- extern int SLSizeReturn(HEAD head){
- int counter=0;
- Node* nodeCounter=(Node*)head;
- while(nodeCounter!=NULL){
- nodeCounter=nodeCounter->next;
- counter++;
- }
- return counter;
- }
- /************************************************************************
- * function name: SLFindElement
- * The input: two void* (head and element)
- * The output: void*
- * the operation:looks for the element in the list and returns its address.
- *************************************************************************/
- extern SLN SLFindElement(HEAD head, ELM node){
- Node* currNode=(Node*)head;
- void* result=NULL;
- while(currNode!=NULL){
- if(currNode->cmp_elm(currNode->data,node)==0){
- result=currNode;
- }
- currNode=currNode->next;
- }
- return result;
- }
- /************************************************************************
- * function name: SLRemoveElement
- * The input: a pointer to the head of the list and a node
- * The output: integer-resault.
- * the operation:removes the node from the list
- returns success.
- *************************************************************************/
- extern RESULT SLRemoveElement(HEAD* head, ELM node){
- Node* prevNode=(Node*)(*head);
- Node* toBeRemovedNode=(Node*)(SLFindElement((*head),node));
- Node* temp;
- temp=NULL;
- if(toBeRemovedNode==NULL){
- return failure;
- }
- while(prevNode->next!=toBeRemovedNode){
- prevNode=prevNode->next;
- }//prevNode is the node before toBeRemoved
- toBeRemovedNode->free_elm(toBeRemovedNode->data);
- temp=toBeRemovedNode->next;
- free(prevNode->next);
- prevNode->next=temp;
- return success;
- }
- /************************************************************************
- * function name: SLAddToElement
- * The input: pointer to the head of the list, two elements
- * The output: none
- * the operation:searches for the node with the first element
- removes it, adds the second element to it and returns it in the list
- *************************************************************************/
- extern void SLAddToElement(HEAD* head, ELM toEl, ELM addEl){
- Node* funcNode = (Node*)(*head);
- Node* tbRemovedNode = (Node*)SLFindElement(*head,toEl);
- //if we add stuff to the first element.
- if(funcNode==tbRemovedNode){
- (*head)=((Node*)(*head))->next;
- tbRemovedNode->data = tbRemovedNode->add_elm_to_elm
- (tbRemovedNode->data,addEl);
- SLAddListElement(head, tbRemovedNode->data);
- return;
- }
- while(funcNode->next != tbRemovedNode)
- funcNode=funcNode->next;
- funcNode->next = tbRemovedNode->next;
- tbRemovedNode->next = NULL;
- tbRemovedNode->data = tbRemovedNode->add_elm_to_elm
- (tbRemovedNode->data,addEl);
- SLAddListElement(head, tbRemovedNode->data);
- /*tbRemovedNode->free_elm(tbRemovedNode->data);
- free(tbRemovedNode);*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement