单循环链表-创建、插入、删除、反转等操作
来源:互联网 发布:java建造者模式实例 编辑:程序博客网 时间:2024/05/17 08:54
//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;}ptr->next = head;//单循环增加return head;}Position findNode(pList head, int x){pList ptr = head;while(ptr){if(ptr->value == x)return ptr;ptr = ptr->next;if(ptr == head)//单循环增加break;}return NotFound;}//在值为val的节点前插入pList InsertList(pList head, int val, int insertvalue){pList temp = head;pList ptr = head;pList tmp;while(ptr){if (ptr->value == val){while(temp->next != ptr)temp = temp->next;tmp = (pList)malloc(sizeof(Node));tmp->next = ptr;tmp->value = insertvalue;temp->next = tmp;return head;}ptr = ptr->next;if (ptr == head){printf("no such value can be insert!\n");break;}}return NotFound;}pList deleteNode(pList head, int delValue){pList ptr,temp;ptr = temp = head;while(ptr){if (ptr->value == delValue)//是“==”注意{while(temp->next != ptr)temp = temp->next;//找到ptr的上一个节点temptemp->next = ptr->next;free(ptr);if (head == ptr)//说明删除的是头节点{return temp->next;}return head;//当删除头节点时就不行}ptr = ptr->next;if (ptr == head)//说明循环了一周{printf("the delValue had not found!\n");break;}}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;if(temp->next == head)//单循环增加break;}temp->next = ptr;head->next = temp;//NULL改成tempreturn temp;}pList freeList(pList head){pList ptr;pList p = head;//单循环新增while(head){ptr = head;head = head->next;free(ptr);if(head == p)//单循环新增{head = NULL;break;}}return head;//注意内存分配问题}void printList(pList head){pList ptr = head;if(!ptr){printf("the list is empty!\n");return;}printf("the list is : ");while(ptr){printf("%d ",ptr->value);ptr = ptr->next;if(ptr == head)//单循环增加break;}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->next); head = reversalList(head->next); printList(head); p = findNode(head,5); printf("the position value is : %d\n",p->value);//在元素为10前面插入100head = InsertList(head,10,insertvalue);printList(head);//删除10000的节点head = deleteNode(head,10); printList(head);//删除头节点head = deleteNode(head,1);printList(head);//释放链表head = freeList(head);printList(head);}
与单链表相比,在插入和删除操作都简化了。
- 单循环链表-创建、插入、删除、反转等操作
- 单循环链表的创建、插入删除等操作
- 单链表-创建、插入、删除、查找、反转等操作
- 链表的基本操作,创建、删除、插入、反转
- 数据结构之单向链表操作1-(插入,删除,交换,反转,排序等操作)
- 链表操作:创建,插入,删除,查找等功能
- 链表创建、查询、删除、插入等操作
- 双循环链表的创建以及插入删除等操作
- 链表的创建、插入、删除等操作
- 链表操作:创建,插入,排序,反转
- 链表操作:创建,插入,排序,反转
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- C语言各种链表操作(创建、打印、删除、插入、反转)
- 单循环链表的初始化、插入、删除、遍历、查找
- 链表的创建,插入,删除,显示,反转
- 数据结构-链表创建,删除,插入,反转,连接
- 顺序表的创建、遍历、插入、删除等操作demo
- 单链表的创建、插入、删除等操作
- db_name、db_domain、service_name、global_name、instance_name、oracle_sid
- 伪代码排版包 clrscode3e——Introduction to Algorithms Third Edition
- XX教XX控件安装记录
- 新版linux系统设备架构中关于电源管理方式的变更
- oracle数据库创建表空间、创建用户、为用户授权、删除用户、更改用户密码、扩大表空间
- 单循环链表-创建、插入、删除、反转等操作
- 淡定人生,享受平静
- Ubuntu JDK安装配置的详细步骤:
- qt socket
- 入静与入世
- 抬头看路,低头走路
- jQuery遍历对象/数组/集合
- 浮华落寂
- ADO VC++ SQL server