问题 A: 算法2-8~2-11:链表的基本操作(问题遗留)

http://codeup.cn/problem.php?cid=100000607&pid=0
输入
输入数据只有一组,第一行有n+1个整数,第一个整数是这行余下的整数数目n,后面是n个整数。这一行整数是用来初始化列表的,并且输入的顺序与列表中的顺序相反,也就是说如果列表中是1、2、3那么输入的顺序是3、2、1。
第二行有一个整数m,代表下面还有m行。每行有一个字符串,字符串是“get”,“insert”,“delete”,“show”中的一种。如果是“get”或者“delete”,则其后跟着一个整数a,代表获得或者删除第a个元素;如果是“insert”,则其后跟着两个整数a和e,代表在第a个位置前面插入e;“show”之后没有整数。
输出
如果获取成功,则输出该元素;如果删除成功则输出“delete OK”;如果获取失败或者删除失败,则输出“get fail”以及“delete fail”。如果插入成功则输出“insert OK”,否则输出“insert fail”。如果是“show”则输出列表中的所有元素,如果列表是空的,则输出“Link list is empty”。注:所有的双引号均不输出。
样例输入 Copy
3 3 2 1
21
show
delete 1
show
delete 2
show
delete 1
show
delete 2
insert 2 5
show
insert 1 5
show
insert 1 7
show
insert 2 5
show
insert 3 6
show
insert 1 8
show
get 2
样例输出 Copy
1 2 3
delete OK
2 3
delete OK
2
delete OK
Link list is empty
delete fail
insert fail
Link list is empty
insert OK
5
insert OK
7 5
insert OK
7 5 5
insert OK
7 5 6 5
insert OK
8 7 5 6 5
7
我的答案(错的错的错的)

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define maxn 100001
struct Node{
	int data;
	struct Node *next;
};
struct Node* init(int n)
{
	struct Node *h=(struct Node*)malloc(sizeof(struct Node));
	if(h==NULL)
		return NULL;
	h->next=NULL;
	for(int i=0;i<n;i++)
	{
		int m;
		scanf("%d",&m);
		struct Node *p=(struct Node*)malloc(sizeof(struct Node));
		p->data=m;
		p->next=h->next;
		h->next=p;
	}
	return h;
}
void show(struct Node *h)
{
	struct Node* p=h->next;
	if(!p)
	{
		printf("Link list is empty\n");
		return;
	}
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
}
int deleted(struct Node *h,int m)
{

	if(!h||!h->next)return 0;
	struct Node* pr=h;
	struct Node* p=h->next;
	int count=0;
	while(p)
	{
		count++;
		if(count==m)
		{
			pr->next=p->next;
			free(p);
			return 1;
		}
		p=p->next;
		pr=pr->next;
	}
	return 0;
}
int insert(struct Node* h,int pos,int m)
{
	int count=0;
	struct Node* pr=h;
	struct Node* p=h->next;
	while(p)
	{
		count++;
		if(count==pos)
		{
			struct Node* n=(struct Node*)malloc(sizeof(struct Node));
			n->data=m;
			n->next=p;
			pr->next=n;
			return 1;
		}
		pr=p;
		p=p->next;
	}
	if(count+1==pos)
	{
		struct Node* n=(struct Node*)malloc(sizeof(struct Node));
		n->data=m;
		n->next=p;
		pr->next=n;
		return 1;
	}
	return 0;
}
int gett(struct Node* h,int pos)
{
	int count=0;
	struct Node* p=h->next;
	while(p)
	{
		count++;
		if(count==pos)
		{
			printf("%d\n",p->data);
			return 1;
		}
		p=p->next;
	}
	printf("get fail\n");
	return 0;
}
int main(){
	int n;
	while(scanf("%d",&n)!=EOF)
	{
	int i;
	struct Node* head;
	head=init(n);
	char str[10];
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",str);
		if(strcmp(str,"show")==0)
			show(head);
		else if(strcmp(str,"delete")==0)
		{
			int m;
			scanf("%d",&m);
			int flag=deleted(head,m);
			if(flag)
				printf("delete OK\n");
			else
				printf("detele fail\n");
		}
		else if(strcmp(str,"insert")==0)
		{
			int m,pos;
			scanf("%d%d",&pos,&m);
			int flag=insert(head,pos,m);
			if(flag)
				printf("insert OK\n");
			else
				printf("insert fail\n");
		}
		else
		{
			int pos;
			scanf("%d",&pos);
			int flag=gett(head,pos);
		}
	}
	}
	return 0;
}

