C 實現單鏈表

2021-08-05 21:47:30 字數 2344 閱讀 6266

鍊錶的頭節點head可以看作鍊錶的索引為0的節點,count用來給插入的節點計數。 鍊錶為空時, 只有頭結點head, 此時對於的count=0

node標頭檔案:

#ifndef node_h

#define node_h

class node;

#endif

node的cpp檔案:

#include "node.h"

#include

using

namespace

std;

node::node()

node::~node(){}

list標頭檔案:

#ifndef list_h

#define list_h

#include "node.h"

class

list;

#endif

list的cpp檔案:

#include "list.h"

#include

#include

using namespace std;

list::list()

list::~list()

void list::clearlist()

m_plisthead->next = null;

}bool list::isemptylist()

int list::getlistsize()

bool list::getelemt(int i, node &node)

node.data = curnode->data;

return

true;

}int list::findelemt(node &node)

return -1;

}bool list::getpre(node *cur, node & pre)

prenode = curnode;

curnode = curnode->next;

}return

false;

}bool list::getpost(node *cur, node & post)

curnode = postnode;

postnode = postnode->next;

}return

false;

}bool list::insertelemt(int i, node * node)

node * newnode = new node;

if (!newnode)

newnode->data = node->data;

newnode->next = curnode->next;

curnode->next = newnode;

m_icount++;

return

true;

}bool list::deleteelemt(int i, node & node)

node.data = curnode->data;

prenode->next = curnode->next;

delete curnode;

m_icount--;

return

true;

}bool list::inserthead(node &node)

newnode->data = node.data;

newnode->next = m_plisthead->next;

m_plisthead->next = newnode;

m_icount++;

return

true;

}bool list::inserttail(node * node)

node * newnode = new node;

if (!newnode)

newnode->data = node->data;

curnode->next = newnode;

newnode->next = null;

m_icount++;

return

true;

}void list::printlist()

cout << endl;

}

C 單鏈表實現

1 單向鍊錶 單向鍊錶 include include class cnode 節點類 class clist 鍊錶類 cnode movetrail cnode pnode 移動到尾節點 return ptmp void addnode cnode pnode 新增節點 else m nodesu...

c 實現單鏈表

include include using namespace std typedef int datatype struct linknode 建立乙個節點 class slist void swap slist s slist const slist s head null tail null ...

單鏈表(C實現)

ifndef list h define list h typedef struct node node typedef struct list list initlist int insertlist list l,void data,int size node findnodebykey lis...