一日一码07——链表
2013-09-29 11:42
183 查看
链表的实现,以后会更新。
/*带头结点链表常用操作*/ #include <stdio.h> typedef struct Node { int data; struct Node *next; //漏写过struct } Node; Node* createList(int *arr, int n); int insertList(Node* head, int pos, int data); int deleteList(Node* head, int pos); void reverseList(Node **head); void printList(Node* head); Node* createList(int* arr, int n){ Node *head = NULL; Node *p,*q; int i = 0; head = (Node*)malloc(sizeof(Node)); p = head; for (i = 0; i < n; ++i) { q = (Node*)malloc(sizeof(Node)); q->data = arr[i]; p->next = q; p = q; } p->next = NULL; return head; } int insertList(Node* head, int pos, int data){ int i = 0; Node *p,*q; p = head; while( p->next != NULL && ++i < pos){ p = p->next; } if ( i != pos) { printf("Insert fail,the postion is not exist.\n"); return -1; } q = (Node*)malloc(sizeof(Node)); q->data = data; q->next = p->next; p->next = q; return 0; } int deleteList(Node* head, int pos){ int i = 0; Node *p,*q; p = head; while( p->next != NULL && ++i < pos){ p = p->next; } if ( i != pos) { printf("delete fail,the postion is not exist.\n"); return -1; } q = p->next; p->next = q->next; free(q); return 0; } void reverseList(Node **head){ Node *p,*q,*e; e = NULL; p = (*head)->next; if (p == NULL) { return; } while(p->next != NULL){ q = p; p = p->next; q->next = e; e = q; } p->next = e; (*head)->next = p; } void printList(Node* head){ Node* p; p = head; while(p->next != NULL){ p = p->next; printf("%d ", p->data); } printf("\n"); } int main(int argc, char const *argv[]) { int arr[5] = {1,2,3,4,5}; Node* head; head = createList(arr,5); printList(head); reverseList(&head); printList(head); return 0; }
相关文章推荐
- 一日一码02——选择排序
- 一日一码04——快速排序
- 【笔试面试——27】链表07——两个链表的第一个公共节点
- Python一日一练07----冒泡排序
- 【从零单排之微软面试100题系列】07之判断两个链表是否相交
- [C++ 从入门到放弃-07]C++STL之list双向链表容器
- 一日一码01——冒泡排序
- 【算法学习笔记】07.数据结构基础 链表 初步练习
- 一日一码05--希尔排序
- 一日一案例07验证登陆信息的正确性
- 【算法学习笔记】07.数据结构基础 链表 初步练习
- 一日一码08——约瑟夫环
- 【算法Everyday】第一日 二叉查找树转双向链表
- 一日一码06——堆排序
- 一日一码03——插入排序
- c++中的链表应用
- 复杂链表的复制
- 线性表学习笔记之链表
- 判断两链表是否相交
- 图像处理------移动模糊 分类: 视频图像处理 2015-07-24 09:33 26人阅读 评论(0) 收藏