单链表的插入和遍历 包括头插入和尾插入
2015-08-27 15:27
381 查看
// Win32Project1.cpp : 定义控制台应用程序的入口点。 // //单链表的插入和遍历 #include "stdafx.h" #include <AccCtrl.h> typedef struct sNode { int data; struct sNode * pNext; }Node,* pNode; pNode gHead = NULL; void add(int d);//尾部插入节点 void addHead(int d);//头部插入节点 void print();//遍历打印 int _tmain(int argc, _TCHAR* argv[]) { add(88); addHead(77); add(99); print(); getchar(); return 0; } void add(int d) { pNode p = gHead; pNode pNew = (pNode)malloc(sizeof(Node)); pNew->data = d; pNew->pNext = NULL; if (!p) { gHead = pNew; return; } while (p->pNext) { p = p->pNext; } p->pNext = pNew; return; } void print() { while (gHead) { printf("%d\n", gHead->data); gHead = gHead->pNext; } } void addHead(int d) { pNode p = gHead; pNode pNew = (pNode)malloc(sizeof(Node)); pNew->data = d; pNew->pNext = gHead->pNext; p = pNew; gHead->pNext = pNew; }
相关文章推荐
- hdu5296(倍增lca)
- Linux系统——提高编译速度的方法
- 【转】C#模拟http 发送post或get请求
- DTD Content model definitions
- java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
- Ubuntu无法进入mysql,报ERROR 2002 (HY000): Can't connect to local MySQL server through socket …错误
- intel驱动包下载
- HTML5之自定义标签(之前都没用过这么6的技术)
- linux进程——后台运行的方法
- 一个SQLSERVER触发器的示例
- Install ffmpeg on CentOS (RHEL/Fedora)
- UTF-8简史
- 今年暑假不AC
- 5. jQuery 效果 - 隐藏和显示
- 通过工具来监控webService请求和返回时的数据
- iOS导航页
- 说说耦合(coupling)
- 环信即时通讯 —— 登录注册
- linux系统初始化——文件系统初始化步骤
- name选择器所以一级节点偶数行换样式(juqery)