C语言——单链表(增删改查)

C语言——单链表(增删改查)

一·链表一

#include
#include
#include

typedef struct Test
{
    int data;
    struct  Test *next;
}Link;

Link *head=NULL;

Link* creatHead(Link* head);
void  AddLinkNode(Link* head,Link newnode);
void  printLinkNode(Link *head);
void  deleteLinkNode(Link* head,int data);
void  searchLink(Link* head,int data);
void  searchLink(Link* head,int data);
void  modifiLink(Link* head,int data,int newdata);

int main(int argc,char** argv)
{   
    int i=0;
    Link tmp={0};
    int data=0;
    head=creatHead(head);
    for(i=0;i<5;i++)
    {
        printf("输入第%d个节点的数据\n",i+1);
        scanf("%d",&tmp.data);
        AddLinkNode(head,tmp);
    }
    printLinkNode(head);
    printf("输入要删除的数据\n");
    scanf("%d",&data);
    deleteLinkNode(head,data);
    printLinkNode(head);
    searchLink(head,3);
    modifiLink(head,3,100);
    printLinkNode(head);
    return 0;
}

//创建节点
Link* creatHead(Link* head)
{
    if(head==NULL)
    {
        head=(Link*)malloc(sizeof(Link));
        head->next=NULL;
    }
    return head;
}

//增加节点(尾插法)
void AddLinkNode(Link* head,Link newnode)
{
    Link *p=head;
    Link *new=(Link*)malloc(sizeof(Link));
    while(p->next!=NULL)
    {
        p=p->next;
    }
    memcpy(new,&newnode,sizeof(Link));
    new->next=NULL;
    p->next=new;
}

//遍历链表
void printLinkNode(Link *head)
{
    int cnt=0;
    Link* p=head;
    while(p->next!=NULL)
    {
        cnt++;
        p=p->next;
        printf("%d ",p->data);
    }
    putchar('\n');
}

//删除节点
void deleteLinkNode(Link* head,int data)
{
    Link* p=head;
    Link* tmp=NULL;
    while(p->next!=NULL)
    {
        tmp=p;
        p=p->next;
        if(p->data==data)
        { 
            tmp->next=p->next;
            free(p);
            p=head;
        }
    }
}

//查找节点
void searchLink(Link* head,int data)
{
    Link* p=head;
    while(p->next!=NULL)
    {
        if(p->data==data)
        {
            printf("找到对应的节点:%d\n",p->data);
        }
        p=p->next;
    }
    if(p->next!=NULL)
    {
        printf("未找到对应的节点\n");
    }
}    

//修改节点
void modifiLink(Link* head,int data,int newdata)
{
    Link* p=head;
    while(p->next!=NULL)
    {
        if(p->data==data)
        {
            p->data=newdata;
            printf("把对应的节点修改为:%d\n",p->data);
        }
        p=p->next;
    }
    if(p->next!=NULL)
    {
        printf("未找到对应的节点\n");
    }

}

二·链表二

#include
#include
#include

typedef  struct Test  
{
    char name[32];
    int  number;
    struct Test *next;
}Link; 

Link* head=NULL;

Link*creatLinkNode(Link* head);
void AddLinkNode(Link* head,Link data);
void printLinkNode(Link* head);
void deleteLinkNode(Link* head,int num);

int main(int argc,char** argv)
{
    Link data ={"xie",100};
    Link data1={"wen",200};
    Link data2={"hui",300};
    head=creatLinkNode(head);
    AddLinkNode(head,data);
    AddLinkNode(head,data1);
    AddLinkNode(head,data2);
    printLinkNode(head);
    deleteLinkNode(head,200);
    printLinkNode(head);
    return 0;
}

//创建节点
Link* creatLinkNode(Link* head)
{
    if(head==NULL)
    {
        head=(Link*)malloc(sizeof(Link));
        head->next=NULL;
    }
    return   head; 
}

//增加节点(尾插法)
void AddLinkNode(Link* head,Link data)
{
    Link* p=head;
    Link* new=(Link*)malloc(sizeof(Link));
    while(p->next!=NULL)
    {
        p=p->next;
    }
    memcpy(new,&data,sizeof(Link));
    p->next=new;
    new->next=NULL;
}

//遍历链表
void printLinkNode(Link* head)
{
    Link* p=head->next;
    while(p!=NULL)
    {
        printf("%s,%d\n",p->name,p->number);
        p=p->next;
    }
    // putchar('\n');
}

//删除节点
void deleteLinkNode(Link* head,int num)
{
    Link* p=head;
    Link* tmp=NULL;
    while(p->next!=NULL)
    {
        tmp=p;
        p=p->next;
        if(p->number==num)
        {
            tmp->next=p->next;
            free(p);
            p=head;
        }
    }
}

三·链表三

#include
#include
#include

typedef struct Test 
{
    char  name[32];
    int number;
    struct Test *next;
}Link;

Link*  head=NULL;
Link* creatLink(Link* head);
void  printLink(Link* head);
void  AddLink(Link* head,Link data);
void  deleteLink(Link* head,int num);
void  saveLink(Link* head);
void  getLink(Link* head);


#if 1
int main(int argc,char** argv)
{
    Link data={"xie",100};
    Link data1={"wen",200};
    Link data2={"hui",300};
    head=creatLink(head);
    AddLink(head,data);
    AddLink(head,data1);
    AddLink(head,data2);
    printLink(head);
    deleteLink(head,100);
    printLink(head);
    saveLink(head);
    getLink(head);
    printLink(head);
    return 0;
}
#endif

