一日一码07——链表
来源:互联网 发布:钢琴电脑软件 编辑:程序博客网 时间:2024/05/10 17:05
链表的实现,以后会更新。
/*带头结点链表常用操作*/#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;}
- 一日一码07——链表
- 一日一码01——冒泡排序
- 一日一码02——选择排序
- 一日一码03——插入排序
- 一日一码04——快速排序
- 一日一码06——堆排序
- 一日一码08——约瑟夫环
- 【算法】API一日一记——递归
- Windows API一日一练——0说明
- Python标准库——一日一库
- Python一日一练07----冒泡排序
- 一日一码05--希尔排序
- 转载:一日一美语
- 一日一结
- 一日一题-September
- 一日一算法
- #一日一图#谁来暖床!
- #一日一图#
- Java中的Array&ArrayList
- 编程乐趣:向上取N层目录
- c++_getline()和istringstream的用法
- IOS笔记1 使用keyChain保护密码等敏感信息
- 2008-11-10 15:24 Linux下C语言的多线程编程学习[写得很好哦]
- 一日一码07——链表
- 淘宝开放API菜鸟教程——根据卖家昵称获取卖家店铺ID
- 家
- Php开源项目大全
- 自制即时通信系统:socket网络编程(3)
- 丑小鸭进化 之 机房收费系统前奏曲
- PHP扩展 - 《config》
- ios:Xcode 5.0 编译低版本app
- Reverse Linked List II