remove added

This commit is contained in:
Remco
2016-02-17 14:50:30 +01:00
parent e61b36fffe
commit b19dac39e8
3 changed files with 66 additions and 10 deletions
+52
View File
@@ -8,6 +8,7 @@ static struct node *pStart = NULL;
void init()
{
printf("Running init...\n");
pEnd = NULL;
pStart = NULL;
}
@@ -105,6 +106,7 @@ int addPos(int data, int positie)
void show()
{
printf("Showing data...\n");
struct node *p = pStart;
int nr = 0;
@@ -123,6 +125,7 @@ void show()
void clear()
{
printf("Clearing data...\n");
struct node *p = pEnd;
struct node *prev = NULL;
@@ -170,4 +173,53 @@ int exists(int data)
return -1;
}
int remove(int begin)
{
struct node *p = NULL;
if (begin == 1)
{
p = pStart->next;
p->previous = NULL;
free(pStart);
pStart = p;
}
else
{
p = pEnd->previous;
p->next = NULL;
free(pEnd);
pEnd = p;
}
return 1;
}
int removePos(int positie)
{
if (positie == 0)
{
remove(1);
}
else if (positie == length())
{
remove(0);
}
else if (positie < length())
{
struct node *r = pStart; //the node you want to remove
/*looping till you got to the positie, and save the node*/
for (int i = 0; i < (positie - 1); i++)
{
r = r->next;
}
r->previous->next = r->next;
r->next->previous = r->previous;
free(r);
}
return 1;
}
+4 -1
View File
@@ -7,6 +7,7 @@ struct node
struct node *next;
struct node *previous;
};
#endif
void init();
int add(int data, int begin);
@@ -15,4 +16,6 @@ void show();
void clear();
int length();
int exists(int data);
#endif
int remove(int);
int removePos(int);
+10 -9
View File
@@ -5,7 +5,7 @@ int main()
{
int idx;
printf("Running init...\n");
init();
@@ -16,13 +16,11 @@ int main()
add(idx, '0');
}
printf("Showing data...\n");
show();
printf("Clearing data...\n");
clear();
printf("Showing data...\n");
show();
printf("Adding data at begin and end...\n");
@@ -38,15 +36,18 @@ int main()
addPos(22, 5);
addPos(22, 40);
addPos(22, -10);
printf("Showing data...\n");
show();
printf("Length of list: %d\n", length());
printf("Position of 22 in list: %d\n", exists(22));
printf("Position of 40 in list: %d\n", exists(40));
printf("Clearing data...\n");
remove(1);
removePos(3);
show();
clear();
printf("Done\n");