下面是本人總結的關於帶頭結點的單鏈表的實現代碼。供大家參考學習。
#include<iostream>
using namespace std;
typedef char datatype;
struct node
{
datatype data;
node * next;
};
//按序號查找鏈表
node * getnode(node * head, int i)
{
if(head->next == NULL)
return NULL;
node * p = head;
int j = 0;
while((j < i) && (p->next != NULL))
{
p = p->next;
j++;
}
if(i > j)
return NULL;
else
return p; //說明沒找到第 i 個結點
}
//按值查找
node * locatenode(node * head, datatype key)
{
if(head->next == NULL)
return NULL;
node * p = head->next;
while((p->data != key)&& (p->next != NULL))
{
p = p->next;
}
if(p->data == key)
return p;
else
return NULL;
}
//刪除第 i 個結點的實現
int link_del(node * head, int i)
{
if(head->next == NULL)
return -1;
node * p = head;
node * r;
int j = 1;
while((j < i) && (p->next != NULL))
{
p = p->next;
j++;
}
if((j < i) || (p->next == NULL))
return -2;
r = p->next;
p->next = p->next->next;
free(r);
return 0;
}
//按序號插入
int link_insert(node* head, int i, datatype x) //i>0,即i=1,2,3... 表示第i個位置
{
if(head->next == NULL)
return -1;
int j = 1;
node * p = head;
while((j<i)&&(p->next != NULL))
{
p = p->next;
j++;
}
if((i<=0)||(j < i)) //插入位置 i 錯誤
return -2;
//cout << p->data << endl;
node * s = (node*)malloc(sizeof(node));
s->data = x;
s->next = p->next;
p->next = s;
return 0;
}
//頭插法建表
node * creat_unbrainlink()
{
node * s;
node * head, * p;
p = head = (node *)malloc(sizeof(node)); // 頭結點
head->next = NULL; //這句很重要,說明鏈表起初爲空
char ch = getchar();
while(ch != '\n')
{
s = (node *)malloc(sizeof(node));
s->data = ch;
s->next = head->next;
head->next = s;
ch = getchar();
}
return head;
}
//尾插法建表
node * creat_unbrainlink_end()
{
node * head, *p, *s;
head = (node *)malloc(sizeof(node));
p = head;
char ch = getchar();
while(ch != '\n')
{
s = (node *)malloc(sizeof(node));
s->data = ch;
p->next = s;
p = s;
ch = getchar();
}
//if(p != NULL)
p->next = NULL;
//s->next = NULL; //這樣寫也可以
return head;
}
// 創建大小爲n的鏈表
node * creat_link(int n)
{
if(n < 1)
return NULL;
node * head, * p, *s;
p = head = (node *)malloc(sizeof(node)); //頭結點嘛?應該是
for(int i = 1; i <= n; i++)
{
s = (node * )malloc(sizeof(node));
datatype x;
cin >> x;
s->data = x;
p->next = s;
p = s;
}
p->next = NULL;
return head;
}
//鏈表顯示函數
int show(node * head)
{
if(head == NULL)
return -1;
node * p;
p = head->next; //p爲頭結點
while(p->next != NULL) //爲NULL說明該鏈表爲空
{
cout << p->data << ends;
p = p->next;
}
cout << p->data << endl;
return 0;
}
//刪除所有鏈表,並釋放空間
int DestroyList(node* &head)
{
node* temp = NULL;
node* p = NULL;
if(head == NULL)
{
cout << "鏈表不存在" << endl;;
return -1;
}
temp = head;
//temp->data = 'H';
while(temp->next != NULL)
{
//cout << temp->data << endl;
p = temp->next;
free(temp);
temp = p;
}
//cout << temp->data;
free(temp);
head = NULL;
return 0;
}
int main()
{
node * head;
head = creat_link(5);
//head = creat_unbrainlink();
//head = creat_unbrainlink_end();
show(head);
link_del(head,3);
show(head);
//node * locte_node = locatenode(head, 's');
//if(locte_node != NULL)
//cout << locte_node->data << endl;
// node* get_node = getnode(head, 3);
//if(get_node != NULL)
//cout << get_node->data << endl;
int result = link_insert(head, 5, 90);
//cout << "result = " << result << endl;
show(head);
DestroyList(head);
show(head);
system("pause");
return 0;
}