C语言单链表实现增删改查操作(详细版本)

    科技2022-07-10  127

    /** * @author: HCHO; * @lastUpDateTime: 2020/10/2 * 单链表的增删改查 */ /* 以下代码链表的头指针head都是为了方便操作而添加的空头指针; head的下标为0; 若在第i个节点后插入数据即往下标为i的节点后插入 */ #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;//this head is a special pointer 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;//Now=tmp } return Head; } List *buildList_head(int Size){//头插法 List *Head,*Tmp;//this head is a special pointer 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){//往第index个节点后插入num if(index>head->Data||index<=0){ printf("Error\n"); return; } List *p=head; List *tmp; for(int i=0;i<index;i++){//p指向的是index前一个节点 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;//如果是0说明链表中没有num这个数 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; }
    Processed: 0.047, SQL: 8