单链表-创建、插入、删除、查找、反转等操作
来源:互联网 发布:windows pe 启动光盘 编辑:程序博客网 时间:2024/04/30 07:23
//list.h
#ifndef _List_H#define _List_H#include <stdio.h>#include <stdlib.h>#define NotFound NULL;typedef struct List {int value;struct Node * next;}Node;typedef struct List *pNode;typedef pNode pList;typedef pNode Position;pList creatList(int *array, int len);Position findNode(pList head, int x);pList lastInsertList(pList head, int insertvalue);pList InsertList(pList head, int val, int insertvalue);pList deleteNode(pList head, int delValue);pList reversalList(pList head);pList freeList(pList head);void printList(pList head);#endif /* _List_H */
//list.c
#include "singleList.h"pList creatList(int *array, int len){pList head,ptr,tmp;int i;head = (pList)malloc(sizeof(Node));if(!head){printf("分配内存失败!");return NULL;}head->next = NULL;head->value = array[0];tmp = head;for(i = 1; i < len; i++){ptr = (pList)malloc(sizeof(Node));if (!ptr){printf("分配内存失败!");return NULL;}ptr->value = array[i];ptr->next = NULL;tmp->next = ptr;tmp = tmp->next;}return head;}Position findNode(pList head, int x){pList ptr = head;while(ptr){if(ptr->value == x)return ptr;ptr = ptr->next;}return NotFound;}//在末节点插入pList lastInsertList(pList head, int insertvalue){pList tmp,ptr;if(!head)return NULL;ptr = head;while(ptr->next)ptr = ptr->next;tmp = (pList)malloc(sizeof(Node));ptr->next = tmp;tmp->value = insertvalue;tmp->next = NULL;return head;}//在值为val的节点前插入pList InsertList(pList head, int val, int insertvalue){pList temp = head->next;pList ptr = head;if(head->value == val){pList tmp = (pList)malloc(sizeof(Node));tmp->next = head;tmp->value = insertvalue;head = tmp;return head;}while(temp){if(temp->value == val){pList tmp = (pList)malloc(sizeof(Node));tmp->next = temp;tmp->value = insertvalue;ptr->next = tmp;return head;}temp = temp->next;ptr = ptr->next;}return NotFound;}pList deleteNode(pList head, int delValue){pList ptr,temp;ptr = head;temp = head->next;if(ptr->value == delValue){pList p = ptr;ptr = ptr->next;head = ptr;free(p);return head;}while(temp){if(temp->value == delValue){temp = temp->next;ptr->next = temp;return head;}temp = temp->next;ptr = ptr->next;}return NotFound;}pList reversalList(pList head){pList ptr,temp;ptr = head;temp = head->next;while(temp->next){pList p = temp;temp = temp->next;p->next = ptr;ptr = p;}temp->next = ptr;head->next = NULL;return temp;}pList freeList(pList head){pList ptr;while(head){ptr = head;head = head->next;free(ptr);}return head;//注意内存分配问题}void printList(pList head){if(!head){printf("the list is empty!\n");return;}printf("the list is : ");while(head){printf("%d ",head->value);head = head->next;}printf("\n");}
//main.c
#include "singleList.h"int main(void){int array[] = {1,2,3,4,5,6,7,8,9,10};int len = sizeof(array)/sizeof(*array);int insertvalue = 100;Position p;pList head = creatList(array,len);printList(head);head = reversalList(head);printList(head);p = findNode(head,5);printf("the position value is : %d\n",p->value);//在元素为10前面插入100head = InsertList(head,10,insertvalue);printList(head);//在末节点后面插入10000head = lastInsertList(head,10000);printList(head);//删除10000的节点head = deleteNode(head,10000);printList(head);head = deleteNode(head,1);printList(head);head = deleteNode(head,5);printList(head);//释放链表head = freeList(head);printList(head);}
- 单链表-创建、插入、删除、查找、反转等操作
- 单链表的创建、插入,删除、查找等操作
- 单循环链表-创建、插入、删除、反转等操作
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 链表操作:创建,插入,删除,查找等功能
- 单链表的创建、插入、删除等操作
- 单链表的创建,插入删除等操作
- 单链表的创建、插入删除等操作
- c/c++单链表的创建和删除,插入,反转操作
- 单链表操作,创建,遍历,插入,删除,排序等操作
- 线性表的基本操作,包括:创建、插入、删除、查找等基本操作
- 单链表基本操作的实现--创建、插入、查找、删除
- 单链表创建插入删除打印反转
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- 【二叉树】二叉搜索树创建、插入、删除、查找等操作
- 头插法和尾插法创建链式循序表 遍历、插入、删除、查找元素等操作
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 单链表插入删除等操作
- 快速迭代,积极改变,寻找对的状态
- Windows Store apps开发[23]创建Windows Store应用程序包
- C#几个经常用到的字符串的截取
- ASN.1入门
- Android中View绘制优化二一---- 使用<include />标签复用布局文件
- 单链表-创建、插入、删除、查找、反转等操作
- Ubuntu 12.04 下 Openstack Essex安装过程详解--Dashboard
- 想念那一池莲花
- 关于希腊字母表
- checklistbox用法总结
- C/C++/Window 文件操作
- 末日的爱情
- Linux下电信等IP地址汇总并以 网段/掩码 格式显示
- rpm 学习