/*
299418045 王國銘
功能說明 : 可新增刪除節點
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *front;
void insert(int key);
void show(void);
void del(int key);
void show(){
struct node *this_node;
if(front->next != NULL){
this_node = front->next;
printf(" front -> ");
while(this_node != NULL){
printf("%d -> ",this_node->data);
this_node = this_node->next;
}
printf("NULL \n");
}else{
printf(" ERROR : link list中沒有資料可以顯示請按 1 輸入資料 \n");
}
}
void del(int key){
struct node *this_node , *tmp_node;
this_node = front;
while(this_node->next != NULL){
if(this_node->next->data == key){
tmp_node = this_node->next;
if(tmp_node->next != NULL){
//找到欲刪除節點
this_node->next = tmp_node->next;
}else{
//找到欲刪除節點 並在末端
this_node->next = NULL;
}
free(tmp_node);
return;
}
this_node = this_node->next;
}
printf(" ERROR : 找不到指定資料!\n");
}
void insert(int key){
struct node *new_node;
new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = key;
new_node->next = NULL;
if(front->next == NULL){
//新增第一筆資料
front->next = new_node;
}else{
//插入資料
struct node *this_node;
this_node = front;
while(this_node->next != NULL){
if(this_node->next->data > key){
//若下一筆 data 比 key 大 就安插在下一筆與這一筆之前
new_node->next = this_node->next;
this_node->next = new_node;
return;
}
this_node = this_node->next;
}
this_node->next = new_node;
}
}
int main(){
front = (struct node *)malloc(sizeof(struct node));
front->next = NULL;
int key = 0;
while(key != 4){
printf("1. 新增資料 \n2. 刪除資料 \n3. SHOW出現有link list \n4. 離開程式\n");
printf("請輸入指令 : ");
scanf("%d",&key);
int n = 0;
switch(key){
case 1://1. 新增資料
printf(" 請輸入一整數資料 = ");
scanf("%d",&n);
insert(n);
show();
break;
case 2://2. 刪除資料
printf(" 請輸入欲的刪除資料 = ");
scanf("%d",&n);
del(n);
show();
break;
case 3://3. SHOW出現有link list
show();
break;
case 4://4. 離開程式\n
exit(0);
break;
default:
break;
}
key = 0;
printf("*************************************************************\n");
}
}
/*
功能說明 : 可新增刪除節點
test01.cpp
*/
#include <iostream>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *front;
void insert(int key);
void show(void);
void del(int key);
void show(){
struct node *this_node;
if(front->next != NULL){
this_node = front->next;
while(this_node != NULL){
cout << this_node->data <<" -> ";
this_node = this_node->next;
}
cout << "NULL \n";
}else{
cout <<"沒有資料 \n";
}
}
void del(int key){
struct node *this_node , *tmp_node;
this_node = front;
while(this_node->next != NULL){
if(this_node->next->data == key){
tmp_node = this_node->next;
if(tmp_node->next != NULL){
//找到欲刪除節點
this_node->next = tmp_node->next;
}else{
//找到欲刪除節點 並在末端
this_node->next = NULL;
}
free(tmp_node);
return;
}
this_node = this_node->next;
}
cout <<" ERROR : 找不到指定資料!\n";
}
void insert(int key){
struct node *new_node;
new_node = (struct node *) malloc(sizeof(struct node));
new_node->data = key;
new_node->next = NULL;
if(front->next == NULL){
//新增第一筆資料
front->next = new_node;
}else{
//插入資料
struct node *this_node;
this_node = front;
while(this_node->next != NULL){
if(this_node->next->data > key){
//若下一筆 data 比 key 大 就安插在下一筆與這一筆之前
new_node->next = this_node->next;
this_node->next = new_node;
return;
}
this_node = this_node->next;
}
this_node->next = new_node;
}
}
int main(){
front = (struct node *)malloc(sizeof(struct node));
while(true){
char key;
cout << "請輸入資料或輸入d刪除資料 (輸入q離開程式\n ?";
cin >> key;
switch(key){
case 'd':
cout << "--請輸入欲刪除之資料\n ?";
cin >> key;
del(key-48);
show();
break;
case 'q':
exit(0);
break;
default:
insert(key-48);
show();
}
}
}
沒有留言:
張貼留言