日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

单链表的几个基本操作

發布時間:2025/7/14 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 单链表的几个基本操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

/*以頭插法,創建長度為n的單鏈表,并實現對其的增、刪、改、查*/

?

#include<stdio.h> #include<stdlib.h>struct node {int data;struct node *next; };struct node *Creat_List(struct node *head, int n) //創建鏈表 {struct node *p;for(int i=0;i<n;i++){p = (struct node *)malloc(sizeof(struct node));scanf("%d",&p->data);p->next = head;head = p;}return head; }void Put(struct node *head) //打印鏈表 {struct node *p;p = head;while(p != NULL){printf("%d ",p->data);p = p->next;}printf("\n"); }struct node* Insert(struct node *head,int k,int num) //在k處增加元素num {struct node *p;p = head;for(int i=0;i<k;i++){p = p->next;}struct node *temp;temp = (struct node *)malloc(sizeof(struct node));temp->data = num;temp->next = p->next;p->next = temp;return head; }struct node *Delete(struct node *head, int k) {struct node *p;p = head;for(int i=1;i<k-1;i++){p = p->next;}struct node *temp;temp = p->next;p->next = temp->next;return head; }bool Find(struct node *head,int num) {struct node *p;p = head;while(p != NULL){if(p->data == num){return true;}p = p->next;}return false; }struct node *Change(struct node *head,int k,int num) //將數字k改為num {struct node *p;p = head;while(p!=NULL){if(p->data == k){p->data = num;}p = p->next;}return head; }int main() {int n; //鏈表長度scanf("%d",&n);struct node *head=NULL;head = Creat_List(head, n);Put(head);head = Insert(head,2,1000);//Put(head);head = Delete(head,4);//Put(head);head = Change(head,4,6);//Put(head);bool ok = Find(head,7);if(ok)printf("yes\n");elseprintf("no\n");return 0; } View Code

?

轉載于:https://www.cnblogs.com/alan-W/p/5887002.html

總結

以上是生活随笔為你收集整理的单链表的几个基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。