js鏈表代碼,單鏈表代碼

本文目錄一覽:

求單鏈表的基本操作代碼

#include stdio.h

#include windows.h

typedef struct node

{

int num;

struct node *next;

}lnode;

lnode *creat()

{

lnode *head,*p,*q;

int n;

head=NULL;

printf(“輸入要創建的節點數\n”);

scanf(“%d”,n);

while(n)

{

p=(lnode *)malloc(sizeof(lnode));

printf(“輸入數據\n”);

scanf(“%d”,p-num);

if (head==NULL)

{

head=q=p;

}

else

{

q-next=p;

q=p;

}

n–;

}

q-next=NULL;

return head;

}

lnode *insert(lnode *head)

{

lnode *p,*q,*s;

int n;

char ch;

q=p=head;

printf(“輸入插入的位置\n”);

scanf(“%d”,n);

printf(“請選擇是插入在前還是在後(F or B)\n”);

getchar();

ch=getchar();

if(ch==’F’||ch==’f’)

{

s=(lnode *)malloc(sizeof(lnode));

printf(“請輸入數據\n”);

scanf(“%d”,s-num);

while(p–n)

{

q=p;

p=p-next;

}

if (q==p)

{

s-next=q;

return s;

}

else

{

q-next=s;

s-next=p;

return head;

}

}

else if (ch==’B’||ch==’b’)

{

s=(lnode *)malloc(sizeof(lnode));

printf(“請輸入數據\n”);

scanf(“%d”,s-num);

while(pn–)

{

q=p;

p=p-next;

}

if (NULL==q-next)

{

q-next=s;

s-next=NULL;

return head;

}

else

{

q-next=s;

s-next=p;

return head;

}

}

else

{

printf(“輸入錯誤\n”);

}

}

lnode *del(lnode *head)

{

lnode *p,*q;

int n;

int flag=0;

p=q=head;

printf(“請輸入刪除的數據\n”);

scanf(“%d”,n);

while(p)

{

if (p-num==n)

{

flag=1;

if (head==p)

{

head=head-next;

}

else if(NULL==p-next)

{

q-next=NULL;

}

else

{

q-next=p-next;

}

}

q=p;

p=p-next;

}

if (flag==0)

{

printf(“沒有找到數據\n”);

system(“pause”);

}

else

{

printf(“刪除成功\n”);

system(“pause”);

}

return head;

}

lnode *sort(lnode *head)

{

lnode *t,*f,*min,*p_min,*s;

char ch;

f=NULL;

printf(“請輸入排序方式:升序(A/a),降序(D/d)\n”);

getchar();

ch=getchar();

if (ch==’A’||ch==’a’)

{

while(NULL!=head)

{

for (min=head,s=head;s-next!=NULL;s=s-next)

{

if (min-nums-next-num)

{

p_min=s;

min=s-next;

}

}

if (NULL==f)

{

f=min;

t=min;

}

else

{

t-next=min;

t=min;

}

if (min==head)

{

head=head-next;

}

else

{

p_min-next=min-next;

}

}

if (f!=NULL)

{

t-next=NULL;

}

printf(“排序完成\n”);

system(“pause”);

head=f;

return f;

}

else if (ch==’D’||ch==’d’)

{

while(NULL!=head)

{

for (min=head,s=head;s-next!=NULL;s=s-next)

{

if (min-nums-next-num)

{

p_min=s;

min=s-next;

}

}

if (NULL==f)

{

f=min;

t=min;

}

else

{

t-next=min;

t=min;

}

if (min==head)

{

head=head-next;

}

else

{

p_min-next=min-next;

}

}

if (f!=NULL)

{

t-next=NULL;

}

printf(“排序完成\n”);

system(“pause”);

head=f;

return f;

}

}

void dispaly(lnode *head)

{

lnode *p;

p=head;

printf(“\n”);

while(p!=NULL)

{

printf(“%d\t”,p-num);

p=p-next;

}

}

int getoption()

{

int n;

printf(“0 退出\n”);

printf(“1 創建鏈表\n”);

printf(“2 插入節點\n”);

printf(“3 刪除節點\n”);

printf(“4 排序節點\n”);

printf(“5 顯示鏈表\n”);

printf(“請選擇操作\t”);

scanf(“%d”,n);

return n;

}

int main()

{

lnode *temp;

char ch;

int o;

do

{

system(“cls”);

o=getoption();

switch (o)

{

case 0:

exit(0);

break;

case 1 :

system(“cls”);

temp=creat();

break;

case 2:

system(“cls”);

temp=insert(temp);

break;

case 3:

system(“cls”);

temp=del(temp);

break;

case 4:

system(“cls”);

temp=sort(temp);

break;

case 5:

system(“cls”);

dispaly(temp);

system(“pause”);

break;

}

system(“cls”);

printf(“按0退出,任意鍵繼續\t”);

ch=getchar();

if (ch==’0′)

{

exit(0);

}

} while (ch!=’0′);

}

