代码语言:javascript复制
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node, *LinkedList;
LinkedList insert(LinkedList head, Node *node, int index) {
if (head == NULL) {//头指针为空
if (index != 0) {
return head;
}
head = node;
return head;
}
if (index == 0) {//插入结点位置链表首位
node->next = head;
head = node;
return head;
}
Node *current_node = head;
int count = 0;
while (current_node->next != NULL && count < index - 1) {
current_node = current_node->next;
count ;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
}
return head;
}
// 请在下面实现输出函数 output
void output(LinkedList head){
if(head == NULL){
return ;
}
Node *current_node = head;
while(current_node != NULL){
printf("%d ",current_node->data);
current_node = current_node->next;
}
printf("n");
}
void clear(LinkedList head) {
Node *current_node = head;
while (current_node != NULL) {
Node *delete_node = current_node;
current_node = current_node->next;
free(delete_node);
}
}
int main() {
LinkedList linkedlist = NULL;
for (int i = 1; i <= 10; i ) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = i;
node->next = NULL;
linkedlist = insert(linkedlist, node, i - 1);
}
output(linkedlist);
clear(linkedlist);
return 0;
}
代码语言:javascript复制#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node, *LinkedList;
LinkedList insert(LinkedList head, Node *node, int index) {
if(head == NULL){
if(index != 0){
printf("failedn");
return head;
}
head = node;
printf("successn");
return head;
}
if(index == 0){
node->next = head;
head = node;
printf("successn");
return head;
}
Node *current_node = head;
int count = 0;
while(current_node->next != NULL && count < index - 1){
current_node = current_node->next;
count ;
}
if(count == index -1 ){
node->next = current_node->next;
current_node->next = node;
printf("successn");
return head;
}
printf("failedn");
return head;
}
void output(LinkedList head) {
if(head == NULL){
return;
}
Node * current_node = head;
while(current_node != NULL){
printf("%d",current_node->data);
if(current_node->next != NULL){
printf(" ");
}
current_node = current_node->next;
}
//printf("n");
}
void clear(LinkedList head) {
Node *current_node = head;
while(current_node != NULL){
Node *delete_node = current_node;
current_node = current_node->next;
free(delete_node);
}
}
int main() {
LinkedList linkedlist = NULL;
int n;
scanf("%d",&n);
int p,q;
for(int i = 0; i < n; i ){
Node *node =(Node *)malloc(sizeof(node));
scanf("%d %d", &p, &q);
node->data = q;
node->next = NULL;
linkedlist = insert(linkedlist, node, p);
}
output(linkedlist);
clear(linkedlist);
return 0;
}