正确答案:
转自https://www.it610.com/article/5004179.htm


#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0

struct stu{
    int num;
    struct stu *next;
};
int N,n;
int num3;
struct stu *creat(){
    struct stu *head;
    struct stu *p1=NULL;
    struct stu *p2=NULL;
    n=0;
    p1=(struct stu *)malloc(sizeof(struct stu));
    p2=p1;
    if(p1==NULL)
        return NULL;
    else {
        head =NULL;
        scanf("%d",&(p1->num));
        num3=p1->num;
    }
    while(n<N){
            n+=1;
        if(n==1){
            head=p1;
            p2->next=NULL;
        }
        else{
            p2->next=p1;
        }
        p2=p1;
        p1=(struct stu *)malloc(sizeof(struct stu));
        scanf("%d",&(p1->num));
        num3=p1->num;
    }
    p2->next=NULL;
    free(p1);
    p1=NULL;
    return head;
};
struct stu *del(struct stu *head,int num){
    struct stu *p1;
    struct stu *p2;
    if(head==NULL){
            printf("delete fail\n");
        return head;
    }
    p1=head;
    n=1;
    while(n<num&&p1->next!=NULL){
        p2=p1;
        p1=p1->next;
        n++;
    }
    if(n==num){
        if(p1==head){
            head=p1->next;
        }
        else{
            p2->next=p1->next;
        }
        free(p1);
        p1=NULL;
        N-=1;
        printf("delete OK\n");
    }
    else{
        printf("delete fail\n");
    }
    return head;
};
struct stu *insert(struct stu *head,struct stu *node,int num1){
    struct stu *p1;
    n=1;
    if(head==NULL){
        if(num1==1){
            head=node;
            node->next=NULL;
            N+=1;
            printf("insert OK\n");
        }
        else
            printf("insert fail\n");
        return head;
    }
    p1=head;
    if(head!=NULL&&num1==1){
        node->next=p1;
        head=node;
        printf("insert OK\n");
        return head;
    }
        while(n<num1-1&&p1->next!=NULL){
            n++;
            p1=p1->next;
        }
        if(n==num1-1){
            node->next=p1->next;
            p1->next=node;
            N++;
            printf("insert OK\n");
        }
        else {
            printf("insert fail\n");
        }
    return head;
};
struct stu *reverse(struct stu *head){
    struct stu *p;
    struct stu *p1;
    struct stu *p2;
    p1=NULL;
    p2=head;
    while(p2!=NULL){
        p=p2->next;
        p2->next=p1;
        p1=p2;
        p2=p;
    }
    head =p1;
    return head;
};
void print(struct stu *head){
    struct stu *p;
    p=head;
    if(head!=NULL){
        do{
            printf("%d",p->num);
            p=p->next;
            if(p!=NULL)
            printf(" ");
        }while(p!=NULL);
        printf("\n");
    }
    else printf("Link list is empty\n");
}
void gt(struct stu *head,int num){
    struct stu *p;
    p=head;
    n=1;
    while(n<num&&p->next!=NULL){
        p=p->next;
        n++;
    }
    if(n==num){
        printf("%d\n",p->num);
    }
    else
        printf("get fail\n");
}
int main(){
    struct stu *head;
    struct stu *stu1;
    scanf("%d",&N);
    head=creat();
    //print(head);
    head=reverse(head);
    //print(head);
    //int num;
    int i,j;
    //scanf("%d",&num);
    //printf("%d\n",num);
    char s[20];
    int num1,num2;
    for(i=0;i<num3;i++){
        scanf("%s",s);
        if(strcmp(s,"show")==0){
            print(head);
        }
        else if(strcmp(s,"delete")==0){
            scanf("%d",&num1);
            head=del(head,num1);
        }
        else if(strcmp(s,"insert")==0){
            stu1=(struct stu *)malloc(sizeof(struct stu));
            scanf("%d %d",&num1,&(stu1->num));
            head=insert(head,stu1,num1);
        }
        else if(strcmp(s,"get")==0){
                scanf("%d",&num1);
            gt(head,num1);
        }
        memset(s,'\0',sizeof(s));
    }
    return 0;
}











  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茅蒐Rubia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值