原题链接:数据结构-链表的基本操作
解题思路:
首先思考一下总思路
①:结点定义
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分
47 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
@验题君 #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); } } } }