#include<stdio.h>
#include<malloc.h>
struct LinkedList
{
char data;
struct LinkedList *next;
};
struct LinkedList *LinkedList;
//Gets the count the elements in the list.
int GetLinkedListLength()
{
int length=0;
struct LinkedList *p = LinkedList;
while(p!=NULL)
{
p=p->next;/*Moves the pointer to the next element.*/
length++;
}
return length;
}
//Gets the element by the given index.
struct LinkedList *GetLinkedListNodeByIndex(int index)
{
int currentIndex;
int length = GetLinkedListLength();
struct LinkedList *node = NULL;
if((index>=0) && (index<length))
{
node = LinkedList;
for(currentIndex=1;currentIndex<=index;currentIndex++)
{
node = node->next;
}
}
return node;
}
//Shows all elments in the list.
void ShowData()
{
struct LinkedList *p = LinkedList;
while(p!=NULL)
{
printf("%c/t",p->data);
p = p->next;
}
printf("/n");
}
//Add an element into the list.
void AddElement(char data, int index)
{
int length = GetLinkedListLength();
struct LinkedList *temNode;
struct LinkedList *p;
if((index<0) || (index>length))
{
printf("Index is ilegal./n");
return;
}
//Initializes the tem node.
temNode = (struct LinkedList *)malloc(sizeof(struct LinkedList));
temNode->data = data;
temNode->next = NULL;
if(length == 0)/*The linkedList is empty now.*/
{
LinkedList = temNode;
}
else
{
p = GetLinkedListNodeByIndex(index-1);
if(p == NULL)
{
temNode->next = LinkedList;
LinkedList = temNode;
}
else
{
temNode->next = p->next;
p->next = temNode;
}
}
}
//Deletes a element by the given index.
void DeleteElement(int index)
{
int length = GetLinkedListLength();
struct LinkedList *father;
struct LinkedList *nodeToDelete;
if(length == 0)
{
printf("The list is empty now, can not delete./n");
return;
}
if((index<0) || (index>length - 1))
{
printf("Index is ilegal./n");
return;
}
father = GetLinkedListNodeByIndex(index - 1);
if(father == NULL)
{
nodeToDelete = LinkedList;
LinkedList = LinkedList->next;
}
else
{
nodeToDelete = father->next;
father->next = nodeToDelete->next;
}
free(nodeToDelete);
}
//Mokes some elements.
void InitializeLinkedList()
{
AddElement('A',0);
AddElement('B',1);
AddElement('C',1);
AddElement('D',2);
AddElement('E',0);
AddElement('F',0);
}
//Tests the methods.
void TestLinkedList()
{
InitializeLinkedList();
ShowData();
DeleteElement(0);
ShowData();
}
#include<malloc.h>
struct LinkedList
{
char data;
struct LinkedList *next;
};
struct LinkedList *LinkedList;
//Gets the count the elements in the list.
int GetLinkedListLength()
{
int length=0;
struct LinkedList *p = LinkedList;
while(p!=NULL)
{
p=p->next;/*Moves the pointer to the next element.*/
length++;
}
return length;
}
//Gets the element by the given index.
struct LinkedList *GetLinkedListNodeByIndex(int index)
{
int currentIndex;
int length = GetLinkedListLength();
struct LinkedList *node = NULL;
if((index>=0) && (index<length))
{
node = LinkedList;
for(currentIndex=1;currentIndex<=index;currentIndex++)
{
node = node->next;
}
}
return node;
}
//Shows all elments in the list.
void ShowData()
{
struct LinkedList *p = LinkedList;
while(p!=NULL)
{
printf("%c/t",p->data);
p = p->next;
}
printf("/n");
}
//Add an element into the list.
void AddElement(char data, int index)
{
int length = GetLinkedListLength();
struct LinkedList *temNode;
struct LinkedList *p;
if((index<0) || (index>length))
{
printf("Index is ilegal./n");
return;
}
//Initializes the tem node.
temNode = (struct LinkedList *)malloc(sizeof(struct LinkedList));
temNode->data = data;
temNode->next = NULL;
if(length == 0)/*The linkedList is empty now.*/
{
LinkedList = temNode;
}
else
{
p = GetLinkedListNodeByIndex(index-1);
if(p == NULL)
{
temNode->next = LinkedList;
LinkedList = temNode;
}
else
{
temNode->next = p->next;
p->next = temNode;
}
}
}
//Deletes a element by the given index.
void DeleteElement(int index)
{
int length = GetLinkedListLength();
struct LinkedList *father;
struct LinkedList *nodeToDelete;
if(length == 0)
{
printf("The list is empty now, can not delete./n");
return;
}
if((index<0) || (index>length - 1))
{
printf("Index is ilegal./n");
return;
}
father = GetLinkedListNodeByIndex(index - 1);
if(father == NULL)
{
nodeToDelete = LinkedList;
LinkedList = LinkedList->next;
}
else
{
nodeToDelete = father->next;
father->next = nodeToDelete->next;
}
free(nodeToDelete);
}
//Mokes some elements.
void InitializeLinkedList()
{
AddElement('A',0);
AddElement('B',1);
AddElement('C',1);
AddElement('D',2);
AddElement('E',0);
AddElement('F',0);
}
//Tests the methods.
void TestLinkedList()
{
InitializeLinkedList();
ShowData();
DeleteElement(0);
ShowData();
}