zoukankan      html  css  js  c++  java
  • 链表

      1 #include<stdio.h>
      2 #include<iostream>
      3 #include<algorithm>
      4 #include<string.h>
      5 #include<math.h>
      6 using namespace std;
      7 struct node{
      8 int data;
      9 struct node* next;
     10 };
     11 
     12 
     13 int ppp,yy;
     14 struct node* create(struct node*pos,int n){///建立一个链表
     15 struct node *p,*q;
     16 p=(struct node*)malloc(sizeof(struct node));
     17 int t;
     18 cin>>t;
     19 p->data=t;
     20 pos=p;
     21 for(int i=1;i<n;i++){
     22     q=(struct node*)malloc(sizeof(struct node));
     23     cin>>t;
     24     q->data=t;
     25     p->next=q;
     26     p=q;
     27 }
     28 
     29 p->next=NULL;
     30 return pos;
     31 }
     32 
     33 
     34 
     35 void print(struct node*pos,int n){///按顺序输出链表
     36 struct node*p=pos;//cout<<"******"<<endl;
     37 for(int i=0;i<n;i++)
     38 {
     39     cout<<p->data<<endl;
     40     p=p->next;
     41 }
     42 }
     43 
     44 
     45 
     46 void clearlist(struct node*pos,int n){///清空链表
     47 struct node *now,*pre;
     48 now=pos->next;
     49 for(int i=0;i<n;i++){
     50     struct node*q=now->next;
     51     free(now);
     52     now=q;
     53     pos=now;
     54 }
     55 pos->next=NULL;
     56 cout<<"链表已经清空"<<endl;
     57 
     58 }
     59 
     60 
     61 
     62 void find_list(int n,struct node*pos){///找到表中第n个元素的值
     63     if(n>ppp)
     64     {
     65         cout<<"Error
    ";
     66         return ;
     67     }
     68 struct node*p=pos;
     69 for(int i=1;i<n;i++){
     70     p=p->next;
     71 }
     72 printf("第%d个元素的值是
    ",n);
     73 cout<<p->data<<endl;
     74 }
     75 
     76 
     77 
     78 void postion(int n,struct node*pos,int len){///找到指定元素在链表中的位置
     79 struct node*p=pos;
     80 int t=1;
     81 while(p)
     82 {
     83     if(p->data!=n){
     84 
     85         t++;
     86          p=p->next;
     87     }
     88     else break;
     89 }
     90 if(t==len+1)cout<<"Error
    ";
     91 else printf("与%d值相同的元素在链表的第%d的位置
    ",n,t);
     92 }
     93 
     94 
     95 int length(struct node*pos)///查询链表的长度
     96 {
     97     struct node*p=pos;
     98     int t=0;
     99     while(p){
    100         t++;
    101         p=p->next;
    102     }
    103     printf("链表的长度是%d
    ",t);
    104     return t;
    105 }
    106 
    107 
    108 
    109 struct node* delete_list(int n,struct node*pos){///清空链表
    110 struct node*pre;
    111 if(n==1){
    112     pre=pos->next;
    113     free(pos);
    114     pos=pre;
    115 }
    116 else
    117 {pre=pos;
    118 for(int i=1;i<n-1;i++){
    119     pre=pre->next;
    120 }
    121 
    122 struct node*q=pre->next;
    123 struct node*last=pre->next->next;
    124 free(pre->next);
    125 pre->next=last;
    126 }
    127 printf("删除第%d位置的元素后的链表为
    ",n);
    128 struct node*pp;
    129 pp=pos;
    130 for(int i=0;i<ppp-1;i++){
    131     cout<<pp->data<<endl;
    132     pp=pp->next;
    133 }
    134 return pos;
    135 }
    136 
    137 
    138 
    139 struct node* add(int poss,struct node*pos,int n){///在固定位置上添加元素
    140 
    141 struct node*p;
    142 p=(struct node*)malloc(sizeof(struct node));
    143 p=pos;
    144 
    145 
    146 if(poss==1){
    147 struct node*q;
    148 q=(struct node*)malloc(sizeof(struct node));
    149 q->data=n;
    150 q->next=p;
    151 pos=q;
    152 }
    153 else {
    154     for(int i=1;i<poss-1;i++){
    155         p=p->next;
    156     }
    157     struct node*q=p->next;
    158      struct node*now=(struct node*)malloc(sizeof(struct node));
    159      now->data=n;
    160      p->next=now;
    161      now->next=q;
    162 }
    163 printf("在%d的位置上插入%d后的链表为
    ",poss,n);
    164 struct node*oo;
    165 oo=pos;
    166 while(oo){
    167     cout<<oo->data<<endl;
    168     oo=oo->next;
    169 
    170 }
    171 return pos;
    172 }
    173 int main()
    174 {
    175     //int n;
    176     struct node*head=NULL;
    177     cout<<"输入一个数n代表链表中元素的个数
    ";
    178     while(cin>>ppp){
    179         head=create(head,ppp);
    180         print(head,ppp);
    181         int t;
    182         cout<<"输入一个数字查询链表该位置的数的值
    ";
    183         cin>>t;
    184         find_list(t,head);
    185         int k;
    186         yy=length(head);
    187         cout<<"输入一个数字查询该数字在链表中的位置
    ";
    188         cin>>k;
    189         postion(k,head,yy);
    190         int ss;
    191         cout<<"输入一个数字,删除该位置上的数字
    ";
    192         cin>>ss;
    193         //printf("删除第%d位置上的数字后链表为
    ");
    194         head=delete_list(ss,head);
    195         int tt,u;
    196         cout<<"输入要插入的位置和插入数字的值";
    197         cin>>tt>>u;
    198         head=add(tt,head,u);
    199     }
    200 }

    正在学习中,以后继续补充哈

    你若盛开,清风自来...
  • 相关阅读:
    MFC Document/View 3
    MFC Document/View 2
    c++基本类型&&&c++string与c风格字符串的相互转化
    C++不同变量所在内存&&new malloc的区别
    C# 操作Excel 复选框
    prepareStatement与Statement的区别
    Oracle查询用户所有表、字段等信息
    同样的用户和密码,PL/SQL可以正常登录,C#程序却出现 ORA-1017 用户名/口令无效
    javascript原生态的同步异步请求实现
    SQL 数据库是否安全
  • 原文地址:https://www.cnblogs.com/shangjindexiaoqingnian/p/5974553.html
Copyright © 2011-2022 走看看