C语言:单链表的实现(包括:尾插、尾删、头插、头删、指定位置插入、长度、打印)
2016-05-30 17:16
615 查看
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<assert.h> typedef int DataType; typedef struct SListNode { DataType _data; struct SListNode* _next; }SListNode; SListNode* _CreateNode(DataType x) { SListNode* head = (SListNode*)malloc(sizeof(SListNode)); head->_data = x; head->_next = NULL; return head; } void PushBack(SListNode*& head, DataType x) { if (head == NULL) { head = _CreateNode(x); head->_next = NULL; } else { SListNode* cur = head; while (cur->_next != NULL) { cur = cur->_next; } cur->_next = _CreateNode(x); } } void PopBack(SListNode*& head) { if (head == NULL) { return; } else if (head->_next == NULL) { free(head); head = NULL; } else { SListNode* cur = head; SListNode* next = head; while (cur) { next = cur->_next; if (next != NULL && next->_next == NULL) { free(next); cur->_next = NULL; return; } cur = cur->_next; } } } void PushFront(SListNode*& head,DataType x) { if (head == NULL) { head = _CreateNode(x); } else { SListNode* pcur = _CreateNode(x); pcur->_next = head; head = pcur; } } void PopFront(SListNode*& head) { if (head == NULL) { return; } else if (head->_next == NULL) { free(head); head = NULL; } else { SListNode* del = head; SListNode* next = head->_next; free(del); del = NULL; head = next; } } void Insert(SListNode* head,int pos,DataType x) { assert(pos >= 0); SListNode* cur = head; while (--pos && cur) { cur = cur->_next; } if (pos > 0) { printf("pos位置大于链表长度!\n"); return; } SListNode* newcur = _CreateNode(x); if (cur->_next) { SListNode* next = cur->_next; cur->_next = newcur; newcur->_next = next; } else if (cur->_next == NULL) { cur->_next = newcur; } } size_t Length(SListNode*& head) { size_t count = 0; SListNode* cur = head; while (cur) { count++; cur = cur->_next; } return count; } void PrintSList(SListNode*& head) { SListNode* cur = head; while (cur) { printf("%d->", cur->_data); cur = cur->_next; } printf("\n"); } void Test() { SListNode* sList =NULL; PushBack(sList, 1); PushBack(sList, 2); PushBack(sList, 3); PushBack(sList, 4); PushBack(sList, 5); PrintSList(sList); PopBack(sList); PrintSList(sList); PushFront(sList, 0); PrintSList(sList); PopFront(sList); PrintSList(sList); Insert(sList, 3, 10); PrintSList(sList); int ret = Length(sList); printf("单链表长度为:%d\n", ret); } int main() { Test(); system("pause"); return 0; }
本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1734401
相关文章推荐
- C语言:【单链表】删除一个无头单链表的非尾节点
- C语言:【单链表】在无头单链表的一个非头节点前插入一个节点
- C语言:【单链表】逆置反转单链表
- C语言:【单链表】查找单链表的中间节点,要求只能遍历一次
- C语言:【单链表】查找单链表的倒数第k个节点,要求只能遍历一次
- C++:复数类构造函数、拷贝构造、运算符重载、析构函数
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- C语言【顺序表】冒泡排序,一次选出最大最小的数据,二分查找,初始化顺序表
- C语言【顺序表】顺序表的初始化,头插,尾插,头删,尾删,增删查改,全删
- C++:【常见面试题】String类的写法
- C++【String类】String查找单个字符,查找字符串的函数实现
- C++【String类】String删除单个字符,删除字符串的函数实现
- C++【String类】String插入单个字符,插入字符串的函数实现
- C++【String类】String头插单个字符,头删单个字符的函数实现
- C++【常见面试题】String类的实现,以及深拷贝、浅拷贝问题
- 【笔试常考】C语言:深度剖析strlen,sizeof
- 【C语言】注释不可以嵌套使用的举例
- 【C语言】局部变量、全局变量,局部静态变量,全局静态变量,extern,static的区别
- C/C++(&Java) Volatile关键词深度剖析
- C语言;在终端输入多行信息,找出包含“ould”的行,并打印改行。