C 資料結構 雙向鍊錶的基本操作

2021-07-04 18:34:34 字數 4683 閱讀 5739

#pragma once

#ifndef _dlist_h

#define _dlist_h

#include #include using namespace std;

typedef enum status;

templateclass list;

templateclass listnode

listnode(type d, listnode*n = null, listnode*p = null)

: data(d), next(n), prio(p)

{} ~listnode()

{} void setdata(const type &d)

type getdata()

private:

type data;

listnode*next;

listnode*prio;

};templateclass list

~list()

{}public:

status push_back(const type &x)

status push_front(const type &x)

else

size++;

return true;

} status pop_back()

listnode*p = first;

listnode*q = p->next;

while (q->next != null)

delete last;

last = p;

last->next = null;

size--;

return true;

} status pop_front()

if (size == 1)

else

size--;

return true;

} status insert_val(const type &x)

if (p->next == null)

else

return true;

} status delete_val(const type &x)

listnode*p = first;

listnode*q = p->next;

while (q != null && q->data != x)

if (q->next == null)

else

return true;

} status modify_val(const type &x, const type &y)

s->data = y;

return true;

} status merge(list<1, list<2)

else

}while (p != null)

while (q != null)

s->next = null;

size = lt1.size + lt2.size;

return true;

} void show_list()

cout << "null" << endl;

} int length()

void clear()

if (p->next == null)

last = first;

last->next = null;

size = 0;

} void destroy()

void sort()

listnode*p = first->next;

listnode*q = p->next;

last = p;

last->next = null;

while (q != null)

if (s->next == null)

else

}} void resver()

listnode*p = first->next;

listnode*q = p->next;

last = p;

last->next = null;

while (q != null)

}listnode*find(const type &key) const

listnode*p = first->next;

while (p != null && p->data != key)

if (p == null)

return p;

} listnode*next(const type &x) const

listnode*s = find(x);

if (s == null)

return s->next;

} listnode*next(listnode*p) const

listnode*q = first;

while (p->next != q)

return q;

} listnode*prio(const type &x) const

listnode*p = first;

listnode*q = p->next;

while (q != null && q->data != x)

return p;

} listnode*prio(listnode*p) const

listnode*pr = first;

while (pr->next != p)

return pr;

}private:

listnode*first;

listnode*last;

size_t size;

};#endif

#include "dlist.h"

void main()

break;

case 2:

cout << "請輸入要插入的資料(-1結束):>";

while (cin >> item, item != -1)

break;

case 3:

system("cls");

mylist.show_list();

system("pause");

break;

case 4:

mylist.pop_back();

break;

case 5:

mylist.pop_front();

break;

case 6:

cout << "請輸入要插入的值:>";

cin >> item;

mylist.insert_val(item);

break;

case 7:

cout << "請輸入要刪除的值:>";

cin >> item;

mylist.delete_val(item);

break;

case 8:

for (int i = 1; i < 10; i += 2)

for (int i = 2; i < 10; i += 2)

mergelist.merge(mylist, youlist);

mergelist.show_list();

break;

case 9:

cout << "請輸入要查詢的值:>";

cin >> item;

cout << "所在查詢值的後繼為:" << mylist.next(item) << endl;

break;

case 10:

cout << "請輸入要查詢的值:>";

cin >> item;

cout << "該值指標為:" << mylist.find(item) << endl;

break;

case 11:

mylist.sort();

break;

case 12:

mylist.resver();

break;

case 13:

cout << "線性表的長度為:" << mylist.length() << endl;

break;

case 14:

mylist.clear();

break;

case 15:

mylist.destroy();

break;

case 16:

cout << "請輸入要修改的值:>";

cin >> item;

cout << "請輸入修改後的值:>";

cin >> pos;

mylist.modify_val(item, pos);

break;

case 17:

cout << "請輸入要查詢的值:>";

cin >> item;

cout << "所在查詢值的前驅為:" << mylist.prio(item) << endl;

break;

default:

break;

} }}

執行介面如下:

資料結構雙向鍊錶 C

博主 牆角黑貓 1 原理解釋 雙向鍊錶的理解是在每個節點之間存在雙向的連線,例如和單鏈表對比 單鏈表是 a b c d 每個節點是單向連線的,a只能指向b,從b無法直接到a。而雙向鍊錶是 a b c d 這樣就實現了a和b的完全互通。大家可以結合下圖理解一下!2 部分 include include...

C 資料結構 雙向鍊錶

鍊錶的概念以及鍊錶與陣列的差異不做過多的敘述,相信大家都耳熟能詳,這裡以c 語言實現簡單的雙向鍊錶,作為備用,記錄下 否則逆序查詢 var prenode head.prev for int i 0 i count 1 index i prenode prenode.prev return pren...

C 資料結構 雙向鍊錶

鍊錶的概念以及鍊錶與陣列的差異不做過多的敘述,相信大家都耳熟能詳,這裡以c 語言實現簡單的雙向鍊錶,作為備用,記錄下 否則逆序查詢 var prenode head.prev for int i 0 i count 1 index i prenode prenode.prev return pren...