复习数据结构,单链表实现代码与君分享,有错误请指正。
#include<stdio.h>
#include<stdlib.h>#include<string.h>
#define SUCCESS 0
#define FAULSE -1
/*data type*/
typedef int Elem;
/*Node Type*/
typedef struct Node{
Elem eData;
struct Node *Next;
}Node_s;
/*prototype*/
int init_list(Node_s **pNode);
int Is_List_Empty(Node_s *pNode);
void Print_List(Node_s *pNode);
int Create_List(Node_s **pNode, Elem *e, int len);
int Insert_Elem_To_List(Node_s **pNode, Elem e, int loac);
int Delete_Elem_List(Node_s **pNode, Elem e);
int Delete_Node_List(Node_s **pNode, int i);
int Clear_List(Node_s **pNode);
int Destroy_List(Node_s **pNode);
int List_Length(Node_s *pNode);
Elem List_Get_Node(Node_s *pNode, int i);
int List_Reverse(Node_s **pNode);
Elem List_Get_Node_From_End(Node_s *pNode, int n);
Elem List_Get_Midle_Node(Node_s *pNode);
void TestList();
int main()
{
TestList();
return 0;
}
/* this function is only for testing*/
void TestList()
{
int w=11;
Elem e[7] = {1, 2, 3, 4, 5, 6, 7};
Node_s *pNode;
init_list(&pNode);
// Create_List(&pNode, e, 7);
// Insert_Elem_To_List(&pNode, 20, 1);
// Delete_Node_List(&pNode, 7);
// Delete_Elem_List(&pNode, 20);
// printf("The Length of list :%d\n",List_Length(pNode));
// printf("The Node is :%d\n",List_Get_Node(pNode, 7));
// Clear_List(&pNode);
// List_Get_Node_From_End(pNode, w);
List_Get_Midle_Node(pNode);
Print_List(pNode);
List_Reverse(&pNode);
Print_List(pNode);
Destroy_List(&pNode);
Is_List_Empty(pNode);
}
/* Function : Intial a list */
int init_list(Node_s **pNode)
{
*pNode = (Node_s *)malloc(sizeof(Node));
if (NULL == *pNode){
exit(0);
}
(*pNode)->Next = NULL;
return SUCCESS;
}
/*
* Function : Creat a list
* Parament : pNode a point to the list
* Notice : when len = 1,equal a node to the head of list
To ensure correct len value.
*/
int Create_List(Node_s **pNode, Elem *e ,int len)
{
int i = 0;
Node_s *p1;
/*if List is empty, Intitial the head of list*/
if (NULL == *pNode){
(*pNode) = (Node_s *)malloc(sizeof(Node));
if (NULL == *pNode){
return FAULSE;
}
(*pNode)->Next = NULL;
}
p1 = (Node_s *)malloc(sizeof(Node_s));
while (i < len){
p1 = (Node_s *)malloc(sizeof(Node_s));
if (NULL == p1){
return FAULSE;
}
p1->eData = e[i++];
p1->Next = (*pNode)->Next;
(*pNode)->Next = p1;
}
return SUCCESS;
}
/*
* Function : To determine whether a linked list is empty
* Parament : pNode a point to the list
* Return : 0 success -1 faulse
*/
int Is_List_Empty(Node_s *pNode)
{
if (NULL == pNode){
exit(0);
}
if (NULL == pNode->Next){
printf("The List is Empty!\n");
return SUCCESS;
}
printf("The List is not Empty!\n");
return FAULSE;
}
/* print the list */
void Print_List(Node_s *pNode)
{
if (NULL == pNode){
exit(0);
}
pNode = pNode->Next; /*point to first date*/
while (NULL != pNode){
printf("%d ",pNode->eData);
pNode = pNode->Next;
}
printf("\n");
}
/*
* Function : Insert an element in the list
* Parament : pNode Header pointer
* e Insert elemment
* loac The location of the insert
* Return : 0 success -1 faulse
*/
int Insert_Elem_To_List(Node_s **pNode, Elem e, int loac)
{
int i = 0;
if (NULL == *pNode){
exit(0);
}
Node_s *sNode = (*pNode), *InsertNode;
while ((NULL != sNode) && (i++ < loac - 1)){
sNode = sNode->Next;
}
if ((NULL == sNode) || (loac <= 0)){
return FAULSE;
}
InsertNode = (Node_s *)malloc(sizeof(Node_s *));
InsertNode->eData = e;
InsertNode->Next = sNode->Next;
sNode->Next = InsertNode;
return SUCCESS;
}
/*Function : Delete an element in the list*/
int Delete_Elem_List(Node_s **pNode, Elem e)
{
Node_s *sNode, *DelNode;
if (NULL == *pNode){
exit(0);
}
sNode = (*pNode);
while (NULL != sNode->Next){
if (sNode->Next->eData == e){
DelNode = sNode->Next;
sNode->Next = DelNode->Next;
free(DelNode);
DelNode == NULL;
return SUCCESS;
}
sNode = sNode->Next;
}
return FAULSE;
}
/* Function : Delete an Node in the list*/
int Delete_Node_List(Node_s **pNode, int i)
{
int del = 0;
Node_s *sNode, *DelNode;
if (NULL == *pNode){
exit(0);
}
sNode = (*pNode);
while ((NULL != sNode) && (del++ < i - 1)){
sNode = sNode->Next;
}
if ((NULL == sNode->Next) || (del > i)){
return FAULSE;
}
DelNode = sNode->Next;
sNode->Next=DelNode->Next;
free(DelNode);
DelNode = NULL; //ensure DelNode isNULL;
return SUCCESS;
}
/*Function : Clear the list*/
int Clear_List(Node_s **pNode)
{
if (NULL == (*pNode)){
exit(0);
}
Node_s *sNode = (*pNode)->Next, *ClrNode;
while (NULL != sNode){
ClrNode = sNode->Next;
free(sNode);
sNode = ClrNode;
}
(*pNode)->Next = NULL;
return SUCCESS;
}
/*Function : Destroy the list*/
int Destroy_List(Node_s **pNode)
{
Clear_List(pNode);
free(pNode);
(*pNode) = NULL;
printf("Destroy Successful!\n");
return SUCCESS;
}
/*Function : Count the length of list
not count the Head*/
int List_Length(Node_s *pNode)
{
int i = 0;
if (NULL == pNode){
exit(0);
}
pNode = pNode->Next;
while (NULL !=pNode){
i++;
pNode = pNode->Next;
}
return i;
}
/*Function : To get en Node in the list*/
Elem List_Get_Node(Node_s *pNode, int i)
{
int del = 0;
if (NULL == pNode){
exit(0);
}
while ((NULL != pNode) && (del++ < i )){
pNode = pNode->Next;
}
if ((NULL == pNode) || (del > i + 1)){
return FAULSE;
}
return pNode->eData;
}
/* Function : Reverse the list */
int List_Reverse(Node_s **pNode)
{
if (NULL == *pNode){
exit(0);
}
/*current always point to 1*/
Node_s *current = (*pNode)->Next, *p;
/* Reverse process
* pNone-1(current)-2(p)-3-4-5-6-7-8
* pNone-2-1(current)-3(p)-4-5-6-7-8
* pNone-3-2-1(current)-4(p)-5-6-7-8*/
while(NULL != current && NULL != current->Next){
p = current->Next;
current->Next = p->Next;
p->Next = (*pNode)->Next;
(*pNode)->Next = p;
}
return SUCCESS;
}
/*Function : Get the N Node from the list bottom*/
Elem List_Get_Node_From_End(Node_s *pNode, int n)
{
int i = 0;
if (NULL == pNode ){
exit(0);
}
Node_s *LowNode = pNode->Next, *FastNode = pNode->Next;
while ((NULL != FastNode) && (i++ < n)){
FastNode = FastNode->Next;
}
if (NULL ==FastNode && i < n || n <= 0){
return FAULSE;
}
while(NULL != FastNode){
FastNode = FastNode->Next;
LowNode = LowNode->Next;
}
return LowNode->eData;
}
/*Function : Get the Middle Node of the list */
Elem List_Get_Midle_Node(Node_s *pNode)
{
int i = 0;
if (NULL == pNode ){
exit(0);
}
Node_s *LowNode = pNode, *FastNode = pNode;
while (NULL != FastNode && NULL !=FastNode->Next){
FastNode = FastNode->Next->Next;
LowNode = LowNode->Next;
}
if(NULL == FastNode->Next){
printf("Not Find Middle Node\n");
return FAULSE;
}
return LowNode->eData;
}