248 lines
4.8 KiB
C
248 lines
4.8 KiB
C
|
#include<stdio.h>
|
|||
|
#include<stdlib.h>
|
|||
|
#include<conio.h>
|
|||
|
#define MAX 100
|
|||
|
typedef int DataType;
|
|||
|
typedef struct node{
|
|||
|
DataType data;
|
|||
|
struct node *next;
|
|||
|
}Node;
|
|||
|
void CreateListHead(Node *L, int n){
|
|||
|
Node *p;
|
|||
|
L->next = NULL;
|
|||
|
for(int i = 0; i < n; i++){
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> p
|
|||
|
p = (Node *)malloc(sizeof(Node));
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><>Ԫ<EFBFBD>أ<EFBFBD>",i+1);
|
|||
|
scanf("%d",&(p->data));
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
p->next = L->next;
|
|||
|
L->next = p;
|
|||
|
}
|
|||
|
};
|
|||
|
void CreateListEnd(Node *L, int n){
|
|||
|
Node *p,*end=L;
|
|||
|
for(int i=0;i<n;i++){
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD> p
|
|||
|
p = (Node *)malloc(sizeof(Node));
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><>Ԫ<EFBFBD>أ<EFBFBD>",i+1);
|
|||
|
scanf("%d",&(p->data));
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
end->next = p;
|
|||
|
end = p;
|
|||
|
}
|
|||
|
end->next = NULL;
|
|||
|
};
|
|||
|
void insertList(Node *L, int n, DataType e){
|
|||
|
Node *p=L;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>λ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (int i=1; i<n; i++) {
|
|||
|
if (p==NULL) {
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч\n");
|
|||
|
return;
|
|||
|
}
|
|||
|
p=p->next;
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> new
|
|||
|
Node *new=(Node*)malloc(sizeof(Node));
|
|||
|
new->data=e;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
new->next=p->next;
|
|||
|
p->next=new;
|
|||
|
};
|
|||
|
void EditList(Node *L, int n){
|
|||
|
Node *p=L->next;
|
|||
|
DataType e;
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ҫ<EFBFBD>ĵ<DEB8>λ<EFBFBD><CEBB>
|
|||
|
for (int i=1; i<n; i++) {
|
|||
|
if (p==NULL) {
|
|||
|
printf("λ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч\n");
|
|||
|
return;
|
|||
|
}
|
|||
|
p=p->next;
|
|||
|
}
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>");
|
|||
|
scanf("%d",&e);
|
|||
|
p->data=e;
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>\n");
|
|||
|
}
|
|||
|
int FindList(Node *L, DataType e){
|
|||
|
Node *p = L->next;
|
|||
|
int i=0;
|
|||
|
while(p != NULL){
|
|||
|
if(p->data == e){
|
|||
|
return i;
|
|||
|
}
|
|||
|
i++;
|
|||
|
p = p->next;
|
|||
|
}
|
|||
|
return -1;
|
|||
|
};
|
|||
|
int DeleteList1(Node *L, DataType e){
|
|||
|
Node *p = L->next, *last = L;
|
|||
|
while(p != NULL){
|
|||
|
if(p->data == e){
|
|||
|
// ɾ<><C9BE>
|
|||
|
last->next = p->next;
|
|||
|
// <20>ͷſռ<C5BF>
|
|||
|
free(p);
|
|||
|
return 1;
|
|||
|
}
|
|||
|
last = last->next;
|
|||
|
p = p->next;
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
int DeleteList2(Node *L, int n){
|
|||
|
Node *p = L->next, *last = L;
|
|||
|
int i=0;
|
|||
|
while(p != NULL){
|
|||
|
if(i == n){
|
|||
|
// ɾ<><C9BE>
|
|||
|
last->next = p->next;
|
|||
|
// <20>ͷſռ<C5BF>
|
|||
|
free(p);
|
|||
|
return 1;
|
|||
|
}
|
|||
|
last = last->next;
|
|||
|
p = p->next;
|
|||
|
i++;
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
void DeleteList3(Node *L, DataType start, DataType end){
|
|||
|
// ȷ<><C8B7> start <= end
|
|||
|
int temp;
|
|||
|
if(start>end){
|
|||
|
temp=end;
|
|||
|
end=start;
|
|||
|
start=temp;
|
|||
|
}
|
|||
|
Node *p = L->next, *last = L, *temp_;
|
|||
|
while(p != NULL){
|
|||
|
if(p->data < start){
|
|||
|
last = last->next;
|
|||
|
p = p->next;
|
|||
|
}
|
|||
|
if(p->data >= start || p->data <= end){
|
|||
|
last->next = NULL;
|
|||
|
// <20>ͷſռ<C5BF>
|
|||
|
temp_=p;
|
|||
|
p = p->next;
|
|||
|
free(temp_);
|
|||
|
}
|
|||
|
if(p->data > end){
|
|||
|
// <20>ı<EFBFBD>ָ<EFBFBD><D6B8>
|
|||
|
last->next = p;
|
|||
|
p = p->next;
|
|||
|
}
|
|||
|
}
|
|||
|
};
|
|||
|
void OutputList(Node *L){
|
|||
|
Node *p = L->next;
|
|||
|
while(p != NULL){
|
|||
|
printf("%d\n", p->data);
|
|||
|
p = p->next;
|
|||
|
}
|
|||
|
};
|
|||
|
int main(){
|
|||
|
Node *L1;
|
|||
|
L1 = (Node *)malloc(sizeof(Node));
|
|||
|
int n,choice,x,y;
|
|||
|
DataType e;
|
|||
|
|
|||
|
for(;;){
|
|||
|
printf("\t\t1.ͷ<>巨<EFBFBD><E5B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
printf("\t\t2.β<>巨<EFBFBD><E5B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
printf("\t\t3.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>Ԫ<EFBFBD><D4AA>\n");
|
|||
|
printf("\t\t4.<2E><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>\n");
|
|||
|
printf("\t\t5.ɾ<><C9BE>ָ<EFBFBD><D6B8>ֵ<EFBFBD><D6B5>Ԫ<EFBFBD><D4AA>\n");
|
|||
|
printf("\t\t6.ɾ<><C9BE>ָ<EFBFBD><D6B8>λ<EFBFBD>õ<EFBFBD>Ԫ<EFBFBD><D4AA>\n");
|
|||
|
printf("\t\t7.ɾ<><C9BE>λ<EFBFBD><CEBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>\n");
|
|||
|
printf("\t\t8.<2E><EFBFBD>ָ<EFBFBD><D6B8>λ<EFBFBD>õ<EFBFBD>Ԫ<EFBFBD><D4AA>\n");
|
|||
|
printf("\t\t9.<2E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD>\n");
|
|||
|
printf("\t\t0.<2E>˳<EFBFBD>\n");
|
|||
|
|
|||
|
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
scanf("%d",&choice);
|
|||
|
if(choice==0) break;
|
|||
|
switch (choice) {
|
|||
|
case 1:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>");
|
|||
|
scanf("%d",&n);
|
|||
|
CreateListHead(L1, n);
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 2:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD>");
|
|||
|
scanf("%d",&n);
|
|||
|
CreateListEnd(L1, n);
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 3:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD>ã<EFBFBD>");
|
|||
|
scanf("%d",&n);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>");
|
|||
|
scanf("%d",&e);
|
|||
|
insertList(L1,n,e);
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 4:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>");
|
|||
|
scanf("%d",&e);
|
|||
|
n=FindList(L1,e);
|
|||
|
if(n>-1){
|
|||
|
printf("<EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d\n",n);
|
|||
|
} else {
|
|||
|
printf("δ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>\n");
|
|||
|
}
|
|||
|
break;
|
|||
|
case 5:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>ֵ<EFBFBD><EFBFBD>");
|
|||
|
scanf("%d",&e);
|
|||
|
n=DeleteList1(L1,e);
|
|||
|
if(n==0){
|
|||
|
printf("ɾ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>Ԫ<EFBFBD>ز<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڡ<EFBFBD>\n");
|
|||
|
} else {
|
|||
|
printf("ɾ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
|
|||
|
}
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 6:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>λ<EFBFBD>ã<EFBFBD>");
|
|||
|
scanf("%d",&n);
|
|||
|
n=DeleteList2(L1,n-1);
|
|||
|
if(n==0){
|
|||
|
printf("ɾ<EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>λ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڡ<EFBFBD>\n");
|
|||
|
} else {
|
|||
|
printf("ɾ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
|
|||
|
}
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 7:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> x y<><79>");
|
|||
|
scanf("%d %d",&x,&y);
|
|||
|
DeleteList3(L1,x,y);
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>\n");
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 8:
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>ĵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>λ<EFBFBD>ã<EFBFBD>");
|
|||
|
scanf("%d",&n);
|
|||
|
EditList(L1,n);
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
case 9:
|
|||
|
OutputList(L1);
|
|||
|
break;
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
printf("<EFBFBD>밴<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
|
|||
|
getch();
|
|||
|
system("cls");
|
|||
|
}
|
|||
|
printf("<EFBFBD><EFBFBD>лʹ<EFBFBD>ã<EFBFBD><EFBFBD>ټ<EFBFBD>\n");
|
|||
|
return 0;
|
|||
|
};
|