// the program is used to practice Linked List
// 2014/10/28 completed by Min-Rong-Li
#include<stdio.h>
#include<stdlib.h>
typedef struct nodes{
int data;
struct nodes* next;
}node;
node* creat(int);
void add(node*);
void del(node*);
int main(void){
node* head = creat(0);
while(true){
printf("加入節點 (a ");
printf("刪除節點 (d ");
printf("離開程式 (e\n\n");
printf("目前link list上的值 :\n");
node* temp = head;
while(temp->next!=NULL){ //走訪陣列並且列印
temp=temp->next;
printf("%d ",temp->data);
}
printf("\n");
char option[10]={};
gets(option); //取得指令
if(option[1]){ //合法的指令不可能有兩個字元以上
system("CLS");
continue;
}
if(option[0]=='e') //結束程式
break;
switch(option[0]){
case 'a':{
add(head); //加入節點
break;
}
case 'd':{
del(head); //刪除節點
break;
}
default:{
system("CLS");
break;
}
}
}
printf("finished...\n");
system("pause");
return 0;
}
node* creat(int value){
node* n = (node*)malloc(sizeof(node));
n->data=value;
n->next=NULL;
return n;
}
void add(node* head){
node* pre = head;
while(pre->next!=NULL){
pre=pre->next;
}
printf("請輸入值\n");
int value;
char enter;
scanf("%d",&value);
node* n= creat(value);
pre->next=n;
system("CLS");
}
void del(node* head){
printf("輸入想刪除的值\n");
node* current=head;
if(head->next==NULL) //串列裡完全沒有資料節點
system("CLS");
else{
int delValue;
char enter;
scanf("%d",&delValue);
current=head;
node* temp;
bool finded=true;
while(current->data!=delValue){ //找尋有無符合的節點
temp=current;
current=current->next;
if(current==NULL){ //沒找到
finded=false;
break;
}
}
if(finded){ //有找到
temp->next=current->next;
free(current);
}
system("CLS");
}
}
2014-10-28
Linked List
訂閱:
張貼留言 (Atom)
(VM) Ubuntu enable ssh
OS版本:14.04 LTS 相關指令: sudo apt-get install openssh-server Port forwarding設定 : 以virtual box為例子,網路->進階->連接阜轉送(port forwarding) ...
-
https://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1849
沒有留言:
張貼留言