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;
    }
    

      

  • 相关阅读:
    Spring Boot第四弹,一文教你如何无感知切换日志框架?
    Spring Boot 第三弹,一文带你了解日志如何配置?
    UVa 1625
    UVa 11584
    UVa 11400
    UVa 12563
    UVa 116
    UVa 1347
    UVa 437
    UVa 1025
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4975894.html
Copyright © 2011-2022 走看看