创建无头结点单链表
2018-04-01 23:23
232 查看
#include "stdafx.h" #include <iostream> using namespace std; typedef int ElemType; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: //创建无头结点的单链表 ListNode* Create(void) { char s; ListNode *tail = NULL; ListNode *head = NULL; while (1) { ListNode *tmp = new ListNode(0); //定义一个s节点用来存放每次要输入的值 scanf("%d", &tmp->val); if (head == NULL) { head = tmp; } else { tail->next = tmp; } tail = tmp; s = getchar(); if (s == '\n') { break; } } if (tail != NULL) tail->next = NULL; return head; } //输出无头结点单链表 void List(ListNode *L) { ListNode *p; p = L; // while (p != NULL) { printf("%2d", p->val); p = p->next; } } }; int main() { ListNode *L1; int temp; printf("请输入链表:\n"); L1 = Solution().Create(); printf("无头结点的链表为:\n"); Solution().List(L1); system("pause"); return 0; }
相关文章推荐
- 单链表的创建(有头结点和无头结点)
- 单链表中头结点的有无. 并讨论下有无头结点在单链表的创建,打印,插入,逆置,删除中的区别.
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- C语言进阶-第18讲:单链表的遍历、创建、插入和删除结点
- 单链表的创建和遍历、求单链表中节点的个数、查找单链表中的中间结点、判断单链表是否有环、取出有环链表中环的长度,删除有序链表中的重复结点
- 在无头结点的动态单链表上实现线性表操作Delete(L,i)
- 单链表(带头结点)的创建
- 在无头结点的动态单链表上实现线性表操作Insert(L,i,b)
- 错误总结:无头结点链表的创建(二级指针)
- 无头结点链表的创建及删除
- 单链表的创建、插入、删除、销毁以及查找中间结点
- 有/无头节点的单链表的创建(尾插法)
- C语言单链表的创建和添加结点
- 链表之无头指针删除单链表指定结点
- 单链表的创建--带头结点
- C语言实现单链表的创建和添加结点
- 面试题:向无头单链表的某一已知结点之前插入结点
- 无头单链表的创建,及无头单链表逆转
- 单链表---删除无头单链表的非尾结点(不遍历链表)
- 单链表(带头结点)的创建与倒置