JS中 數組與鏈表

常規數組: 數組元素內容是一種類型的元素,如const arr = [1,2,3,4],在存儲空間是連續內存的

JS數組: 數組元素內容不是同一種類型的元素,如const arr = [‘haha’, 1, {a:1}],則在存儲上是一段非連續空間。此時,JS 數組不再具有數組的特徵,其底層其實是由鏈表來實現的

總結

鏈表的插入/刪除效率較高,而訪問效率較低;

數組的訪問效率較高,而插入效率較低

js鏈表怎麼去輸入啊

//Node表示要加入列表的項

var Node=function(element){

this.element=element;

this.next=null;

};

var length=0;//存儲列表項的數量

var head=null;//head存儲的是第一個節點的引用

//向鏈表尾部追加元素

this.append=function(element){

var node=new Node(element),

current;

if(head===null){

head=node;

 }else{

current=node;

while(current.next){

current=current.next;

}

current.next=node;

}

length++;

};

//在鏈表的任意位置插入元素

this.insert=function(position,element){

if(position=0position=length){

var node=new Node(element),

current=head,

previous,

index=0;

if(position===0){

node.next=current;

head=node;

}else{

while(indexposition){

previous=current;

previous.next=node;

index++;

}

node.next=current;

previous.next=node;

}

length++;

return true;

}else{

return false;

}

};

//從鏈表中移除元素

this.removeAt=function(position){

if(position-1  positionlength){

var current=head,

previous,

index=0;

if(position===0){

head=current.next;

}else{

while(indexposition){

previous=current;

current=current.next;

index++;

}

previous.next=current.next;

}

length–;

return current.element;

}else{

return null;

}

};

//返回元素在鏈表中的位置

this.indexOf=function(element){

var current=head,

index=-1;

while(current){

if(element===current.element){

return index;

}

index++;

current=current.next;

}

return -1;

};

//移除某個元素

this.remove=function(element){

var index=this.indexOf(element);

return this.removeAt(index);

};

//判斷鏈表是否為空

this.isEmpty=function(){

return length===0;

};

//返回鏈表的長度

this.size=function(){

return length;

};

//把LinkedList對象轉換成一個字元串

this.toString=function(){

var current=head,

string=””;

while(current){

string=current.element;

current=current.next;

}

return string;

};

};

var list=new LinkedList();

list.append(15);

list.append(10);

list.insert(1,11);

list.removeAt(2)

console.log(list.size());

js 刪除鏈表中重複的節點

題目描述:

給定一個排序的鏈接列表,刪除所有具有重複數字的節點,從原始列表中只留下不同的數字。

例如, 給定1- 2- 3- 3- 4- 4- 5,返回1- 2- 5。

給定1- 1- 1- 2- 3,返回2- 3。

JavaScript 版數據結構與演算法(三)鏈表

可以看出JavaScript中的鏈表是通過不斷 new 出來節點,並在節點的next屬性上繼續 new 創建出來的

結構大概長這樣:

參考資料:

我該怎麼在js里獲取後台查詢到的list

1、首先創建如下空鏈表,添加數據到鏈表中。

2、輸出顯示鏈表中的內容。

3、調用front獲取list容器中的頭部信息。

4、輸出如下所示的數據,是list容器中的第一個信息。

5、調用back獲取list容器中的尾部信息。

6、輸出如下所示的數據,是list容器中的最後一個信息。

7、如果想要查找list容器中的某個元素,通過調用函數find實現,傳入容器的起始位置、結束位置和需要查找的信息。

8、最後通過函數find返回的迭代器來判斷是否查找到數據,並且獲取數據內容。

【JS演算法】 刪除鏈表中某個節點

先來了解一個基礎知識

b=a,但改變 b,並不會影響 a

y=x , 但改變y,會影響x,因為class有原型鏈

1=2=3,鏈表是由一組節點組成的集合。每個節點都使用一個對象的引用指向它的後繼,指向另一個節點的引用叫做鏈

給你一個鏈表的頭節點 head 和一個整數 val ,請你刪除鏈表中所有滿足 Node.val == val 的節點,並返回 新的頭節點 。

輸入:head = [1,2,6,3,4,5,6] val = 6

輸出:[1,2,3,4,5]

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/242991.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 12:53
下一篇 2024-12-12 12:53

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • 利用Python實現兩個鏈表合併為一個有序鏈表

    對於開發工程師來說,實現兩個鏈表合併為一個有序鏈表是必須掌握的技能之一。Python語言在鏈表處理上非常便利,本文將從多個方面詳細闡述如何利用Python實現兩個鏈表合併為一個有序…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變數和數…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在著手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29

發表回復

登錄後才能評論