数据结构作业

1 #include "link.h"
  2 
  3 int main(int argc, char* argv[])
  4 {
  5 
  6         int a[]={1,2,5,4,6,9,8,7,5};
  7         //1.创建单链表
  8         Plink L=create();
  9 
 10         //1.1头插法
 11         /*for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){
 12                 front_insert(L,a[i]);
 13         }*/
 14 
 15 
 16         //2、遍历单链表
 17         //printf("%d\n",L->len);
 18 
 19 
 20         //1.2尾插法
 21         for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){
 22                 rear_insert(L,a[i]);
 23         }
 24 
 25         output_link(L);
 26 
 27         //3、任意位置插入一个节点
 28         anypos_insert(L,12,2);
 29 
 30         output_link(L);
 31         //4、任意位置删除一个节点
 32         anypos_dele(L,3);
 33 
 34         output_link(L);
 35 
 36         //5.任意位置查找一个节点
 37         ss(L,6);
 38 
 39         //任意位置修改一个节点
 40         uu(L,3,888);
 41         output_link(L);
 42 
 43         return 0;
 44 }
~                                                                     
~                                             
  1 #include"link.h"
  2 Plink create(){
  3         Plink p=malloc(sizeof(link));
  4         if(NULL==p){
  5                 return NULL;
  6         }
  7 
  8         p->len=0;
  9         p->next=NULL;
 10         return p;
 11 }
 12 
 13 
 14 int front_insert(Plink p,int a){
 15         if(p==NULL){
 16                 printf("单链表不存在,创建失败\n");
 17                 return -1;
 18         }
 19         Plink n=malloc(sizeof(link));
 20         n->data=a;
 21         n->next=p->next;
 22         p->next=n;
 23         p->len++;
 24         return 0;
 25 
 26 }
 27 
 28 
 29 int output_link(Plink L){
 30         Plink t=L->next;
 31         while(t!=NULL){
 32                 printf("%d ",t->data);
 33                 t=t->next;
 34         }
 35         printf("\n");
 36 
 37         return 0;
 38 }
 39 
 40 int rear_insert(Plink L,int a){
 41         if(L==NULL){
 42                 printf("链表不存咋,插入失败\n");
 43                 return -1;
 44         }
 45 
 46         Plink n = malloc(sizeof(link));
 47         n->data=a;
 48         n->next=NULL;
 49         Plink t=L;
 50         while(t->next!=NULL){
 51                 t=t->next;
 52         }
 53 
 54         t->next=n;
 55         L->len++;
 56         return 0;
 57 
 58 
 59 
 60 }
 61 
 62 
 63 int anypos_insert(Plink L,int a,int n){
 64         if(n<1||n>L->len+1||L==NULL){
 65                 printf("插入失败\n");
 66                 return -1;
 67         }
 68         Plink t=L;
 69         for(int i=0;i<n-1;i++){
 70                 t=t->next;
 71         }
 72 
 73         Plink n1 = malloc(sizeof(link));
 74         n1->data=a;
 75         n1->next=t->next;
 76         t->next=n1;
 77         L->len++;
 78         return 0;
 79 
 80 }
 81 
 82 int anypos_dele(Plink L,int pos){
 83         if(pos<1||pos>L->len||L==NULL){
 84                 printf("删除失败\n");
 85                 return -1;
 86         }
 87         Plink t=L;
 88         for(int i=0;i<pos-1;i++){
 89                 t=t->next;
 90         }
 91 
 92         Plink p=t->next;
 93 
 94         t->next=t->next->next;
 95         free(p);
 96         p=NULL;
 97         L->len--;
 98         return 0;
 99 
100 }
101 
102 
103 int ss(Plink L,int pos){
104         if(pos<1||pos>L->len||L==NULL){
105                 printf("查找节点\n");
106                 return -1;
107         }
108 
109         Plink t=L;
110         for(int i=0;i<pos;i++){
111                 t=t->next;
112         }
113         printf("查找成功,查找结果为:%d\n",t->data);
114         return 0;
115 }
116 
117 
118 int uu(Plink L,int pos,int value){
119         if(pos<1||pos>L->len||NULL==L){
120                 printf("修改失败\n");
121                 return -1;
122         }
123 
124         Plink t=L;
125         for(int i=0;i<pos;i++){
126                 t=t->next;
127         }
128 
129         t->data=value;
130 
131         return 0;
132 
133 }
134 
~                                                                                                                                                                                                                                                                                                                                                                                                         
~                                                                                                                                                                                                                                                                                                                                                                                                         
~                                
1 #ifndef _LINK_
  2 #define _LINK_
  3 
  4 #include<stdlib.h>
  5 #include<stdio.h>
  6 typedef struct link
  7 {
  8         union{
  9                 int data;
 10                 int len;
 11         };
 12         struct link *next;
 13 
 14 } link,*Plink;
 15 
 16 Plink create();
 17 
 18 int front_insert(Plink p,int a);
 19 
 20 int output_link(Plink L);
 21 
 22 int rear_insert(Plink L,int a);
 23 
 24 int anypos_insert(Plink L,int a,int n);
 25 
 26 int anypos_dele(Plink L,int pos);
 27 
 28 int ss(Plink L,int pos);
 29 
 30 int uu(Plink L,int pos,int value);
 31 #endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值