大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 數(shù)據(jù)結(jié)構(gòu)中單鏈表的實現(xiàn)

數(shù)據(jù)結(jié)構(gòu)中單鏈表的實現(xiàn)

更新時間:2022-09-28 15:10:15 來源:動力節(jié)點 瀏覽1063次

一個單鏈表就像一列火車系統(tǒng),每個轉(zhuǎn)向架連接到下一個轉(zhuǎn)向架。一個單鏈表是一個單向鏈表;即。,你只能從頭到尾節(jié)點遍歷它。這里有一些關(guān)于鏈表的快速的事實。它是用來做一個幻燈片或記事本上一些基本操作如撤銷和重做。

如何實現(xiàn)一個單鏈表嗎?

您可以創(chuàng)建節(jié)點使用類或結(jié)構(gòu)的單鏈表。你聯(lián)系他們使用下一個指針。

// implementation of singly linked list
#include <bits/stdc++.h>
using namespace std;
//A class to create node
class Node {
public:
int data;
Node* next;
};
// A function to print the given linked list
// starting from the given node
void printList(Node* n)
{
while (n != NULL)
 {
cout << n->data << " ";
n = n->next;
 }
}
int main()
{
//creating nodes
Node* head = NULL;
Node* second = NULL;
Node* third = NULL;
Node* tail = NULL;
// allocate four nodes
head = new Node();
second = new Node();
third = new Node();
  tail = new Node(); 
head->data = 2; // assign data in head node
head->next = second; // Link first node with second
  second->data = 3; // assign data to second node
  second->next = third;//Link second node with third
  third->data = 5; // assign data to third node
  third->next = tail;//Link third node with tail
tail->data = 7;// assign data to tail node
tail->next=NULL;//link tail node with NULL
//printing singly linked list
cout<<"Created singly linked list: "<<endl;
printList(head);
return 0;
}

哪些操作可以執(zhí)行在一個單鏈表嗎?

你可以在一個單鏈表執(zhí)行兩個操作:

插入

刪除

如何插入一個節(jié)點在一個單鏈表嗎?

你可以在三個不同的位置插入一個節(jié)點,它們是:

一開始

結(jié)束時

在一個特定的位置后一個節(jié)點

//A c++ code to insert a node 
//in singly linked list 
#include <bits/stdc++.h>
using namespace std; 
//A class to create nodes
class Node 
{ 
public:
int data; 
Node *next; 
}; 
// A function to insert a node at the 
//beginning of singly linked list
void push(Node** head, int newdata) 
{ 
Node* newnode = new Node();//creating newnode
newnode->data = newdata; //put in data
newnode->next = (*head); //link newnode to head
(*head) = newnode; //changing head
} 
// A function to insert a node after  
//a specific node in a singly linked list
void insertAfter(Node* prevnode, int newdata) 
{ 
//check if previous node is null
if (prevnode == NULL) 
{ 
cout<< “the given previous node cannot be NULL”; 
return; 
} 
Node* newnode = new Node();//creating newnode
newnode->data = newdata; //put in data
//link newnode to prevnode’s next node
newnode->next = prevnode->next; 
prevnode->next = newnode; //link prevnode to newnode
} 
// A function to insert a node at the 
//end of singly linked list
void append(Node** head, int newdata) 
{ 
Node* newnode = new Node();//creating newnode
Node *last = *head; // creating a ‘last’ node
newnode->data = newdata; //put in data
newnode->next = NULL; //link newnode with null
//Check if head is null
if (*head == NULL) 
{ 
*head = newnode; 
return; 
} 
//traversing ‘last’ node to end of the linked list 
while (last->next != NULL) 
last = last->next; 
//link ‘last’ node with newnode
last->next = newnode; 
return; 
} 
// A function to print the given linked list
// starting from the given node
void printList(Node *node) 
{ 
while (node != NULL) 
{ 
cout<<" "<<node->data; 
node = node->next; 
} 
}   
/* Driver code*/
int main() 
{ 
/* Start with the empty list */
Node* head = NULL;       
// Insert 6 at the end,
append(&head, 6); 
//6->NULL     
// Insert 7 as head 
push(&head, 7); 
//7->6->NULL       
// Insert 1 as head. 
push(&head, 1); 
//1->7->6->NULL      
// Insert 4 at the end  
append(&head, 4); 
//1->7->6->4->NULL       
// Insert 8, after 7
insertAfter(head->next, 8); 
//1->7->8->6->4->NULL      
cout<<"Created Linked list is: "; 
printList(head);       
return 0; 
}

如何從一個單鏈表中刪除一個節(jié)點?

你可以刪除一個節(jié)點從3個不同的位置,它們是:

從一開始

從過去的

給定節(jié)點后從一個特定的位置

//A c++ code to insert a node 
//in singly linked list
#include <bits/stdc++.h>
using namespace std;
//A class to create node
class Node{
public:
int data;
Node* next;
};
//insert a node at the beginning
void push(Node** head, int newdata)
{
//create newnode
Node* newnode = new Node();
newnode->data = newdata;//put in data
newnode->next = (*head);//link newnode with head
(*head) = newnode;//changing head
}
//A function to delete a node
void deleteNode(Node** head, int key)
{
Node* temp = *head;//creating temp node
Node* prev = NULL;//creating prev node
//checking if node to be deleted is head the node
if (temp != NULL && temp->data == key)
{
*head = temp->next;//changing head
delete temp; //delete node
return;
}
else
{
//traversing to find key to delete
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
return;
prev->next = temp->next;
delete temp;//delete node
}
}
// This function prints contents of
// linked list starting from the
// given node
void printList(Node* node)
{
while (node != NULL)
{
cout << node->data << " ";
  node = node->next;
}
}
// Driver code
int main()
{    
// Start with the empty list
Node* head = NULL; 
// Add elements in linked list
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
puts("Created Linked List: ");
printList(head);
deleteNode(&head, 1);
puts("\nLinked List after Deletion of 1: "); 
printList(head);  
return 0;
}

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 成人毛片免费视频播放 | 久久综合亚洲伊人色 | 久久久精品国产免费观看同学 | 一级毛片美国一级j毛片不卡 | 成人合集大片bd高清在线观看 | 一级中国毛片 | 日韩天堂在线 | 手机在线看片不卡中文字幕 | 噜噜噜天天躁狠狠躁夜夜精品 | 中文字幕一区二区三区 精品 | 精品欧美一区二区三区精品久久 | 日日爽天天干 | 久久66热这里只会有精品 | 精品 日韩 国产 欧美在线观看 | 伊人色综合久久 | 国内精品亚洲 | 久久天天躁综合夜夜黑人鲁色 | 97在线视频免费播放 | 国产精品mm| 日本中文在线观看 | 天天夜碰日日摸日日澡 | 国产精品福利久久2020 | 五月婷婷国产 | 精品久久一区二区三区 | 特黄特黄aaaa级毛片免费看 | 特级一级全黄毛片免费 | 伊人久久久久久久久香港 | 色综合图 | 中文国产成人久久精品小说 | 精品a视频 | 成人亚洲精品一区二区 | 国产精品久久久久无码av | 高清性色生活片久久久 | 尹人香蕉网在线观看视频 | 日本三级做a全过程在线观看 | 午夜私人影院在线观看 | 久久99精品久久久久久青青日本 | 国产亚洲精品久久久久久 | 四虎地址8848 | 日本精品视频在线观看 | 一区二区三区国产 |