C 實現雙向鍊錶

2021-08-08 16:19:44 字數 2543 閱讀 4676

雙向鍊錶結構:

實現:

list

.h#pragma once

#include

#include

<

string

>

#include

using namespace std;

typedef int datetype;

struct node

node(const datetype &

data)

:_pnext(null)

,_pprev(null)

,_data(data)

{}node *_pnext;

node *_pprev;

datetype _data;

};class list

void display(); //列印

void pushback(const datetype data); //尾部插入

void popback(); //尾部刪除

void pushfront(const datetype data); //頭部插入

void popfront(); //頭部刪除

void erase(node *pos); //刪除指定節點

node* find(datetype &d); //查詢節點位置

void insert(node*pos, const datetype &

data); //指定地點插入

size_t size(); //大小

void clear(); //清空鍊錶

private:

node *_phead;

};list

.cpp

#define _crt_secure_no_warnings 1

#include

"list.h"

void

list

::display()

while (cur !=

null)

cout << endl;

}void

list

::pushback(const datetype data)

node *cur =

new node(data);

next->_pnext = cur;

cur->_pprev = next;

cur->_pnext =

null;

}void

list

::popback()

node *cur = _phead;

while ((cur !=

null) && (cur->_pnext !=

null))

node *dest = cur->_pprev;

delete cur;

dest->_pnext =

null;

}void

list

::pushfront(const datetype data)

node *

data

=new node(data);

node *cur = _phead->_pnext;

_phead->_pnext =

data;

data

->_pprev = _phead;

data

->_pnext = cur;

cur->_pprev =

data;

}void

list

::popfront()

node*next = _phead->_pnext->_pnext;

node*cur = _phead->_pnext;

delete cur;

if (next ==

null)

_phead->_pnext = next;

next->_pprev = _phead;

}void

list

::erase(node *pos)

node*

list

::find(datetype &d)

return

null;

}void

list

::insert(node*pos, const datetype &

data)

size_t list

::size()

return count;

}void

list

::clear()

node *cur = _phead->_pnext;

node *next = _phead->_pnext->_pnext;

while (cur->_pnext !=

null)

}

雙向鍊錶(C實現)

list.h ifndef list h define list h typedef struct node node typedef struct list list initlist int insertnode list l,void data,int size int deletenode ...

雙向鍊錶(c 實現)

雙向鍊錶與單鏈表有許多的相似之處,但是也有不同之處。雙向鍊錶與單鏈表主要的不同在於 雙向鍊錶可以從兩個方向進行遍歷,但是單鏈表只能從頭節點遍歷到尾節點,不能從尾節點遍歷到頭節點,對於鍊錶中一些插入和刪除等操作,雙向鍊錶較單鏈表更為簡單些。所以,雙向鍊錶有其存在的特殊意義。下面是通過c 來實現雙向鍊錶...

雙向鍊錶 C 實現

雙向鍊錶在類中的實現 include include includeusing namespace std typedef int datatype struct node node pnext node ppre int data class list list const list l else...