解题思路:
注意事项:
参考代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int num;
struct node *next;
}listnode;
listnode *creatnode(int amount,int *q);
void shownode(listnode *head);
void getnode(listnode *head);
void deletenode(listnode *head);
void insertnode(listnode *head);
void freenode(listnode *head);
int main()
{
int n,m,*p;
scanf("%d",&n);
p=(int *)malloc(sizeof(int)*n);
for(int i=0;i<n;i++)
{
scanf("%d",&p[n-1-i]);
}
listnode *firstnode=creatnode(n,p);
free(p);
scanf("%d",&m);
while(m--)
{
char str[10]={0};
scanf("%s",str);
if(!strcmp(str,"show"))
{
shownode(firstnode);
}
else if(!strcmp(str,"get"))
{
getnode(firstnode);
}
else if(!strcmp(str,"delete"))
{
deletenode(firstnode);
}
else if(!strcmp(str,"insert"))
{
insertnode(firstnode);
}
else
{
continue;
}
}
freenode(firstnode);
return 0;
}
listnode *creatnode(int amount,int *q)
{
listnode *head,*middle,*temp;
head=(listnode *)malloc(sizeof(listnode));
if(head==NULL)
{
perror("malloc");
exit(0);
}
head->next=NULL;
temp=head;
for(int i=0;i<amount;i++)
{
middle=(listnode *)malloc(sizeof(listnode));
if(middle==NULL)
{
perror("malloc");
exit(0);
}
middle->num=q[i];
temp->next=middle;
temp=middle;
}
temp->next=NULL;
return head;
}
void shownode(listnode *head)
{
listnode *temp=head;
listnode *i;
for(i=temp;i->next!=NULL;i=i->next)
{
printf("%d ",i->next->num);
}
if(i==temp)
{
printf("Link list is empty");
}
printf("\n");
}
void getnode(listnode *head)
{
int sz,count=0;
listnode *temp=head;
scanf("%d",&sz);
for(listnode *i=temp;i->next!=NULL;i=i->next)
{
count++;
if(count==sz)
{
printf("%d",i->next->num);
}
}
if(sz<1 || count<sz)
{
printf("get fail");
}
printf("\n");
}
/*void deletenode(listnode *head)
{
listnode *temp=head;
int sz,count=0;
scanf("%d",&sz);
for(listnode *i=temp;i->next!=NULL;i=i->next)
{
count++;
if(count==sz)
{
listnode *temp1=i->next;
i->next=temp1->next;
free(temp1);
printf("delete OK");
}
}
if(sz<1 || count<sz)
{
printf("delete fail");
}
printf("\n");
}*/
void deletenode(listnode *head) {
int sz, count = 0;
listnode *temp = head, *prev = NULL;
scanf("%d", &sz);
while (temp != NULL && count < sz)
{
count++;
prev = temp;
temp = temp->next;
}
if (temp == NULL || sz < 1)
{
printf("delete fail\n");
}
else
{
if (prev != NULL) {
prev->next = temp->next;
}
else
{ // Deleting the head node
head = temp->next;
}
free(temp);
printf("delete OK\n");
}
}
/*void insertnode(listnode *head)
{
listnode *temp=head;
int a,e,count=0;
scanf("%d %d",&a,&e);
for(listnode *i=temp;i->next!=NULL;i=i->next)
{
count++;
}
if(a<1 || count<a)
{
printf("insert fail");
}
else
{
count=0;
for(listnode *j=temp;j->next!=NULL;j=j->next)
{
count++;
if(count==(a-1))
{
listnode *temp1=j->next;
listnode *middle;
middle=(listnode *)malloc(sizeof(listnode));
middle->num=e;
middle->next=temp1->next;
temp1->next=middle;
printf("insert OK");
}
}
}
printf("\n");
}*/
void insertnode(listnode *head)
{
int a, e, count = 0;
listnode *temp = head, *prev = NULL;
scanf("%d %d", &a, &e);
while (temp != NULL && count < a)
{
count++;
prev = temp;
temp = temp->next;
}
if (a < 1 || count < a)
{
printf("insert fail\n");
}
else
{
listnode *new_node = (listnode *)malloc(sizeof(listnode));
if (new_node == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
new_node->num = e;
new_node->next = temp;
if (prev != NULL)
{
prev->next = new_node;
}
else
{ // Inserting at the head position
head = new_node;
}
printf("insert OK\n");
}
}
void freenode(listnode *head)
{
listnode *curr=head;
while(curr)
{
listnode *temp=curr->next;
free(curr);
curr=temp;
}
}
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复