#include<stdio.h>
#include<time.h>
#include<stdlib.h>
const int maxn = 1e5 + 10;
typedef struct Node{
int Data;
struct Node* Next;
}List;
List *buildList_tail(int Size){
List *Head,*Now,*tmp;
Head=(List*)malloc(sizeof(List));
Head->Data=Size;
Head->Next=NULL;
Now=Head;
for(int i=0;i<Size;i++){
tmp=(List*)malloc(sizeof(List));
scanf("%d",&tmp->Data);
tmp->Next=NULL;
Now->Next=tmp;
Now=Now->Next;
}
return Head;
}
List *buildList_head(int Size){
List *Head,*Tmp;
Head=(List*)malloc(sizeof(List));
Head->Next=NULL;
Head->Data=Size;
for(int i=0;i<Size;i++){
Tmp=(List*)malloc(sizeof(List));
scanf("%d",&Tmp->Data);
Tmp->Next=NULL;
if(Head->Next==NULL)Head->Next=Tmp;
else{
Tmp->Next=Head->Next;
Head->Next=Tmp;
}
}
return Head;
}
void printList(List* head){
List *tmp=head->Next;
while(tmp!=NULL){
printf("%d ",tmp->Data);
tmp=tmp->Next;
}
printf("\n");
}
void add_item_into_List(List* head,int index,int num){
if(index>head->Data||index<=0){
printf("Error\n");
return;
}
List *p=head;
List *tmp;
for(int i=0;i<index;i++){
p=p->Next;
}
tmp=(List*)malloc(sizeof(List));
tmp->Data=num;
tmp->Next=p->Next;
p->Next=tmp;
}
List* find_item(List* head,int num){
int flag=0;
List* tmp=head->Next;
if(tmp->Data==num)return head;
while(tmp->Next!=NULL){
if(tmp->Next->Data==num){
flag=1;
return tmp;
}
tmp=tmp->Next;
}
if(!flag){
return NULL;
}
}
void delete_item_from_List(List* head,int num){
List* tmp=find_item(head,num);
if(tmp==NULL){
printf("There is no number equal to %d\n",num);
return;
}
List* tmpex=tmp->Next;
tmp->Next=tmpex->Next;
free(tmpex);
}
void EX_delete_item_from_List(List* head,int index){
if(index>head->Data||index<=0){
printf("Error\n");
return;
}
List* p=head,*tmp;
for(int i=1;i<index;i++){
p=p->Next;
}
tmp=p->Next;
p->Next=tmp->Next;
free(tmp);
}
void modif_item_from_List(List* head,int num,int x){
List *tmp=find_item(head,num);
if(tmp==NULL){
printf("There is no number equal to %d\n",num);
return;
}else{
tmp->Next->Data=x;
}
}
void EX_modif_item_from_List(List* head,int index,int x){
List *p=head->Next;
if(index>head->Data||index<=0){
printf("Error\n");
return;
}else{
for(int i=1;i<index;i++){
p=p->Next;
}
p->Data=x;
}
}
int main(){
int n;
scanf("%d",&n);
List *listHead=buildList_tail(n);
List *tmp;
EX_modif_item_from_List(listHead,3,666);
printList(listHead);
modif_item_from_List(listHead,3,666);
printList(listHead);
EX_delete_item_from_List(listHead,3);
printList(listHead);
delete_item_from_List(listHead,5);
printList(listHead);
tmp=find_item(listHead,5);
if(tmp!=NULL){
printf("the pre pointer data is %d\n",tmp->Data);
}else{
printf("Not Find!\n");
}
add_item_into_List(listHead,2,666);
printList(listHead);
while(listHead!=NULL){
tmp=listHead->Next;
free(listHead);
listHead=tmp;
}
system("pause");
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-932.html