zoukankan      html  css  js  c++  java
  • 链表的基本操作

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #define mem(x,y) memset(x,y,sizeof(x))
    using namespace std;
    typedef long long LL;
    struct Node{
    	int date;
    	struct Node *next;
    };
    Node *Creatlist(Node *head,int n){
    	head->next=NULL;
    	for(int i=0;i<n;i++){
    		Node *p;
    		p=(Node*)malloc(sizeof(Node));
    		scanf("%d",&p->date);
    		p->next=head->next;
    		head->next=p;
    	}
    	return head;
    } 
    Node *Insertlist(Node *head,int pos,int val){
    	Node *p,*q;
    	p=head;
    	for(int i=1;i<pos;i++){
    		p=p->next;
    	}
    	q=(Node *)malloc(sizeof(Node));
    	q->date=val;
    	q->next=p->next;
    	p->next=q;
    	return head;
    }
    Node *Dellist(Node *head,int pos){
    	Node *p,*pl;
    	p=head;
    	for(int i=1;i<pos;i++)p=p->next;
    	pl=p->next;
    	p->next=pl->next;
    	free(pl);
    	return head;
    }
    void Display(Node *head){
    	Node *p;
    	p=head->next;
    	while(p!=NULL){
    		printf("%d ",p->date);
    		p=p->next;
    	}
    	puts("");
    }
    int main(){
    	int n;
    	Node *head;
    	while(~scanf("%d",&n)){
    		head=(Node*)malloc(sizeof(head));
    		head->next=NULL;
    		head=Creatlist(head,n);
    		Display(head);
    		int pos,val;
    		puts("添加元素");
    		scanf("%d%d",&pos,&val);
    		head=Insertlist(head,pos,val);
    		Display(head);
    		puts("删除元素");
    		scanf("%d",&pos);
    		head=Dellist(head,pos);
    		Display(head);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    poj 3613(经过N条边的最短路)
    poj 3328(多起点多终点的最短路)
    poj 3311(floyd+状态压缩)
    新CCIE笔记-IP网络基础
    新CCIE笔记-IP网络基础
    算法之【冒泡排序法】
    算法之【冒泡排序法】
    算法之【冒泡排序法】
    算法之【辗转相除法】
    算法之【辗转相除法】
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4975894.html
Copyright © 2011-2022 走看看