【数据结构】链表

#include<bits/stdc++.h>
using namespace std;
struct edge{
    int v;
    edge *next;
}; 
int main(){
    //创建 
    edge *head = (edge*)malloc(sizeof(edge));
    head->next = NULL;
    edge *now = head;
    for(int i=1;i<=5;i++){
        edge *p = (edge*)malloc(sizeof(edge));
        p->v = i;
        p->next = NULL;
        now->next = p;
        now = now->next;
    }
    
    //插入 
    now = head;
    while(now->next){
        now = now->next;
        if(now->v == 3) {
            edge *new_p = (edge*)malloc(sizeof(edge)); 
            new_p->v = 6;
            new_p->next = now->next;
            now->next = new_p;
            break;
        }
    }
    
    //删除 
    now = head;
    while(now->next){
        now = now->next;
        edge *next_p = now->next;
        if(next_p->v == 3){
            now->next = next_p->next;
            free(next_p);
            break;
        }
    }
    
    //打印 
    while(head->next){
        head = head->next;
        if(head->next) printf("%d->",head->v);
        else printf("%d",head->v);
    }
    return 0;
}
View Code

 

posted @ 2021-12-04 10:09  Oranges  阅读(97)  评论(0编辑  收藏  举报