解题思路:
首先思考一下总思路
①:结点定义
typedef struct node_{ int data; struct node_* next; }*node,Node;
②:声明所需的函数
node creat_list(int *Count); void get_(node L,int a); void insert_(node L,int a,int e,int *Count); void delete_(node L,int a,int *Count); void show_(node L);
③:编写主函数总体框架
int main() { char order[7]; int n,a,e,Count; /*Count表示结点数*/ node L=creat_list(&Count); scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",order); if(!strcmp(order,"get")) { scanf("%d",&a); /*判断查找位置是否合法,*/ if(a>Count||a<1) printf("get fail\n"); else get_(L,a); } else if(!strcmp(order,"insert")) { scanf("%d%d",&a,&e); /*判断插入位置是否合法,若a=Count+1,表示插入在链表尾部*/ if(a>Count+1||a<1) printf("insert fail\n"); else insert_(L,a,e,&Count); } else if(!strcmp(order,"delete")) { scanf("%d",&a); /*判断删除位置是否合法*/ if(a>Count||a<1) printf("delete fail\n"); else delete_(L,a,&Count); } else if(!strcmp(order,"show")) { show_(L); } } return 0; }
④:对前面声明的函数写出具体的实现
注意事项:
删除是删除第a位置的元素,插入是在第a位置之前
每个输出占一行,数字后面带空格
参考代码:
#include <stdio.h> #include <malloc.h> #include <string.h> typedef struct node_ { int data; struct node_ * next; }*node, Node; node creat_list( int *Count ); void get_( node L, int a ); void insert_( node L, int a, int e, int *Count ); void delete_( node L, int a, int *Count ); void show_( node L ); int main() { char order[7]; int n, a, e, Count; node L = creat_list( &Count ); scanf( "%d", &n ); for ( int i = 0; i < n; i++ ) { scanf( "%s", order ); if ( !strcmp( order, "get" ) ) { scanf( "%d", &a ); /*判断查找位置是否合法,*/ if ( a > Count || a < 1 ) printf( "get fail\n" ); else get_( L, a ); }else if ( !strcmp( order, "insert" ) ) { scanf( "%d%d", &a, &e ); /*判断插入位置是否合法,若a=Count+1,表示插入在链表尾部*/ if ( a > Count + 1 || a < 1 ) printf( "insert fail\n" ); else insert_( L, a, e, &Count ); }else if ( !strcmp( order, "delete" ) ) { scanf( "%d", &a ); /*判断删除位置是否合法*/ if ( a > Count || a < 1 ) printf( "delete fail\n" ); else delete_( L, a, &Count ); }else if ( !strcmp( order, "show" ) ) { show_( L ); } } return(0); } /*============================================*/ node creat_list( int *Count ) { int n, e; node p; scanf( "%d", &n ); (*Count) = n; /*头结点*/ node head = (node) malloc( sizeof(Node) ); head->next = NULL; for ( int i = 0; i < n; i++ ) { scanf( "%d", &e ); p = (node) malloc( sizeof(Node) ); /*前插*/ p->data = e; p->next = head->next; head->next = p; } /*返回指向头结点的指针*/ return(head); } /*============================================*/ void get_( node L, int a ) { node p = L; for ( int i = 0; i < a; i++ ) p = p->next; printf( "%d\n", p->data ); } /*============================================*/ void insert_( node L, int a, int e, int *Count ) { node p = L; /*找到第a个位置之前的结点即第a-1个结点*/ for ( int i = 1; i < a; i++ ) /* for(int i=0;i<a-1;i++) */ p = p->next; node q = (node) malloc( sizeof(Node) ); q->data = e; /*插入结点*/ q->next = p->next; p->next = q; /*结点数加1*/ (*Count)++; printf( "insert OK\n" ); } /*============================================*/ void delete_( node L, int a, int *Count ) { node d, p = L; /*找到第a个位置之前的结点即第a-1个结点*/ for ( int i = 0; i < a - 1; i++ ) p = p->next; /*删除结点*/ d = p->next; p->next = d->next; free( d ); /*结点数减去1*/ (*Count)--; printf( "delete OK\n" ); } /*============================================*/ void show_( node L ) { node p = L->next; if ( p == NULL ) printf( "Link list is empty\n" ); else{ while ( p != NULL ) { printf( "%d ", p->data ); p = p->next; } printf( "\n" ); } }
别忘点赞哦-.-!
0.0分
63 人评分
好文,推荐到咱们dotcpp微信平台~
Manchester 2018-03-15 10:05:20 |
谢谢验题君-.-
后同学 2020-11-11 22:18:15 |
#include<stdio.h> #include<string.h> #include <malloc.h> int s; struct hst{ int t; struct hst* next; }*hst,Hst; struct hst* create(); void Delete(struct hst* start,int a); void show(struct hst* start); void insert(struct hst* start,int a,int b); void get(struct hst* start,int a); struct hst* create() { struct hst* temp,*start; int i; start->next=NULL; for(i=0;i<s;i++) { temp=(struct hst*)malloc(sizeof(hst)); scanf("%d",&temp->t); temp->next=start->next; start->next=temp; } return start; } void Delete(struct hst* start,int a) { struct hst* temp,*pre; pre=(struct hst*)malloc(sizeof(hst)); temp=(struct hst*)malloc(sizeof(hst)); temp=start; int i; for(i=0;i<a;i++) { pre=temp; temp=temp->next; } pre->next=temp->next; s--; free(temp); } void show(struct hst* start) { struct hst* temp; temp=(struct hst*)malloc(sizeof(hst)); temp=start; int i=0; while(i<s) { temp=temp->next; printf("%d ",temp->t); i++; } printf(" "); } void insert(struct hst* start,int a,int b) { struct hst* temp,*pre,*pnew; pre=(struct hst*)malloc(sizeof(hst)); temp=(struct hst*)malloc(sizeof(hst)); temp=start; int i; pnew=(struct hst*)malloc(sizeof(hst)); if(s!=0) { for(i=0;i<a;i++) { pre=temp; temp=temp->next; } pnew->t=b; pnew->next=pre->next; pre->next=pnew; } else { pnew->t=b; pnew->next=start->next; start->next=pnew; } s++; } void get(struct hst* start,int a) { int i; struct hst* temp; temp=(struct hst*)malloc(sizeof(hst)); temp=start; for(i=0;i<a;i++) { temp=temp->next; } printf("%d ",temp->t); } int main() { int p,i,i1; int a,b; char c[7],c1; struct hst* start; scanf("%d",&s); start=create(); scanf("%d",&p); getchar(); for(i=0;i<p;i++) { scanf("%s",&c); if(!strcmp(c,"delete")) { scanf("%d",&a); if(a<1 || a>s) { printf("delete fail "); } else { Delete(start,a); printf("delete OK "); } } else if(!strcmp(c,"show")) { if(s!=0) { show(start); } else { printf("Link list is empty "); } } else if(!strcmp(c,"insert")) { scanf("%d %d",&a,&b); if(a>s+1 || a<1) { printf("insert fail "); } else { insert(start,a,b); printf("insert OK "); } } else if(!strcmp(c,"get")) { scanf("%d",&a); if(a<1 || a>s) { printf("get fail "); } else { get(start,a); } } } }
简单的a+b (C语言代码)浏览:457 |
【亲和数】 (C语言代码)浏览:628 |
关于float,double变量的几点说明浏览:1926 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:571 |
程序员的表白 (C语言代码)浏览:678 |
班级人数 (C语言代码)浏览:981 |
C语言程序设计教程(第三版)课后习题9.3 (C语言代码)浏览:630 |
C语言训练-字符串正反连接 (C语言代码)浏览:653 |
C语言训练-排序问题<1> (C语言代码)浏览:369 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:527 |
虹山上峰 2022-02-22 10:01:00 |
这就要注意传入的L是结构体指针a