linklist2

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAX_LEN 100

typedef struct ITEM
{
    char cStr[MAX_LEN];
}ITEM_STRU;

typedef struct NODE
{
    ITEM_STRU stItem;
    struct NODE *pNext;
}NODE_STRU;

typedef struct LIST
{
    NODE_STRU *pNext;
}LIST_STRU;

void InitList(LIST_STRU *pList)
{
     pList->pNext = NULL;
     return;
}

NODE_STRU *CreateNode(char *pStr)
{  
    NODE_STRU *pOutNode;
    pOutNode = (NODE_STRU *)malloc(sizeof(NODE_STRU));
    if(NULL == pOutNode)
    {
        return NULL;
    }
    memset(pOutNode->stItem.cStr,'\0',strlen(pStr) + 1);
    strcpy(pOutNode->stItem.cStr, pStr);
    pOutNode->pNext = NULL;    
    return pOutNode;
}


void LeftShiftStringByOneChar(char *pStr)
{    
     int iPos;
     char temp;
     temp = (*pStr);
     for(iPos = 1;iPos<strlen(pStr);iPos++)
     {
         *(pStr + iPos - 1) = *(pStr + iPos);             
     }
     *(pStr + iPos - 1) = temp;
    
     return;    
}

NODE_STRU *GetNodeByIndex(LIST_STRU *pList, int iIndex)
{
    NODE_STRU *pOutNode;
    int iCount;   
    pOutNode = pList->pNext;
    if(NULL == pOutNode || iIndex < 0)
    {
       return NULL;
    }  
   
    for(iCount = 0; iCount < iIndex; iCount++)
    {
        pOutNode = pOutNode->pNext;
    }
   
    return pOutNode;
}

void DestroyList(LIST_STRU *pList)
{
     NODE_STRU *pTemp = NULL;
     NODE_STRU *pPos;
     pPos = pList->pNext;
     while(NULL !=pPos)
     {
         pTemp = pPos;
         pPos = pPos->pNext;
         free(pTemp);
     }
     pTemp = NULL;
     pList->pNext = NULL;
     return;    
}

void InsertOrderedList(LIST_STRU *pList,NODE_STRU *pNode)
{
    
     NODE_STRU *pTemp;  //结点位置临时变量
     NODE_STRU *pFront; //pTemp的前驱    
     pFront = pList->pNext;   
     pTemp = pList->pNext;
     //如果链表为空,或小于第一个结点,插入头结点后
     if(NULL == pTemp || strcmp(pTemp->stItem.cStr,pNode->stItem.cStr)>0)
     {
         pNode->pNext = pTemp;
         pList->pNext = pNode;
         return;        
     }    
    
     //否则,找到相应位置插入      
     while((NULL != pTemp))
     {
         if(strcmp(pTemp->stItem.cStr,pNode->stItem.cStr) <= 0)
         {
           pFront = pTemp;
           pTemp = pTemp->pNext;
           continue;
         }
         else
         {            
             break;            
         }
     }    
     pNode->pNext = pTemp;
     pFront->pNext = pNode;    
    
     return;    
}

void TraverseList(LIST_STRU *pList)
{
     NODE_STRU *pNode;
     pNode = pList->pNext;
     printf("All Item in the List as follows:\r\n");
     while(NULL != pNode)
     {
         printf("%s\r\n",pNode->stItem.cStr);
         pNode = pNode->pNext;
     }
     return;
}

int main()
{
    LIST_STRU stList;
    NODE_STRU stNode;
    NODE_STRU *pstOutNode;
    char cInputStr[MAX_LEN];
    char cOutPutStr[MAX_LEN];
    int iIndex = -1;
    int iNodeNum = 0; 
    int iCount;
    printf("Please input a String:\r\n");
    scanf("%s",cInputStr);   
    iNodeNum = strlen(cInputStr);   
   
    InitList(&stList);
    for(iCount = 0;iCount < iNodeNum; iCount++)
    {
        NODE_STRU *pstNode = NULL;
        pstNode = CreateNode(cInputStr);
        if(NULL == pstNode)
        {
           DestroyList(&stList);
           printf("Alloc Mem fail!\r\n");
           return 0;
        }
        InsertOrderedList(&stList,pstNode);
       
        LeftShiftStringByOneChar(cInputStr);    
    }
   
    printf("List Created!\r\n");
    TraverseList(&stList);
    printf("Input a index to find the string:\r\n");
    scanf("%d",&iIndex);
   
    pstOutNode = GetNodeByIndex(&stList,iIndex);
    if(pstOutNode == NULL)
    {
        printf("Sting not find!\r\n");      
    }
    else
    {
        printf("The String is:%s\r\n",pstOutNode->stItem.cStr);
    }
   
    DestroyList(&stList);
   
    system("pause");
   
    return;  
   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值