别看我只是一只羊


私信TA

用户名:bkwzsyzy

访问量:3788

签 名:

等  级
排  名 2322
经  验 2271
参赛次数 1
文章发表 23
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

TA的其他文章

解题思路:

注意事项:

参考代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node {
   // int  sno;
    int sc;
    struct Node* next;
}node, * LinkList;

LinkList L;

void CreateFromHead(int flag) {
    node* s;
    int c;
    //struct Node L;
    L = (node*)malloc(sizeof(node));  //建立头结点
    L->next = NULL;                     //建立空的单链表L
    
    //int n;
    int f=0;
    while (f<flag) {
       // scanf("%d", &n);
        scanf("%d",&c);
         
        f++;
        //if (c != 0) {
            s = (node*)malloc(sizeof(node));//建立新结struct Node *点
            s->sc = c;
           // s->sno=n;
            s->next = L->next;              //将s结点插入表头
            L->next = s;
    //  }
     
    }
   
}


void InsList(LinkList L, int i, int e) {
/*在带头结点的单链表L中第i个位置插入值为e的新结点*/
	node* pre, * s;
	pre = L;
	int k = 0;
	while (pre != NULL && k < i - 1) {		//查找第i-1个结点
		pre = pre->next;
		k++;
	}
	if (pre == NULL) {
		printf("insert fail\n");

	}
	s = (node*)malloc(sizeof(node));		//申请一个新结点
	s->sc = e;
	s->next = pre->next;					//修改指针完成插入操作
	pre->next = s;
	printf("insert OK\n");
   


}


void DelList(LinkList L, int i) {
/*在带头结点的单链表L中删除第i个元素*/
	node* pre, * r;
	pre = L;

	int k = 0;
	while (pre->next != NULL && k < i - 1) {//查找第i-1个结点
		pre = pre->next;
		k++;
	}
///	if (pre->next == NULL) {
//		printf("delete fail\n");
		//return ERROR;
//	}
	r = pre->next;
	pre->next = r->next;
	//修改指针,删除结点r
	int e=0;
	e=r->sc;
	free(r);
	printf("delete OK\n");
//	o--;
	//return o;
	//return OK;
}


void get(LinkList L, int a )
{
   node* p = L;
 
    for ( int i = 0; i < a; i++ )
        p = p->next;
 
    printf( "%d\n", p->sc);
}

void OutputList(LinkList L) {
    node* p;
        p = L->next;
    if (p == NULL) {
        printf("Link list is empty\n");
        //return 
    }
    else {
    //  node* p;
       // p = L->next;
        while (p!=NULL) {
            printf("%d ",p->sc);
            p = p->next;
        }
        printf("\n");
    }
}
 

int main()
{
	int n;
	scanf("%d",&n);
	CreateFromHead(n);
//	OutputList(L);
//	InsList(L,2,8);
//	OutputList(L);
//	DelList(L,3); 
//	OutputList(L);
//	get(L,2);
	int y;
	int o;
	o=n;
	scanf("%d",&y);
	char z[7];
	for(int i=0;i<y;i++){
	    scanf("%s",z);
	    //printf("%s",z);
	    if(strcmp(z,"show")==0){
	       OutputList(L);
	    }
	    
	    
	    if(strcmp(z,"delete")==0){
	       
	        int a;
	        scanf("%d",&a);
	        
	        if ( a > o || a < 1 )
                printf( "delete fail\n" );
            else
                {
                   DelList(L,a);
                   o--;
                }
	    }
	    
	    if(strcmp(z,"get")==0){
	        int a;
	        scanf("%d",&a);
	        get(L,a);
	    }
	    
	    if(strcmp(z,"insert")==0){
	        int a,b;
	        scanf("%d",&a);
	        scanf("%d",&b);
	        if ( a>1&&L->next==NULL)
            printf("insert fail\n");
            else{   InsList(L,a,b);
	             o++;}
	      
	        
	    }
	        //InsList(L,a,b,o);
	        
	    }
	   
	}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区