单向链表简单实现
2015-09-13 19:30
363 查看
/********************************************************************************* * Copyright: (C) 2015 songyong<handy_skyoutlook.com> * All rights reserved. * * Filename: link.c * Description: This file * * Version: 1.0.0(2015年09月11日) * Author: sky <handy_sky@outlook.com> * ChangeLog: 1, Release initial version on "2015年09月11日 21时35分16秒" * ********************************************************************************/ #include<stdio.h> #include<stdlib.h> #define LEN sizeof(struct link) typedef struct link{ int data; struct link *next; }node; node *create(node *head) { node *p1, *p2; int n =0; p1 = p2 =(node *)malloc(LEN); printf("请输入一个数据:"); scanf("%d",&p1->data); while(p1->data != 0) { n++; if(1 == n) { head = p1; } else { p2->next=p1; } p2 = p1; p1= (node *)malloc(LEN); printf("请输入数据,输入0终止:\n"); scanf("%d",&p1->data); } p2->next = NULL; return (head); } /******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/ int main (int argc, char **argv) { node *create(node *head); void print(node *head); node *Reverse(node *head); node *sort(node *head); node *getlink(node *head, int i); void deletlink(node *head); node *insertlink(node *head);//尾插 node *head =NULL; node *p =NULL; int i=0; head = create(head); printf("链表逆置前的数据:\n"); print(head); head = Reverse(head); printf("链表逆置后的数据:\n"); print(head); printf("链表从小到大: \n"); sort(head); print(head); printf("请输入现在您想要打印的节点序号:\n"); scanf("%d",&i); p =getlink(head,i); printf("node:%d\n",p->data); head = insertlink(head);//尾插 print(head); deletlink(head); print(head); return 0; } /* ----- End of main() ----- */ void print(node *head) { node *temp; temp = head; if(NULL == head) return; while(temp != NULL) { printf(" %d",temp->data); temp = temp->next; } printf("\n"); } #if 0 node *creatlist(void) { int val,i,n; node *p,*q,*head; head = NULL; printf("please input link mount..\n"); scanf("%d", &n); for(i =0;i < n; i++) { printf("please input link:\n"); scanf("%d",val); q = p = (node *)malloc(sizeof(node)); p->data = val; if(NULL == head) { q = head = p; } else { q->next = p; q = p; } } p->next = NULL; return 0; } #endif node *Reverse(node *head) { node *p, *q, *r; p = head; q = r =NULL; if(NULL == head) return; while(p)//link not empty { q = p->next; p->next = r; r = p; p = q; } return r; } node *sort(node *head) { node *f,*s; int temp; f = head; for(;f != NULL; f = f->next) { for(s = f->next; s != NULL; s = s->next) { if(f->data > s->data) { temp = f->data; f->data = s->data; s->data = temp; } } } return head; } node * getlink(node *head, int n) { node *p = head; int j = 1; if(0 == n) return head; if(0 > n) return NULL; while(p && j<n) { p = p->next; j++; } return p; } void deletlink(node *head) { int i; node *p,*q; printf("请输入你要删除的节点序号:(输入0可跳过)\n"); scanf("%d",&i); while(1) { if(0 == i) return; p = getlink(head,i); if( NULL == p->next ) { q = getlink(head, i-1); q->next = NULL; free(p); } else { q = p->next; p->data = p->next->data;//这里删除采用的是将后一个节点的值传给前一个节点,再释放后一个节点。 p->next = q->next; free(q); } printf("请输入你要删除的节点序号:(输入0可跳过)\n"); scanf("%d",&i); } } node *insertlink(node *head)//尾插 i > 0 { int i=0; node *p, *s; s = (node *)malloc(LEN); printf("请输入新节点数据:\n"); scanf("%d",&i); s->data = i; printf("请输入你要插入位置:\n"); scanf("%d",&i); while(i == 1) { s->next =head; head = s; return head; } #if 1 p = getlink(head,i-1); s->next = p->next; p->next = s; return head; #endif }
相关文章推荐
- Android:30分钟弄明白Touch事件分发机制
- 查看某个文件的git 历史和更改
- 破解android手机付费软件视频教程
- 《舌尖上的中国》精彩故事
- 分享一个Linux下安装ASM+ORACLE的自动化脚本(三)
- quotation & citation
- Python类的专用方法
- CCF-2015-9-13-04
- 华为测试 字符加密
- 画布
- hdu 5437 优先队列+模拟 **
- ubuntu 14.04设备flash媒体播放器
- 浏览器解析过程
- JQuery动画函数总结
- nyoj 38 布线问题(kruskal 最小生成树)
- 兔子--Spring基金会
- 清除缓存的方法(计算)
- Android图片压缩工具类
- HDU 5233 杂题
- 二叉树的创建和遍历