diff --git a/Main.cpp b/Main.cpp new file mode 100644 index 0000000..6991b67 --- /dev/null +++ b/Main.cpp @@ -0,0 +1,58 @@ +#include +#include +#include"Node.h" + +/* this program from Dietel Book "C How To Program" at page 482-487 */ + +int main() +{ + ListNodePtr startPtr = NULL; + int choice; + char item; + + instructions(); + printf("? "); + scanf("%d",&choice); + + /* loop while user does not choose 3 */ + + while(choice != 3){ + switch(choice){ + case 1: + printf("Enter a character: "); + scanf("\n%c",&item); + insert(&startPtr,item); /* insert item to the list*/ + printList(startPtr); + break; + + case 2: /* delete an item */ + /* if a list is not empt */ + if(!isEmpty(startPtr) ){ + printf("Enter character to be deleted: "); + scanf("\n%c",&item); + /* if a character is found, remove it */ + if( deletee(&startPtr,item) ) { /* remove item */ + printf("%c deleted. \n",item); + printList( startPtr); + + } + else { + printf("%c is not found. \n\n",item); + } + } /* end of if for isEmpty */ + break; + + default: + printf(" Invalid choice. \n\n"); + instructions(); + break; + } /* end of switch */ + + printf("? "); + scanf("%d", &choice); + } /* end while */ + + printf("End of run. \n"); + + return 0; +} /* end main */ diff --git a/Node.cpp b/Node.cpp new file mode 100644 index 0000000..fdb3051 --- /dev/null +++ b/Node.cpp @@ -0,0 +1,110 @@ +#include +#include +#include"Node.h" + +/* Implemention Level */ + +void instructions(void) +{ + printf("Enter your choice:\n" + "1- to insert an element into the list.\n" + "2- to delete an element from the list.\n" + "3- to end.\n"); +} + +/* insert a new value into the list in sorted order */ +void insert( ListNodePtr *sPtr, char value ) +{ + ListNodePtr newPtr; /* pointer to new node */ + ListNodePtr previousPtr; /* pointer to previous node in list */ + ListNodePtr currentPtr; /* pointer to current node in list */ + + newPtr = (ListNode*)malloc(sizeof(ListNode)); /* create node */ + + if( newPtr != NULL ){ + newPtr->data = value; /* place value in the node */ + newPtr->nextPtr = NULL; + + previousPtr = NULL; + currentPtr = *sPtr; + + /* loop to find the correct location in the list */ + while( currentPtr != NULL && value > currentPtr->data) { + previousPtr = currentPtr; + currentPtr = currentPtr->nextPtr; + } + /* insert new node at beginning of the list */ + if( previousPtr == NULL ){ + newPtr->nextPtr = *sPtr; + *sPtr = newPtr; + } + else { /* insert new node between previousPtr and currentPtr */ + previousPtr->nextPtr = newPtr; + newPtr->nextPtr = currentPtr; + } + } + else { + printf("%c is not inserted. No memory avalibale.\n",value); + } + } + +/* Delete a list element */ +char deletee( ListNodePtr *sPtr,char value ) +{ + ListNodePtr previousPtr; + ListNodePtr currentPtr; + ListNodePtr tempPtr; /* temporary node pointer */ + + /* delete first node */ + + if( value == (*sPtr)->data){ + tempPtr = *sPtr; + *sPtr = (*sPtr)->nextPtr; + tempPtr = NULL; + return value; + } + else { + previousPtr = *sPtr; + currentPtr = ( *sPtr )->nextPtr; + + /* loop to find the correct location in the list */ + while( currentPtr != NULL && currentPtr->data != value ) { + previousPtr = currentPtr; + currentPtr = currentPtr->nextPtr; + } + /* delete node at currentPtr */ + if( currentPtr != NULL ){ + tempPtr = currentPtr; + previousPtr->nextPtr = currentPtr->nextPtr; + tempPtr = NULL; + return value; + } + } + + return '\0'; +} + +/* return 1 if the list is Empty, 0 otherwise */ +int isEmpty( ListNodePtr sPtr ) +{ + return sPtr == NULL; +} + +void printList( ListNodePtr currentPtr ) +{ + /* if list is Empty */ + if( currentPtr == NULL ) { + printf("List is Empty.\n\n"); + } + else { + printf("The list is: \n"); + + /* whole not the end of the list */ + while( currentPtr != NULL ) { + printf("%c --> ",currentPtr->data); + currentPtr = currentPtr->nextPtr; + } + + printf("NULL \n\n" ); + } +} \ No newline at end of file diff --git a/Node.h b/Node.h new file mode 100644 index 0000000..81e1177 --- /dev/null +++ b/Node.h @@ -0,0 +1,15 @@ + +typedef struct listNode { + char data; /* each listNode contains a character */ + struct listNode *nextPtr; /* pointer to next node*/ +}ListNode; + + +typedef ListNode *ListNodePtr; + + +void insert( ListNodePtr *,char ); +char deletee( ListNodePtr *,char ); +int isEmpty( ListNodePtr ); +void printList( ListNodePtr ); +void instructions( void ); \ No newline at end of file diff --git a/README.md b/README.md index 18d3b29..a873c28 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -# DataStructures \ No newline at end of file +## DataStructures +### Stack pointer based