设为首页 加入收藏

TOP

c语言链表,头插法和尾插法
2017-01-06 08:14:53 来源: 作者: 【 】 浏览:47
Tags:语言

#include 
  
   
#include 
   
     #include 
    
      typedef struct stData{ int e; }Data; typedef struct stLink{ Data data; struct stLink* next; }Link; struct Link* create(){ Link* head = (Link*)malloc(sizeof(Link)); memset(&head->data,0,sizeof(Link)); head->next = NULL; return head; } void head_insert(Link* head,Data e){ Link *pTemp,*pNew; if (head->data.e != 0){ pTemp = head->next; pNew = (Link*)malloc(sizeof(Link)); memcpy(&pNew->data,&e,sizeof(Data)); pNew->next = pTemp; head->next = pNew; }else{ memcpy(&head->data,&e,sizeof(Data)); } } void nial_insert(Link* head,Data e){ Link *pTemp,*pNew; pTemp = head; if (head->data.e != 0){ pNew = (Link*)malloc(sizeof(Link)); memcpy(&pNew->data,&e,sizeof(Data)); while(pTemp->next != NULL) pTemp = pTemp->next; pTemp->next = pNew; pNew->next = NULL; } else{ memcpy(&head->data,&e,sizeof(Data)); } } void dele(Link* head,Data e){ Link *pBefore = head; Link *pTemp = head; while(pTemp->next != NULL){ if (pTemp->data.e == e.e) { Link *pDel = pTemp; pBefore->next = pTemp->next; pDel->next = NULL; free(pDel); return; }else{ pBefore = pTemp; pTemp = pTemp->next; } } if (pTemp->data.e == e.e) { Link *pDel = pTemp; pBefore->next = pTemp->next; pDel->next = NULL; free(pDel); } } void show(Link* head){ Link *pTemp = head; while(pTemp->next != NULL){ printf("%d \n",pTemp->data.e); pTemp = pTemp->next; } printf("%d \n",pTemp->data.e); } int main() { Link *head = create(); Data e1={1001},e2={1002},e3={1003},e4={1004}; //head_insert(head,e1); //head_insert(head,e2); //head_insert(head,e3); //head_insert(head,e4); nial_insert(head,e1); nial_insert(head,e2); nial_insert(head,e3); nial_insert(head,e4); show(head); printf("dele------------\n"); dele(head,e2); show(head); return 0; } 
    
   
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇没有了 下一篇C语言中的可变参数列表

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目