CC12. 牛牛的链表添加节点
描述
牛牛输入了一个长度为 n 的数组,他把这个数组转换成链表并在第 i 个节点的后面添加一个值为 i 的新节点输入描述
输出描述
示例1
输入:
5 3 5 4 8 6 3
输出:
5 4 8 3 6 3
C 解法, 执行用时: 2ms, 内存消耗: 304KB, 提交时间: 2022-07-30
#include<stdio.h> typedef struct Node { int value; struct Node *link; }node; node*Init_list(int n) { node*header=(node*)malloc(sizeof(node)); header->value=0; header->link=NULL; node*pRear=header; int count=0; int v; while(count<n) { scanf("%d",&v); node*code=(node*)malloc(sizeof(node)); code->value=v; code->link=NULL; pRear->link=code; pRear=code; count++; } return header; } void add_node(node*header,int i) { node*add=(node*)malloc(sizeof(node)); node*pLast=header; node*pNow=header->link; add->value=i; while(i>0) { pLast=pNow; pNow=pNow->link; i--; } pLast->link=add; add->link=pNow; } int main() {int n; int i; scanf("%d %d",&n,&i); node*p=Init_list(n); add_node(p, i); p=p->link; for(p;p!=NULL;p=p->link) printf("%d ",p->value); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 316KB, 提交时间: 2022-07-29
#include <stdio.h> #include <stdlib.h> typedef struct Node { int num; struct Node *next; }LinkNode; LinkNode *creat_link(int n){ int i; LinkNode *head = (LinkNode *)malloc(sizeof(LinkNode)); LinkNode *tail; head->next = NULL; scanf("%d", &head->num); tail = head; for (i = 0; i < n-1; ++i) { LinkNode *p = (LinkNode *)malloc(sizeof(LinkNode)); scanf("%d", &p->num); p->next = NULL; tail->next = p; tail = tail->next; } return head; } LinkNode *insertNode(LinkNode *head,LinkNode *ptr,int value){ LinkNode *newnode=(LinkNode *)malloc(sizeof(LinkNode)); if(!newnode)return NULL; newnode->num = value; newnode->next = NULL; if(ptr==NULL){ newnode->next=head; return newnode; }else{ if(ptr->next==NULL)ptr->next=newnode; else{ newnode->next=ptr->next; ptr->next=newnode; } } return head; } int main(int argc, char const *argv[]) { int n,i; scanf("%d", &n); int num; scanf("%d", &num); LinkNode *head = creat_link(n); LinkNode *tail,*ptr; tail = head; while(i<=num+1){ ptr = tail; tail = tail->next; i++; } // printf("%d\n", ptr->num); tail = head; head = insertNode(head, ptr, num); while(tail != NULL){ printf("%d ", tail->num); tail = tail->next; } printf("\n"); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 316KB, 提交时间: 2022-06-10
#include <stdio.h> void addLink(int *arr, int size,int value) { int len; if(size > value) len = size+1; else len =value+1; int a[len]; for(int i=0; i < len; i++) { if(i < value) { a[i]=arr[i]; printf("%d ",a[i]); } else if(i == value) { a[i]=i; printf("%d ",a[i]); } else { a[i]=arr[i-1]; printf("%d ",a[i]); } } } int main() { int n,i; scanf("%d %d",&n,&i); int arr[n]; for(int m=0;m<n;m++) { scanf("%d ",&arr[m]); } addLink(arr,n,i); }
C 解法, 执行用时: 2ms, 内存消耗: 320KB, 提交时间: 2022-08-01
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; }node; int main() { int n,x,val; scanf("%d %d",&n,&x); node *head=(node*)malloc(sizeof(node)); node *cur=head,*tmp; for(int i=0;i<n;i++) { cur->next=(node *)malloc(sizeof(node)); cur=cur->next; scanf("%d",&val); cur->data=val; cur->next=NULL; } cur=head; for(int i=0;i<x;i++) { cur=cur->next; } tmp=cur->next; cur->next=(node *)malloc(sizeof(node)); cur=cur->next; cur->data=x; cur->next=tmp; cur=head->next; while(cur) { printf("%d ",cur->data); cur=cur->next; } free(cur); cur=NULL; free(tmp); tmp=NULL; return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2022-07-14
#include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node * next; }node; node *create_head() { node *head =(node *)malloc(sizeof(node)); if(head == NULL) { return NULL; } head->next =NULL; return head; } void insert_tail(node **head,int data) { node * new =(node*)malloc(sizeof(node)); new->data = data; new->next = NULL; node *ptr = *head; while(ptr->next) { ptr = ptr ->next; } ptr ->next =new; return ; } void show_slist(node *head){ if(head->next==NULL) { return ; } node *ptr = head->next; while (ptr!=NULL) { printf("%d ",ptr->data); ptr= ptr->next; } printf("\n"); } void insert_center(node *head,int data,int old) { node *new =(node *)malloc(sizeof(node)); new->data=data; new->next=NULL; node *p =head->next; if(head==NULL) { return; } while(p) { if(p->data==old) { new->next = p->next; p->next =new; } p=p->next; } } int main(void) { struct node *head =create_head(); int n=0,m=0; scanf("%d %d",&n,&m); int arr[n]; for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } for(int j=0;j<n;j++) { insert_tail(&head,arr[j]); } insert_center(head,m,arr[m-1]); show_slist(head); return 0; }