Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<stdio.h>
#include<stdlib.h>
#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 */
110 changes: 110 additions & 0 deletions Node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include<stdio.h>
#include<stdlib.h>
#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" );
}
}
15 changes: 15 additions & 0 deletions Node.h
Original file line number Diff line number Diff line change
@@ -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 );
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# DataStructures
## DataStructures
### Stack pointer based