#ifndef BASIC_H #define BASIC_H #include #include #include struct Node{ char * words; int count; struct Node *nextPtr; }; struct Node *makeNode(char *words, int size) { struct Node *newNodePtr=(struct Node *)malloc(sizeof(struct Node)); if (newNodePtr==NULL) { printf("Out of memory!\n"); exit(0); } else { newNodePtr->words=(char *)malloc(sizeof(char)*size); strcpy(newNodePtr->words,words); newNodePtr->count=0; /* initialize the counter */ } return newNodePtr; } struct List { int count; struct Node *headPtr; }; void initializeList(struct List *listPtr) { listPtr->headPtr=NULL; listPtr->count=0; } struct Node * setPosition(struct List *listPtr,int position) { int i; struct Node *nodePtr=listPtr->headPtr; if(position<0||position>=listPtr->count) { printf("Invalid position\n"); exit(0); } else { for (i=0;inextPtr; } } return nodePtr; } void insertItem(struct List *listPtr,char *words,int size,int position) { struct Node *newNodePtr=makeNode(words,size); struct Node * nodePtr=NULL; if (position==0) { newNodePtr->nextPtr=listPtr->headPtr; listPtr->headPtr=newNodePtr; } else { nodePtr=setPosition(listPtr,position-1); newNodePtr->nextPtr=nodePtr->nextPtr; nodePtr->nextPtr=newNodePtr; } listPtr->count++; } int listEmpty(struct List *listPtr) { if((listPtr->count)==0) return 1; else return 0; } void deleteNode(struct List *listPtr, int position) { struct Node * oldNodePtr=NULL; struct Node * nodePtr=NULL; if (!listEmpty(listPtr) && positioncount) { if (position==0) { oldNodePtr=listPtr->headPtr; listPtr->headPtr=oldNodePtr->nextPtr; } else { nodePtr=setPosition(listPtr,position-1); oldNodePtr=nodePtr->nextPtr; nodePtr->nextPtr=oldNodePtr->nextPtr; } listPtr->count--; free(oldNodePtr); } else { printf("List is empty or invalid position\n"); exit(0); } } #define ON 1 #define OFF 0 #endif