標籤:鏈表 刪除元素 ios nbsp 迴圈 cout amp 申請 --
定義一個迴圈鏈表結構
typedef struct LNode{ Elemtype data; struct LNode *prev; struct LNode *next;}*Linklist;初始化鏈表
void CreatList(Linklist &L){ Linklist p, h; cout << "請輸入鏈表的長度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = L; L->prev = L->next; 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; p->prev = h; h = h->next; n--; } L->prev = h;}按位置尋找
void GetElem(Linklist &L){ cout << "請輸入要尋找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != L && 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 != L && 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 != L && 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 != L) { 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 *prev; struct LNode *next;}*Linklist;//初始化鏈表void CreatList(Linklist &L){ Linklist p, h; cout << "請輸入鏈表的長度" << endl; int n; cin >> n; L = (Linklist)malloc(sizeof(LNode)); L->next = L; L->prev = L->next; 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; p->prev = h; h = h->next; n--; } L->prev = h;}//按位置尋找void GetElem(Linklist &L){ cout << "請輸入要尋找的位置" << endl; int e; cin >> e; Linklist p = L; while (p->next != L && 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 != L && 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 != L && 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 != L) { l = l->next; cout << l->data << ‘ ‘; } cout << endl;}
覺得文章不錯,點個贊和關注喲.
雙向迴圈鏈表