#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head;
void setupNullList();//創(chuàng)建一個(gè)空鏈表
void insertTailList();//尾插鏈表
void insertHeadList();//頭插鏈表
struct node *creatNode();//創(chuàng)建一個(gè)結(jié)點(diǎn)
void appendNode(struct node *n);//追加結(jié)點(diǎn)
void insertNode(struct node *n);//插入結(jié)點(diǎn)
void deleteNode();//刪除結(jié)點(diǎn)
void updateNode();//修改結(jié)點(diǎn)
void reverseList();//鏈表逆序
void showList();//輸出鏈表
int listType = 0;
int main(){
setupNullList();
system("clear");
printf("\n1、頭插鏈表\n\n2、尾插鏈表\n\n請(qǐng)選擇:\n\n");
int flag = 0;
scanf("%d",&flag);
switch(flag){
case 1:{
listType = 1;
insertHeadList();
break;
}
case 2:{
listType = 2;
insertTailList();
break;
}
default:
break;
}
showList();
flag = 1;
int flagExit = 0;
while(flag){
printf("\n1性置、追加結(jié)點(diǎn)\n\n2、插入結(jié)點(diǎn)\n\n3蹦渣、刪除結(jié)點(diǎn)\n\n4、修改結(jié)點(diǎn)\n\n5貌亭、鏈表逆序\n\n6剂桥、輸出鏈表\n\n0、退出程序\n\n請(qǐng)選擇:\n\n");
scanf("%d",&flag);
switch(flag){
case 1:{
struct node *n = creatNode();
if(NULL != n){
appendNode(n);
}
break;
}
case 2:{
struct node *n = creatNode();
if(NULL != n){
insertNode(n);
}
break;
}
case 3:{
deleteNode();
break;
}
case 4:{
updateNode();
break;
}
case 5:{
reverseList();
break;
}
case 6:{
showList();
break;
}
case 0:{
flagExit = 1;
break;
}
}
if(flagExit == 1) break;
}
return 0;
}
//創(chuàng)建一個(gè)空鏈表
void setupNullList(){
head = (struct node *)malloc(sizeof(struct node));
if(NULL == head){
printf("鏈表創(chuàng)建失敗!\n");
}else{
head->next = NULL;
head->data = 0;
}
}
//尾插鏈表
void insertTailList(){
struct node *tail = NULL;//尾結(jié)點(diǎn)
printf("\n請(qǐng)輸入要?jiǎng)?chuàng)建的結(jié)點(diǎn)id,輸入0結(jié)束\n\n");
int number = 0;
scanf("%d",&number);
while(number != 0){
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = number;
n->next = NULL;
if(head->next == NULL){//第一個(gè)結(jié)點(diǎn)
head->next = n;
}else{
tail->next = n;
}
tail = n;
scanf("%d",&number);
}
}
//頭插鏈表
void insertHeadList(){
printf("\n請(qǐng)輸入要?jiǎng)?chuàng)建的結(jié)點(diǎn)id,輸入0結(jié)束\n\n");
int number = 0;
scanf("%d",&number);
while(0 != number){
struct node *n = (struct node *)malloc(sizeof(struct node));
n->data = number;
n->next = head->next;
head->next = n;
scanf("%d",&number);
}
}
//創(chuàng)建結(jié)點(diǎn)
struct node *creatNode(){
struct node *n = (struct node *)malloc(sizeof(struct node));
printf("請(qǐng)輸入要?jiǎng)?chuàng)建的結(jié)點(diǎn)id\n\n");
int number = 0;
scanf("%d",&number);
if(NULL != n){
n->data = number;
n->next = NULL;
}
return n;
}
//在表尾追加結(jié)點(diǎn)
void appendNode(struct node *n){
struct node *p = head->next;
while(NULL != p->next){
p = p->next;
}
p->next = n;
}
//插入結(jié)點(diǎn)
void insertNode(struct node *n){
struct node *before = head;
struct node *p = head->next;
while(NULL != p){
if(listType == 1 && p->data < n->data){
before->next = n;
n->next = p;
break;
}else if(listType == 2 && p->data > n->data){
before->next = n;
n->next = p;
break;
}else{
break;
}
before = p;
p = p->next;
}
if(p == NULL){
before->next = n;
}
}
//刪除節(jié)點(diǎn)
void deleteNode(){
printf("\n請(qǐng)輸入要?jiǎng)h除的結(jié)點(diǎn)id\n\n");
int number = 0;
scanf("%d",&number);
struct node *before = head;
struct node *p = head->next;
while(NULL != p){
if(p->data == number){
before->next = p->next;
free(p);
break;
}
before = p;
p = p->next;
}
}
//更新結(jié)點(diǎn)
void updateNode(){
int oldNumber = 0;
int newNumber = 0;
printf("\n請(qǐng)輸入要修改的結(jié)點(diǎn)id\n\n");
scanf("%d",&oldNumber);
printf("\n請(qǐng)輸入新的結(jié)點(diǎn)id\n\n");
scanf("%d",&newNumber);
struct node *p = head->next;
while(NULL != p){
if(p->data == oldNumber){
p->data = newNumber;
}
p = p->next;
}
}
//鏈表逆序
void reverseList(){
struct node *p = head->next;
struct node *q = NULL;
head->next = NULL;
while(NULL != p){
q = p->next;
p->next = head->next;
head->next = p;
p = q;
}
}
//輸出鏈表
void showList(){
system("clear");
struct node *p = head->next;
while(NULL != p){
if(NULL != p->next){
printf("%d -> ",p->data);
}else{
printf("%d\n",p->data);
}
p = p->next;
}
}