本文目錄一覽:
求單鏈表的基本操作代碼
#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-hant/n/242991.html