码迷,mamicode.com
首页 > 其他好文 > 详细

单链表 头插 尾插 遍历

时间:2019-10-05 20:18:33      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:rtt   printf   turn   The   include   ++   null   ror   int   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
int data;
struct node *pNext;
};

void insertTail(struct node *pH,struct node *new)
{
struct node *p=pH;
int counter=0;

while(NULL != p->pNext)
{
p=p->pNext;
counter++;
}

p->pNext=new;
pH->data=counter+1;
}

void insertHeader(struct node *pH,struct node *new)
{
new->pNext=pH->pNext;
pH->pNext=new;
pH->data=+1;
}

void traversal(struct node *pH)
{
struct node *p=pH;

while(NULL != p->pNext)
{
p=p->pNext;
printf("data : %d \n",p->data);

}
}

struct node *createNode(int data)
{
struct node *p = (struct node *)malloc(sizeof(struct node));
if(NULL == p)
{
printf("malloc error ! \n");
return NULL;
}

memset(p,‘\0‘,sizeof(struct node));

p->data=data;
p->pNext=NULL;

return p;
}

int main()
{
struct node *pHeader;
struct node *p,*p1,*p2;

pHeader=createNode(0);

insertHeader(pHeader,createNode(8));
insertTail(pHeader,createNode(1));
insertTail(pHeader,createNode(2));
insertTail(pHeader,createNode(3));
insertTail(pHeader,createNode(4));

printf("counter %d \n",pHeader->data);

traversal(pHeader);

printf("data %d \n",pHeader->pNext->data);
printf("hello world ! \n");
return 0;
}

单链表 头插 尾插 遍历

标签:rtt   printf   turn   The   include   ++   null   ror   int   

原文地址:https://www.cnblogs.com/zhangjianrong/p/11625334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!