#if 0
int main(int argc,char**  argv)
{
    head=creatLink(head);
    getLink(head);
    printLink(head);
    return  0;
}
#endif

Link* creatLink(Link* head)
{
    if(head==NULL)
    {
        head=(Link*)malloc(sizeof(Link));
        head->next=NULL;
    }
    return head;
}

void printLink(Link* head)
{
    /*Link* p=head->next;
                  while(p!=NULL)
                  {
                      printf("%s,%d\n",p->name,p->number);
                      p=p->next;
                  }*/
    Link* p=head->next;
    while(p!=NULL)
    {
        printf("%s,%d\n",p->name,p->number);
        p=p->next;
    }
}

void AddLink(Link* head,Link data)
{ 
    Link* p=head;
    Link* new=(Link*)malloc(sizeof(Link));
    while(p->next!=NULL)
    {
        p=p->next;
    }
    memcpy(new,&data,sizeof(Link));
    p->next=new;
    new->next=NULL;
}

void deleteLink(Link* head,int num)
{
    Link* p=head;
    Link* tmp=NULL;
    while(p->next!=NULL)
    {
        tmp=p;
        p=p->next;
        if(p->number==num)
        {
            tmp->next=p->next;
            free(p);
            p=head;
        }
    }
}

void saveLink(Link* head)
{
    Link* p=head;
    FILE *fp=NULL;
    fp=fopen("./text.txt","w+");
    if(fp==NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    while(p->next!=NULL)
    {
        fwrite(p,1,sizeof(Link),fp);
        p=p->next;
    }
    fclose(fp);
}

/*
            void getLink(Link* head)
            {
                 Link* p=head;
                 Link* new=(Link*)malloc(sizeof(Link));
                 FILE* fp=NULL;
                 Link  data;
                 fp=fopen("./text.txt","rb");
                 if(fp==NULL)
                 {
                    printf("文件打开失败\n");
                    return;
                 }
                 while(!feof(fp))
                 {
                       fread(&data,sizeof(Link),1,fp);
                       AddLink(p,data);
                 }
                 fclose(fp);
            }
            */
/*
            void  getLink(Link* head)
            {
                FILE *fp=NULL;
                Link* p=head;
                Link* new=(Link*)malloc(sizeof(Link));
                new->next=NULL;
                fp=fopen("./text.txt","r+");
                {
                      printf("打开文件失败\n");
                      return ;
                }
                while((fread(new,sizeof(Link),1,fp))==1)
                {
                      p->next=new;
                      p=new;
                      new=(Link*)malloc(sizeof(Link));
                      new->next=NULL;
                }
                free(new);
                fclose(fp);
            }
            */

void getLink(Link* head)
{
    Link* p=head;
    FILE* fp=NULL;
    Link data={0};
    fp=fopen("./text.txt","r+");
    if(fp==NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    while(!feof(fp))
    {
        fread(&data,1,sizeof(Link),fp);
        AddLink(head,data);
    }
    fclose(fp);
}

四·链表四

#include
#include

typedef struct ListNode
{
    int data;
    struct ListNode* next;
}ListNode;

//创建节点
ListNode* createNode(int data)
{
    ListNode *node=(ListNode*)malloc(sizeof(ListNode));
    node->data=data;
    node->next=NULL;
    return node;
}

//头插法
ListNode* insertNodeHead(ListNode *head,int data)
{
    ListNode *node=createNode(data);
    node->next=head;
    return node;
}

//尾插法
ListNode *insertNodeTail(ListNode *head,int data)
{
    ListNode* node=createNode(data);
    ListNode* point=head;
    if(head==NULL)
    {
        return NULL;
    }
    while(point->next!=NULL)
    {
        point=point->next;
    }
    point->next=node;
    return head;
}

//删除节点
ListNode* deleteNode(ListNode* head,int data)
{
    ListNode* point=head;
    if(head==NULL)
    {
        return NULL;
    }
    if(head->data==data)
    {
        head=head->next;
        free(point);
        return head;
    }
    while(point->next!=NULL&&point->next->data!=data)
    {
        point=point->next;
    }
    if(point->next!=NULL)
    {
        ListNode* node=point->next;
        point->next=node->next;
        free(node);  
    }
    return head;
}

//修改节点
void updateNode(ListNode* head,int data,int newdata)
{
    ListNode* point=head;
    while(point!=NULL)
    {
        if(point->data==data)
        {
            point->data=newdata;
        }
        point=point->next;
    }
}

//遍历节点
void printNode(ListNode* head)
{
    ListNode* point=head;
    while(point!=NULL)
    {
        printf("%d ",point->data);
        point=point->next;
    }
    putchar('\n');
}

//清楚节点
void clearNode(ListNode* head)
{

    while(head!=NULL)
    {
        ListNode* point=head;
        head=head->next;
        free(point);
    }
}

int main(int argc,char** argv)
{
    ListNode* head=NULL;
    head=insertNodeHead(head,1);
    head=insertNodeHead(head,2);
    head=insertNodeTail(head,3);
    printNode(head); 
    head=deleteNode(head,2);
    printNode(head);
    updateNode(head,1,4);
    printNode(head);
    clearNode(head);
    return 0;
}

你可能感兴趣的:(c语言,网络,算法)