鍊錶c 實現

2021-07-05 23:27:01 字數 2711 閱讀 3545

昨天把鍊錶實現**寫到  寫鍊錶收穫 裡了,今天看了看居然沒有,不知道是沒儲存還是怎麼了,沒辦法,今天就把自己的**再粘一遍吧

對了,是用vs2013編輯的

slnode.cpp

#include

struct slnode

slnode(const int& item, slnode* nextnode = null)

};sllist.h

#ifndef head

#define head

#include"slnode.cpp"  //鍊錶節點定義

using namespace std;

class sllist

sllist(const int& item)

~sllist(){};

void inserthead(const int& item);    //插入

void inserttail(const int& item);

void insert(int location, const int& item);  

int search(int& item);   //查詢

int find(int location);         //訪問

void deletehead();   //刪除

void deletetail();

void delte(int location);

void print();   //輸出鍊錶

};#endif

sllist.cpp

#include"sllist.h"

void sllist::inserthead(const int& item)

slnode* p=new slnode(item,head);      

head = p;

cout << "插入表頭" << "資料為:" << item << endl;

}void sllist::inserttail(const int& item)

while ( p->next != null)

p = p->next;

slnode* q = new slnode(item);

p->next = q;

cout << "插入表尾" << "資料為:" << item << endl;

}   

void sllist::insert(int location, const int& item)

if (head->data == null)

slnode* p;

p = head;

int i=1;

while (i < location&&p->next!=null)

if (i < location)

slnode* q = new slnode(item);

q->next = p->next;

p->next = q;

cout << "第" << location+1 << "個節點插入成功," << "插入資料為:" << item << endl;

}  int sllist::search(int& item)

if (p == null)

cout << "資料為"if (i < location)

int item;

item = p->data;

cout << "第" else if (head->next == null)

else

}void sllist::deletetail()

if (head->next == null)

while (p->next != null)

q->next = null;

cout << "被刪除的表尾資料為:"delete p;

}void sllist::delte(int location)

if (head->data == null)

slnode *p,*q;      //q指向p的前乙個節點,刪除p指向的節點用

p = q = head;

int i = 1;

while (i < location&&p->next != null)

if (i < location)

q->next = p->next;

cout << "第"delete p;

cout << "已刪除"<< endl;

}void sllist::print()

while (p->next != null)

cout << "[" << p->data << "]->" << "null" << endl;

}main.cpp

#include"sllist.h"

int main()

else if (j == 2)

else if (j == 3)

else

break;

}else if (i == 2)

else if (j == 2)

else if (j == 3)

else

break;

}else if (i == 3)

else if (i == 4)

else if (i == 5)

else          //退出

break;

}return 0;

}

鍊錶C 實現

node.h 第乙個檔案 ifndef node h define node h define true 1 define false 0 define ok 1 define error 0 define null 0 define flag 1 class node endif node h l...

C 鍊錶實現

include include include using namespace std typedef int datatype typedef struct node linknode 鍊錶初始化 初始化鍊錶頭部指標需要用二級指標或者一級指標的引用 void linklist init linkn...

C 鍊錶實現

typedef struct node node class linklist linklist bool clearsqlist bool isempty int length void getelem int i,int e int locateelem int e bool priorelem...