写了单链表的实现
来源:互联网 发布:淘宝店如何更改类目 编辑:程序博客网 时间:2024/06/05 23:47
源代码:list.h
#ifndef _LIST_H
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct node{
int val;
struct node * next;
};
typedef struct node* Hlist;
typedef struct node* Position;
void insert(Position pos,int num);
void deletenode(int num,Hlist head);
Position getPrevious(int num,Hlist head);
Position find(int num,Hlist head);
void printlist(Hlist head);
#endif
list.c
#include"list.h"
void insert(Position pos,int num)
{
Position temp=malloc(sizeof(struct node));
if (temp==NULL)
printf("allocate memory failed!");
temp->val=num;
temp->next=pos->next;
pos->next=temp;
}
void deletenode(int num,Hlist head)
{
Position pos=getPrevious(num,head);
if(pos==NULL)
printf("fail to delete num of node");
Position temp=pos->next;
pos->next=pos->next->next;
free(temp);
}
Position getPrevious(int num,Hlist head)
{
Position p=head->next;
while(p->next!=NULL){
if(p->next->val==num)
return p;
p=p->next;
}
return NULL;
}
Position find(int num,Hlist head)
{
Position p=head->next;
while(p!=NULL)
{
if(p->val==num)
return p;
p=p->next;
}
return NULL;
}
void printlist(Hlist head)
{
Position p= head->next;
while(p!=NULL)
{
printf("add:%d value:%d\n",p,p->val);
p=p->next;
}
}
int main()
{
Hlist head=malloc(sizeof(struct node));
head->val=0;
head->next=NULL;
insert(head,6);
insert(head,7);
insert(head,8);
insert(head,9);
deletenode(7,head);
printlist(head);
printf("-------------------\n");
printf("the %d value of add: %d\n",8,find(8,head));
free(head);
return 0;
}
运行结果:
add:166887496 value:9
add:166887480 value:8
add:166887448 value:6
-------------------
the 8 value of add: 166887480
这主要实现链表插入,删除,查找,显示。希望对读者有学习上的帮助。
- 写了单链表的实现
- 自己写了一段assert的实现
- JavaSE写的记事本程序,实现了简单的功能
- huffman编码的实现(有空了再写!!!!!!)
- 空闲了 写几个简单的排序实现
- 用servlet写的JPetStore,实现了全部基本功能
- 自己写的求最大值实现,用到了模板函数。
- 自己写了一个上传类,实现了常用的功能
- 自己用c#写的俄罗斯方块,基本上实现了,就是算法太笨了,呵呵
- 写了个杀美萍的程序
- 写的文章没了
- 一些写了的题
- 自写控件:滑动呈现控件(实现了两个以上控件间的切换)我写的
- 自己写的一个CoreData的Demo,实现了增删改查。
- 写个接口的实现类,在方法的前面加了@Override居然报错
- 自写控件:滑动呈现控件(实现了两个以上控件间的切换)师傅的
- 想研究下linux的recv是怎么实现的,写了一半
- 写个接口的实现类,在方法的前面加了@Override居然报错
- 基于SVN分支开发模式流程浅析
- 堆变量和栈变量
- [LeetCode] Word Break II
- java西天取经之路(二)Servlet部署和配置
- 安卓处理Html----图片异步加载
- 写了单链表的实现
- 【MongoDB】windows下搭建Mongo主(Master)/从(slave)数据库同步
- BeagleBone Black Cross-compiler environment
- [LeetCode] Distinct Subsequences
- mdev.c分析(一)
- 单例模式内容
- 单例模式的理解认识 练习笔记
- HDOJ 题目1217 Arbitrage(最短路径,Floyd)
- 希尔伯特(Hilbert)空间和巴拿赫(Banach)空间