From 9b611ccc726e8629155f1ea4780a234baf428c07 Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Wed, 29 Nov 2017 20:23:24 +0200 Subject: [PATCH 1/3] Add files via upload --- Node.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++ Node.h | 15 +++++++ Sample Stack.cpp | 58 +++++++++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 Node.cpp create mode 100644 Node.h create mode 100644 Sample Stack.cpp 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/Sample Stack.cpp b/Sample Stack.cpp new file mode 100644 index 0000000..7f6bfe9 --- /dev/null +++ b/Sample Stack.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 */ \ No newline at end of file From 5041c5ba75d1f8230fb1d50033dedd76d48c4332 Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Wed, 29 Nov 2017 20:29:55 +0200 Subject: [PATCH 2/3] Rename Sample Stack.cpp to Main.cpp --- Sample Stack.cpp => Main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Sample Stack.cpp => Main.cpp (93%) diff --git a/Sample Stack.cpp b/Main.cpp similarity index 93% rename from Sample Stack.cpp rename to Main.cpp index 7f6bfe9..6991b67 100644 --- a/Sample Stack.cpp +++ b/Main.cpp @@ -55,4 +55,4 @@ int main() printf("End of run. \n"); return 0; -} /* end main */ \ No newline at end of file +} /* end main */ From 9e0c327ce8f6183f2e41eb470e032f8b353796a3 Mon Sep 17 00:00:00 2001 From: Moustafa Attia Date: Thu, 7 Dec 2017 11:47:36 +0200 Subject: [PATCH 3/3] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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