/*******************************************************
2.2
Implement an algorithm to find the nth to last element of a singly linked list.
Note:
use 2 pointers to traverse the list, separated by n nodes. When one reaches the end,
the other is pointing at the last nth element.
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);
int main(void)
{
int readInt;
int NumOfElem = 0; //counter of how many elements.
int i;
//ElemType elem;
Position FrontNode, BackNode;
LinkList list = New_LinkList();
if(!list)
{
return ERROR;
}
freopen("array.txt", "r", stdin);
while(1)
{
if(scanf("%d", &readInt) == EOF)
{
break;
}
Append_LinkList(list, readInt);
NumOfElem++;
}
FrontNode = list;
BackNode = list;
for(i = 1; i < LASTN; i++)
{
if(FrontNode->next)
{
FrontNode = FrontNode->next;
}
else
{
printf("The link list is too short!!!\n");
return ERROR;
}
}
while(FrontNode->next)
{
FrontNode = FrontNode->next;
BackNode = BackNode->next;
}
Print_LinkList(list);
printf("The last %dth element is %d\n", LASTN, BackNode->elem);
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\n", 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;
}
2.2
Implement an algorithm to find the nth to last element of a singly linked list.
Note:
use 2 pointers to traverse the list, separated by n nodes. When one reaches the end,
the other is pointing at the last nth element.
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);
int main(void)
{
int readInt;
int NumOfElem = 0; //counter of how many elements.
int i;
//ElemType elem;
Position FrontNode, BackNode;
LinkList list = New_LinkList();
if(!list)
{
return ERROR;
}
freopen("array.txt", "r", stdin);
while(1)
{
if(scanf("%d", &readInt) == EOF)
{
break;
}
Append_LinkList(list, readInt);
NumOfElem++;
}
FrontNode = list;
BackNode = list;
for(i = 1; i < LASTN; i++)
{
if(FrontNode->next)
{
FrontNode = FrontNode->next;
}
else
{
printf("The link list is too short!!!\n");
return ERROR;
}
}
while(FrontNode->next)
{
FrontNode = FrontNode->next;
BackNode = BackNode->next;
}
Print_LinkList(list);
printf("The last %dth element is %d\n", LASTN, BackNode->elem);
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\n", 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;
}