1.插入元素 2.删除元素 3.获取第i个结点的数据元素 4.判断元素是否存在5.获取某个元素的下一个元素;NextElem(L, cur_e, &next_e)main函数里面怎么写
时间: 2023-05-29 08:04:34 浏览: 86
next_e)6.获取某个元素的前一个元素;PriorElem(L, cur_e, prior_e)7.获取线性表的长度;ListLength(L)8.清空线性表;ClearList(L)9.判断线性表是否为空;ListEmpty(L)10.遍历线性表;ListTraverse(L, visit)
相关问题
编程实现单向链表的基本操作: 1.插入元素 2.删除元素 3.获取第i个结点的数据元素 4.判断元素是否存在 5.获取某个元素的下一个元素;
C++代码实现单向链表的基本操作:
```c++
#include<iostream>
using namespace std;
struct ListNode{
int data;
ListNode *next;
};
// 插入元素
ListNode* insert(ListNode* head, int newData){
ListNode *newNode = new ListNode;
newNode->data = newData;
newNode->next = NULL;
if(head==NULL){
head = newNode;
}
else{
ListNode *p = head;
while(p->next!=NULL){
p = p->next;
}
p->next = newNode;
}
return head;
}
// 删除元素
ListNode* remove(ListNode* head, int key){
if(head==NULL){
return NULL;
}
else if(head->data == key){
ListNode *temp = head;
head = head->next;
delete temp;
}
else{
ListNode *p = head;
while(p->next!=NULL && p->next->data!=key){
p = p->next;
}
if(p->next!=NULL){
ListNode *temp = p->next;
p->next = temp->next;
delete temp;
}
}
return head;
}
// 获取第i个结点的数据元素
int getElement(ListNode* head, int i){
int count = 0;
ListNode *p = head;
while(p!=NULL && count<i){
p = p->next;
count++;
}
if(p!=NULL){
return p->data;
}
else{
return -1; // 若不存在第i个结点,返回-1
}
}
// 判断元素是否存在
bool contains(ListNode* head, int key){
ListNode *p = head;
while(p!=NULL && p->data!=key){
p = p->next;
}
if(p!=NULL){
return true;
}
else{
return false;
}
}
// 获取某个元素的下一个元素
int getNextElement(ListNode* head, int key){
ListNode *p = head;
while(p!=NULL && p->data!=key){
p = p->next;
}
if(p!=NULL && p->next!=NULL){
return p->next->data;
}
else{
return -1; // 若key不存在或key是最后一个结点,返回-1
}
}
int main(){
ListNode *head = NULL;
head = insert(head, 1);
head = insert(head, 3);
head = insert(head, 5);
head = insert(head, 7);
cout<<"链表的元素为:";
ListNode *p = head;
while(p!=NULL){
cout<<p->data<<" ";
p = p->next;
}
head = remove(head, 3);
cout<<"\n删除元素3后,链表的元素为:";
p = head;
while(p!=NULL){
cout<<p->data<<" ";
p = p->next;
}
int element = getElement(head, 2);
cout<<"\n获取第2个结点的元素为:"<<element<<endl;
bool flag = contains(head, 5);
cout<<"是否存在元素5:"<<flag<<endl;
int nextElement = getNextElement(head, 7);
cout<<"元素7的下一个元素为:"<<nextElement<<endl;
return 0;
}
```
输出结果:
```
链表的元素为:1 3 5 7
删除元素3后,链表的元素为:1 5 7
获取第2个结点的元素为:5
是否存在元素5:1
元素7的下一个元素为:-1
```
编程实现单向链表的基本操作: 1.插入元素 2.删除元素 3.获取第i个结点的数据元素 4.判断元素是否存在 5.获取某个元素的下一个元素;C语言
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
// 插入元素
void insert(struct node **head, int x){
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = x;
new_node->next = *head;
*head = new_node;
printf("Insert %d successfully.\n", x);
}
// 删除元素
void delete(struct node **head, int x){
struct node *cur = *head;
struct node *prev = NULL;
while(cur != NULL){
if(cur->data == x){
if(prev == NULL){
*head = cur->next;
} else{
prev->next = cur->next;
}
free(cur);
printf("Delete %d successfully.\n", x);
return;
}
prev = cur;
cur = cur->next;
}
printf("The element %d is not exist in the list.\n", x);
}
// 获取第i个结点的数据元素
int get(struct node *head, int index){
int i = 0;
struct node *cur = head;
while(cur != NULL){
if(i == index){
return cur->data;
}
i++;
cur = cur->next;
}
printf("The index %d is out of bounds.\n", index);
return -1;
}
// 判断元素是否存在
int has(struct node *head, int x){
struct node *cur = head;
while(cur != NULL){
if(cur->data == x){
printf("The element %d is exist in the list.\n", x);
return 1;
}
cur = cur->next;
}
printf("The element %d is not exist in the list.\n", x);
return 0;
}
// 获取某个元素的下一个元素
int getNext(struct node *head, int x){
struct node *cur = head;
while(cur != NULL){
if(cur->data == x){
if(cur->next == NULL){
printf("The element %d is the last one.\n", x);
return -1;
} else{
printf("The next element of %d is %d.\n", x, cur->next->data);
return cur->next->data;
}
}
cur = cur->next;
}
printf("The element %d is not exist in the list.\n", x);
return -1;
}
int main(){
struct node *head = NULL;
int x, index;
while(1){
printf("\nEnter the operation you want to do:\n");
printf("1. Insert an element\n");
printf("2. Delete an element\n");
printf("3. Get the i-th element\n");
printf("4. Judge whether an element exists\n");
printf("5. Get the next element of an element\n");
printf("0. Exit\n");
int op;
scanf("%d", &op);
switch(op){
case 0:
return 0;
case 1:
printf("\nEnter the element you want to insert:\n");
scanf("%d", &x);
insert(&head, x);
break;
case 2:
printf("\nEnter the element you want to delete:\n");
scanf("%d", &x);
delete(&head, x);
break;
case 3:
printf("\nEnter the index of the element you want to get:\n");
scanf("%d", &index);
x = get(head, index);
if(x != -1){
printf("The %d-th element is %d.\n", index, x);
}
break;
case 4:
printf("\nEnter the element you want to search:\n");
scanf("%d", &x);
has(head, x);
break;
case 5:
printf("\nEnter the element whose next you want to get:\n");
scanf("%d", &x);
getNext(head, x);
break;
default:
printf("\nInvalid operation.\n");
break;
}
}
return 0;
}
阅读全文