链表转置实现
2013-03-03 23:06
176 查看
链表转置功能实现,不足之处 ;还请指正
#include <stdio.h> #include <stdlib.h> typedef struct Listnode { int nodedata; struct Listnode* next; }Listnode, *pListnode; //create list //return value the number of node created //param the head of the listnode int CreateListnode(pListnode head, int nodenumber) { int ret = 0; if(NULL == head) { return ret; } int input = 0; Listnode* front = NULL; Listnode* rear = head; //conect for(int i = 0; i < nodenumber; i++) { front = (Listnode*)malloc(sizeof(Listnode)); if(NULL != front) { scanf("%d", &input); front->nodedata = input; front->next = NULL; } rear->next = front; //append rear = rear->next; ++ret; } return ret; }
bool Traversallist(pListnode head) { bool ret = false; if(NULL == head) { return ret; } Listnode* p = head->next; while(p) { printf(" %d", p->nodedata); p = p->next; } printf("\n"); ret = true; return ret; }
bool ConvertList(pListnode head) { bool ret = false; if(NULL == head) { return ret; } Listnode* rear = head->next; Listnode* front = head->next->next; //init the ptr Listnode* p = front; if(NULL == p) //only one element { ret = true; return ret; } rear->next = NULL; //disconnect the list while(NULL != p) //get the current element { front = front->next; //insert the element head->next = p; p->next = rear; rear = p; p = front; } ret = true; return ret; }
int releaseListnode(Listnode* head) { int ret = 0; Listnode* rear = head->next; Listnode* front = head->next; while(NULL != rear) { front = front->next; free(rear); rear = NULL; rear = front; ++ret; } printf("The release number of element is %d\n", ret); return ret; }
int main() { printf("sizeof(Listnode) == %d\n", sizeof(Listnode)); pListnode mynode = (pListnode)malloc(sizeof(Listnode)); mynode->next = NULL; printf("Please Input the number for the node:"); int nodecount = 0; scanf("%d", &nodecount); int nodenumber = CreateListnode(mynode, nodecount); printf("nodenumber ===== %d\n", nodenumber); Traversallist(mynode); printf("\n after convert the list:\n"); bool ret = ConvertList(mynode); Traversallist(mynode); int relnum = releaseListnode(mynode); free(mynode); mynode = NULL; return 0; }
相关文章推荐
- Program work 5. 用链表实现矩阵及实现矩阵转置
- 【算法题】使用递归和非递归实现单向链表的转置
- 链表的实现
- 数组去实现链表
- 剑指offer----链表中环的入口节点----java实现
- 程序一:带头结点双向链表的简单实现
- 链表基础操作+实例[C语言实现]
- C语言循环链表实现约瑟夫问题
- C语言通用数据类型链表的实现,(数据域为指针,可指向任意类型数据)
- PHP实现合并两个排序链表的方法
- 实现单链表反转
- [VB.NET]急!!!! 实现矩阵转置,即将矩阵的行,列互换,一个3行2列的矩阵将转换为2行3列.
- C 双向链表的实现
- 链表的C语言实现之动态内存分配(转载)
- C++链表结构简单实现
- 用邻接链表存储图并且实现拓扑排序
- C++实现链表基本操作
- LabVIEW中实现链表、树等数据结构
- 单向循环链表实现约瑟环问题
- C++实现链表逆序