-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
110 lines (94 loc) · 2.55 KB
/
Node.cpp
File metadata and controls
110 lines (94 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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" );
}
}