typedef struct listnode listnode;
//建立乙個單鏈表
listnode *create_list()
q->next = null;
return head;
}
//單鏈表的列印
void print_list(listnode *head)
while(p != null)
cout << endl;
}
//測試單鏈表的長度
int length_list(listnode *head)
return len;
}
//單鏈表的查詢
listnode *search_list(listnode *head, int pos)
if(pos == 0)
if(p == null)
while(--pos)
} return p;
}
//單鏈表的插入
listnode *insert_list(listnode *head, int pos, int data)
p = search_list(head, pos);
if(p != null)
return head;
}
//單鏈表的刪除
listnode *delete_list(listnode *head, int pos)
p = search_list(head, pos - 1);
if(p != null && p->next != null)
return head;
}
//將單鏈表逆置
//例如原煉表為: 1->2->3->4;逆置後為: 4->3->2->1
listnode *reverse_list(listnode *head)
p = head->next;
q = p->next;
p->next = null;
while(q != null)
return head;
}
//查詢單鏈表中間的元素,只需要一次遍歷
listnode *search_middle(listnode *head)
i++;
current = current->next;
} return middle;
}
int main()
cout << endl;
cout << "the input insert pos and data is: ";
cin >> pos >> data;
head = insert_list(head, pos, data);
print_list(head);
cout << endl;
cout << "please input the delete pos: ";
cin >> pos;
head = delete_list(head, pos);
print_list(head);
head = reverse_list(head);
print_list(head);
item = search_middle(head);
cout << "the middle data is : " << item->data << endl;
}
單鏈表操作
include include typedef struct node tag node 建立不帶頭結點的單鏈表 node createnode else p q scanf d n 函式體結束,q指標變數被釋放,不能通過head引數帶回到呼叫函式 要把head帶回到呼叫函式,要把它定義為指向指標的...
單鏈表操作
include stdio.h include malloc.h include define n 10 代表要處理的元素個數 可以誰使用者的意思修改 define ok 1 define overflow 0 typedef int elemtype typedef int status type...
單鏈表操作
這一次補上鍊表的注釋,是空閒的時候敲出來的,如果有錯,希望幫忙糾正 部分給出了詳細說明,這裡只選取了基本操作,因為更複雜的鍊錶操作太繁瑣,這裡就不寫了 如果有什麼不懂的地方,可以隨時詢問 include using namespace std typedef int elemtype struct ...