标签:free div 空格 while 节点 list 初始化 inf out
typedef struct LNode { Elemtype data; struct LNode *next; }*Linklist;
void CreatList(Linklist &L) { Linklist p, h; cout << "请输入链表的长度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = NULL; h = L; if (!L) { cout << "申请空间失败" << endl; } cout << "请输入新节点的数值" << endl; while (n != 0) { p = (Linklist)malloc(sizeof(LNode)); cin >> p->data; p->next = h->next; h->next = p; h = h->next; n--; } }
void GetElem(Linklist &L) { cout << "请输入要查找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != NULL && e != 0) { p = p->next; e--; } if (e != 0) { cout << "您所查找的位置不在本链表中" << endl; } else { cout << "您所查找的元素为:" << p->data << endl; } }
void InsertElem(Linklist &L) { Elemtype data; int e; cout << "请输入插入的元素值和位置(以空格隔开)" << endl; cin >> data; cin >> e; Linklist s, p = L; s = (Linklist)malloc(sizeof(LNode)); s->data = data; while (p->next != NULL && e != 1) { p = p->next;; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } s->next = p->next; p->next = s; }
void DeleteElem(Linklist &L) { int e; Linklist p = L,q = NULL; cout << "请输入删除元素位置" << endl; cin >> e; while (p->next != NULL && e != 1) { p = p->next; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } else { q = p->next; p->next = q->next; free(q); } }
void ShowList(Linklist &L) { cout << "遍历一遍链表" << endl; Linklist l = L; while (l->next != NULL) { l = l->next; cout << l->data << ‘ ‘; } cout << endl; }
#include"stdafx.h" #include <iostream> #include<stdlib.h> using namespace std; typedef int Elemtype; //定义一个单链表结构 typedef struct LNode { Elemtype data; struct LNode *next; }*Linklist; //初始化链表 void CreatList(Linklist &L) { Linklist p, h; cout << "请输入链表的长度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = NULL; h = L; if (!L) { cout << "申请空间失败" << endl; } cout << "请输入新节点的数值" << endl; while (n != 0) { p = (Linklist)malloc(sizeof(LNode)); cin >> p->data; p->next = h->next; h->next = p; h = h->next; n--; } } //按位置查找 void GetElem(Linklist &L) { cout << "请输入要查找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != NULL && e != 0) { p = p->next; e--; } if (e != 0) { cout << "您所查找的位置不在本链表中" << endl; } else { cout << "您所查找的元素为:" << p->data << endl; } } //插入一个元素 void InsertElem(Linklist &L) { Elemtype data; int e; cout << "请输入插入的元素值和位置(以空格隔开)" << endl; cin >> data; cin >> e; Linklist s, p = L; s = (Linklist)malloc(sizeof(LNode)); s->data = data; while (p->next != NULL && e != 1) { p = p->next;; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } s->next = p->next; p->next = s; } //删除一个元素 void DeleteElem(Linklist &L) { int e; Linklist p = L,q = NULL; cout << "请输入删除元素位置" << endl; cin >> e; while (p->next != NULL && e != 1) { p = p->next; e--; } if (e > 1) { cout << "您输入的位置超过本表的长度啦!" << endl; } else { q = p->next; p->next = q->next; free(q); } } //遍历一遍链表 void ShowList(Linklist &L) { cout << "遍历一遍链表" << endl; Linklist l = L; while (l->next != NULL) { l = l->next; cout << l->data << ‘ ‘; } cout << endl; }
标签:free div 空格 while 节点 list 初始化 inf out
原文地址:https://www.cnblogs.com/Lazy-Cat/p/9835431.html