/*******************************************************
2.3
Implement an algorithm to delete a node in the middle of a single linked list, given
only access to that node
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
Complexity: O(n)
Mod Time: 5.6, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<malloc.h>
#define OK 0
#define ERROR -1
#define MYDEBUG 0
#define LASTN 13 //last nth element.
typedef int ElemType;
typedef int Status;
struct Node;
typedef Node ListNode;
typedef ListNode * Position;
typedef Position LinkList;
struct Node
{
ElemType elem; //this field of header should be NULL.
Position next;
};
LinkList New_LinkList(void);
Status Print_LinkList(LinkList ll);
Status Append_LinkList(LinkList ll, ElemType val);
Status Destroy_LinkList(LinkList ll);
ElemType DeleteNextNode(Position p);
int main(void)
{
int readInt, Loca;
int NumOfElem = 0; //counter of how many elements.
int i;
ElemType val;
//ElemType elem;
Position TravNode;
LinkList list = New_LinkList();
if(!list)
{
return ERROR;
}
printf("Please input the No. of element that should be deleted.\n");
scanf("%d", &Loca);
printf("%d", Loca);
freopen("array.txt", "r", stdin);
while(1)
{
if(scanf("%d", &readInt) == EOF)
{
break;
}
Append_LinkList(list, readInt);
NumOfElem++;
}
Print_LinkList(list);
if(Loca > NumOfElem)
{
printf("Impossible to delete that node!\n");
return ERROR;
}
else //if(Loca < NumOfElem)
{
TravNode = list;
for(i = 1; i < Loca; i++)
{
TravNode = TravNode->next;
}
val = DeleteNextNode(TravNode);
printf("The deleted element is %d\n", val);
}
/*
else // delete the link tail.
{
}*/
Print_LinkList(list);
fclose(stdin);
Destroy_LinkList(list);
return OK;
}
LinkList New_LinkList(void)
{
LinkList ll = (LinkList)malloc(sizeof(ListNode));
ll->elem = 0;
ll->next = NULL;
return ll;
}
Status Print_LinkList(LinkList ll)
{
Position p = ll;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to print a linklist that does not exist\n");
return ERROR;
}
printf("The elements in link list: \n");
while(p->next != NULL)
{
p = p->next;
printf("%d\t", p->elem);
}
return OK;
}
Status Append_LinkList(LinkList ll, ElemType val)
{
Position posi;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to insert to a linklist that does not exist\n");
return ERROR;
}
posi = ll;
while(posi->next)
{
posi = posi->next;
}
posi->next = (Position)malloc(sizeof(ListNode));
posi->next->next = NULL;
posi->next->elem = val;
return OK;
}
Status Destroy_LinkList(LinkList ll)
{
Position posi, temp;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to delete a linklist that does not exist\n");
return ERROR;
}
posi = ll;
ll->next = NULL;
while(posi->next)
{
temp = posi->next;
free(posi);
posi = temp;
}
return OK;
}
ElemType DeleteNextNode(Position p)
{
ElemType val;
Position temp = p->next;
val = temp->elem;
p->next = p->next->next;
free(temp);
return val;
}
2.3
Implement an algorithm to delete a node in the middle of a single linked list, given
only access to that node
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
Complexity: O(n)
Mod Time: 5.6, 2012
Copyright: Ben
*******************************************************/
#include<stdio.h>
#include<malloc.h>
#define OK 0
#define ERROR -1
#define MYDEBUG 0
#define LASTN 13 //last nth element.
typedef int ElemType;
typedef int Status;
struct Node;
typedef Node ListNode;
typedef ListNode * Position;
typedef Position LinkList;
struct Node
{
ElemType elem; //this field of header should be NULL.
Position next;
};
LinkList New_LinkList(void);
Status Print_LinkList(LinkList ll);
Status Append_LinkList(LinkList ll, ElemType val);
Status Destroy_LinkList(LinkList ll);
ElemType DeleteNextNode(Position p);
int main(void)
{
int readInt, Loca;
int NumOfElem = 0; //counter of how many elements.
int i;
ElemType val;
//ElemType elem;
Position TravNode;
LinkList list = New_LinkList();
if(!list)
{
return ERROR;
}
printf("Please input the No. of element that should be deleted.\n");
scanf("%d", &Loca);
printf("%d", Loca);
freopen("array.txt", "r", stdin);
while(1)
{
if(scanf("%d", &readInt) == EOF)
{
break;
}
Append_LinkList(list, readInt);
NumOfElem++;
}
Print_LinkList(list);
if(Loca > NumOfElem)
{
printf("Impossible to delete that node!\n");
return ERROR;
}
else //if(Loca < NumOfElem)
{
TravNode = list;
for(i = 1; i < Loca; i++)
{
TravNode = TravNode->next;
}
val = DeleteNextNode(TravNode);
printf("The deleted element is %d\n", val);
}
/*
else // delete the link tail.
{
}*/
Print_LinkList(list);
fclose(stdin);
Destroy_LinkList(list);
return OK;
}
LinkList New_LinkList(void)
{
LinkList ll = (LinkList)malloc(sizeof(ListNode));
ll->elem = 0;
ll->next = NULL;
return ll;
}
Status Print_LinkList(LinkList ll)
{
Position p = ll;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to print a linklist that does not exist\n");
return ERROR;
}
printf("The elements in link list: \n");
while(p->next != NULL)
{
p = p->next;
printf("%d\t", p->elem);
}
return OK;
}
Status Append_LinkList(LinkList ll, ElemType val)
{
Position posi;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to insert to a linklist that does not exist\n");
return ERROR;
}
posi = ll;
while(posi->next)
{
posi = posi->next;
}
posi->next = (Position)malloc(sizeof(ListNode));
posi->next->next = NULL;
posi->next->elem = val;
return OK;
}
Status Destroy_LinkList(LinkList ll)
{
Position posi, temp;
if(!ll) //trying to delete a linklist that does not exist
{
printf("trying to delete a linklist that does not exist\n");
return ERROR;
}
posi = ll;
ll->next = NULL;
while(posi->next)
{
temp = posi->next;
free(posi);
posi = temp;
}
return OK;
}
ElemType DeleteNextNode(Position p)
{
ElemType val;
Position temp = p->next;
val = temp->elem;
p->next = p->next->next;
free(temp);
return val;
}