一个简单的带头尾指针单向链表(C实现)
来源: 作者: 出处:综艺读书 2006-05-22用C写了一个带头尾指针的单向链表,仅在尾部进行插入操作,在任意位置进行删除操作。因为只用到这么些功能,又因为懒,所以没有扩展。因为插入是固定在尾部进行,带一个尾指针的好处是显而易见的。当然删除时要付出一些开销。
|
|
|||
list.h
-------------------------------------------
/* list.h
** Copyright 2004 Coon Xu.
** Author: Coon Xu
** Date: 06 Sep 2004
*/
#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include <stdlib.h>
struct listnode
{
struct listnode* next;
int data;
};
struct list
{
struct listnode* head;
struct listnode* tail;
int count;
};
void list_init(struct list*);
void list_insert(struct list*, struct listnode*);
int list_delete(struct list*, struct listnode*);
#endif
------------------------------------------
list.c
------------------------------------------
/* list.c
** Copyright 2004 Coon Xu.
** Author: Coon Xu
** Date: 06 Sep 2004
*/
#include "list.h"
void list_init(struct list* myroot)
{
myroot->count = 0;
myroot->head = NULL;
myroot->tail = NULL;
}
void list_insert(struct list* myroot, struct listnode* mylistnode)
{
myroot->count++;
mylistnode->next = NULL;
if(myroot->head == NULL)
{
myroot->head = mylistnode;
myroot->tail = mylistnode;
}
else
{
myroot->tail->next = mylistnode;
myroot->tail = mylistnode;
}
}
int list_delete(struct list* myroot, struct listnode* mylistnode)
{
struct listnode* p_listnode = myroot->head;
struct listnode* pre_listnode;
//myroot is empty
if(p_listnode == NULL)
{
return 0;
}
if(p_listnode == mylistnode)
{
myroot->count--;
//myroot has only one node
if(myroot->tail == mylistnode)
{
myroot->head = NULL;
myroot->tail = NULL;
}
else
{
myroot->head = p_listnode->next;
}
return 1;
}
while(p_listnode != mylistnode && p_listnode->next != NULL)
{
pre_listnode = p_listnode;
p_listnode = p_listnode->next;
}
if(p_listnode == mylistnode)
{
pre_listnode->next = p_listnode->next;
myroot->count--;
return 1;
}
else
{
return 0;
}
}
-------------------------------------------
main.c
-------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
int main(int argc, char *argv[])
{
struct list l;
list_init(&l);
int ix = 0;
for(ix = 0; ix < 10; ix++)
{
struct listnode* p_node = malloc(sizeof(struct listnode));
p_node->data = ix;
list_insert(&l, p_node);
}
struct listnode* p_listnode = l.head;
while(p_listnode != NULL)
{
printf("%d\n", p_listnode->data);
p_listnode = p_listnode->next;
}
int input;
printf("input the number you want delete:\n");
scanf("%d", &input);
while(input > 0)
{
p_listnode = l.head;
while(p_listnode->next != NULL && p_listnode->data != input)
{
p_listnode = p_listnode->next;
}
if(p_listnode->data == input)
{
printf("Delete success!!\nprint the list!!\n");
list_delete(&l, p_listnode);
free(p_listnode);
p_listnode = l.head;
while(p_listnode != NULL)
{
printf("%d\n", p_listnode->data);
p_listnode = p_listnode->next;
}
}
else
{
printf("not find %d\n", input);
}
scanf("%d", &input);
}
system("PAUSE");
return 0;
}
·习惯必须延续 必要可以延伸 (29次浏览)
·专访SupeSite/X-Space研发经理李国德 (7次浏览)
·蔡文胜讲经:经营个人网站“五部曲” (0次浏览)
·专访SupeSite/X-Space研发经理李国德 06-09
·习惯必须延续 必要可以延伸 06-01
·设计师的生活态度和侦探 05-15
·七种武器——.NET工程师求职面试必杀技 03-30
·最令我难忘的Java学习经历 03-13
·关于提高自己JAVA水平的十大技术 03-12
·社区精神如何有效的宣扬 03-09
·从程序员到CTO所要培养的六种能力 03-02
·Debian LINUX 学习日记 02-13
|
|||
| ·ACDSEE专题教程-下载使用 ·迅雷专题教程-下载使用 ·Windows XP频道 ·Windows Vista频道 ·Windows 2000频道 ·win2003频道 ·Freebsd频道 ·Oracle频道 |
·Linux频道 ·Windows频道 ·邮件服务器专题 ·协议大全 ·数据恢复指南教程 ·FreeBSD使用教程 ·Linux数据库宝典 ·Linux基础知识 |
||
| · 秘密:Vista隐蔽的动态屏保 · 腾讯开发新电子宠物--QQ熊 · 惠普否认2999元PC有价无货 |
· 驱逐Win系统“流氓”文件 · WinXP中获取未使用的IP地址 · 尝试format C:格式化硬盘? |
| · 在DOS下恢复回收站中的文件 · 拯救WinXP崩溃的救命稻草 · Linux系统中超级权限的应用 |
· 搜狗PK谷歌:谁能代言拼... · 昨日重现,一键GHOST轻松.. · 实现Web迅雷在空闲时杀毒 |
| · AVIFile函数制做AVI文件 · VC中链接动态链接库的方法 · 熊猫烧香核心源码(Delphi) |
· DateDiff函数祥解 · JavaScript去除空格的三种 · js效果 图片加载进度实时.. |
| · SQL Server数据库优化方案 · Oracle的初学者入门心得 · JSP连接Mysql数据库 |
· Photoshop为美女做艺术处理 · 用Freehand创建发光字特效 · 设计自己的个性QQ动态表情 |




