#define _crt_secure_no_deprecate
//#define _crt_secure_cpp_overload_standard_names 1
#includeusing namespace std;
typedef struct node
node;
node *headpointer;//頭指標
int listlength;//鍊錶長度
/*將頭指標加入到引數列表中*/
/* 1.初始化鍊錶,輸入資料,資料小於0結束輸入。*/
void initlist1()
headpointer->next = nullptr;
node *temp = headpointer;//頭節點的位址賦給接下來使用的中間變數
cout << "請輸入鍊錶資料(小於0結束輸入):\n";
while (1)//鍊錶賦值
cin >> p->data;
if (p->data < 0)
//資料小於0不可用,需要釋放記憶體
temp->next = p;
temp = p;
temp->next = nullptr;//相當於p->next=nullptr;賦值為空指標,是個好習慣
listlength++;
} cout << endl;
}/* 2.在鍊錶的第i個位置之 前 插入資料e */
void insertbefore(int i, int e)
node *p = headpointer;
node *temp = new node;//申請新記憶體
if (!temp)
while (--i)
p = p->next;//找到第i-1個位置
temp->data = e;
temp->next = p->next;
p->next = temp;
listlength++;
cout << "鍊錶長度為:" << listlength << " 資料插入成功!\n";
}/*在鍊錶第i個位置之 後 插入資料e*/
void insertafter(int i, int e)
node *p = headpointer;
node *temp = new node;//申請新記憶體
if (!temp)
while (i--)
p = p->next;
temp->data = e;
temp->next = p->next;
p->next = temp;
listlength++;
cout << "鍊錶長度為:" << listlength << " 資料插入成功!\n";
}/* 4. 刪除鍊錶的第i個位置的資料 */
bool deletelist(int i)
/* 5.列印鍊錶所有資料 */
bool printlist()
cout << "鍊錶長度為:" << listlength << "列印鍊錶:\n";
node *p = headpointer;
while (p->next != nullptr)
cout << "鍊錶列印成功!\n\n";
return true;
}/* 6. 清除鍊錶*/
bool clearlist()
node *p = headpointer;
node *temp;
while (p != nullptr)
cout << "鍊錶清除成功!\n" << "鍊錶長度為" << listlength + 1 << endl;//頭節點不計入鍊錶長度,而清除鍊錶時,頭節點也被清除
return true;
}/* 7.返回鍊錶中第i個結點中的資料的位址*/
int* getdata(int i)
node *p = headpointer;
while (i--) p = p->next;
return &(p->data);
}/* 8. 給定數值,判斷鍊錶中是否存在該數值,如果存在返回數值相同的第乙個結點的位址*/
node* findlist(int e)
return nullptr;
}/* 9. 修改鍊錶中第i個結點的資料*/
bool modifylist(int i, int e)
node *p = headpointer;
while (i--) p = p->next;
p->data = e;
return true;
}/* 10. 逆位序輸入n個元素的值*/
void initlist2(int n)
cin >> p->data;
p->next = headpointer->next;
headpointer->next = p; }}
/* 11. 按順序歸併兩個鍊錶*/
bool mergelist(node* &a, node* &b, node* &c)
else
}pc->next = pa ? pa : pb;
delete b;
return true;
}/*12.交換兩個節點的data*/
void swaplist(node *i,node *j)
/* 13. 鍊錶排序*/
//pstart指向首個元素的節點,不是頭節點;pend是null,也就是最後乙個節點的next
void sortlist(node *pstart,node *pend)
j = j->next;
} swaplist(firstnode, i);
sortlist(pstart, i);
sortlist(i->next, pend);
}/*14.已知線性表資料遞增有序,刪除表中所有值大於mink且小於maxk的資料,並釋放被刪除的結點*/
bool deleteyouwant1(int mink, int maxk)
else
p = p->next;
} return false;
}/*15.已知線性表資料遞增無序,刪除表中所有值大於mink且小於maxk的資料,並釋放被刪除的結點*/
void deleteyouwant2(int mink, int maxk)
else
p = p->next; }}
/*16.刪除資料相同的節點,僅只保留乙個*/
void uniquelist1()
else
}p = p -> next; }}
/*17.刪除資料相同的節點,僅只保留乙個*/
void uniquelist2()//和上面的版本不一樣之處就是,沒有frontnode指標保留前乙個結點,而是採用先判斷再去刪除的辦法
else
temp = temp->next;
} p = p->next; }}
int main()
線性表 單鏈表
單鏈表結構與順序儲存結構對比 一 儲存分配方式 1 順序儲存結構用一段連續的儲存單元依次儲存線性表的資料元素 2 單鏈表採用鏈式儲存結構,用一組任意的儲存單元存放線性表的元素 二 時間效能 1 查詢 順序儲存結構o 1 單鏈表o n 2 插入和刪除 順序儲存結構o n 單鏈表找到位置後插入刪除時間o...
線性表 單鏈表
template struct node template class linklist 無參建構函式,建立只有頭結點的空鍊錶 linklist t a int n 有參建構函式,建立有n個元素的單鏈表 linklist 析構函式 int length 求單鏈表的長度 t get int i 按位查...
線性表 單鏈表
1 單鏈表的結點定義 typedef struct node slnode 2 初始化listinitiate slnode head void listinitiate slnode head 初始化 3 求當前資料元素個數listlength slnode head int listlength...