KiKi学习了结构体和指针,他了解了结构体类型可以定义包含多个不同类型成员,而指针本质是内存地址,是引用数据的另外一种方式。现在他想将多个输入的数据通过结构体和指针的方式连接在一起,形成一个单向链表,即:每个结点是结构体类型,包括整型数据成员(data)和结构体指针类型成员(next),每个结点的指针部分指向下一个输入的结点。具体建立过程如下:先输入n个整数,按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除。输出删除后的单链表信息。
包括三行:第一行输入数据个数n (3≤n≤100);第二行依次输入n个整数,用空格分隔;
第三行输入欲删除数据m。
包括两行:第一行输出完成删除后的单链表长度;
第二行依次输出完成删除后的单链表数据。
5 1 2 2 3 4 2
3 1 3 4
#include <iostream> #include <vector> class forward_list { public: forward_list() { createHead(); } void createHead();//创建头节点 void insert(int);//插入 void erase(int);//删除指定元素 void reverse_print();//打印元素 ~forward_list() { delete head; } int length = 0; private: //节点结构 typedef struct Node { Node(int v = 0) :value(v), next(NULL) {} int value; Node* next; }; std::vector<int> temp; Node* head;//头结点 }; //创建头结点 void forward_list::createHead() { head = new Node(0); } void forward_list::insert(int value)//插入 { //头插法 Node* p = new Node(value); p->next = head->next; head->next = p; length--; //第二轮 head p q //此时p->next == nullptr //head->next = p //Node* q = new Node(value); //q->next = head->next; //head->next = q; } void forward_list::erase(int target)//删除指定元素 { Node* temp_ptr; temp_ptr = head->next; while (temp_ptr !=NULL) { if (temp_ptr->value != target) { temp.push_back(temp_ptr->value); } temp_ptr = temp_ptr->next; } } void forward_list::reverse_print()//打印元素 { std::cout << temp.size() << std::endl; for (std::vector<int>::reverse_iterator iter = temp.rbegin(); iter != temp.rend(); ++iter) { std::cout << *iter << " "; } std::cout << std::endl; } int main() { int deleteNum; forward_list list; std::cin >> list.length; int temp_value; while (std::cin >> temp_value) { list.insert(temp_value); if (!list.length) break; } std::cin >> deleteNum; list.erase(deleteNum); list.reverse_print(); }
#include<bits/stdc++.h> using namespace std; int main(){ list<int> l; int n; cin >> n; for(int i = 0; i < n; i++){ int num; cin >> num; l.push_back(num); } int num; cin >> num; list<int>::iterator it = l.begin(); list<int>::iterator p = l.begin(); while(it != l.end()){ if(*it == num){ p = it; it++; l.erase(p); continue; } it++; } cout << l.size() << endl; for(auto i : l) cout << i << " "; cout << endl; return 0; }
import java.util.ArrayList; import java.util.Scanner; public class Main{ public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); //输入需要输入数字个总个数 int n = sc.nextInt(); for (int i = 0; i < n; i++) { list.add(sc.nextInt()); } //输入删除的数字 int m = sc.nextInt(); //循环遍历集合中的数字 for (int index = 0; index < list.size(); ) { if(m == list.get(index)){ list.remove(list.get(index)); //如果集合里面有和输入的数字相同就减去。集合索引不增加 }else{ //如果集合里面没有相同数字,不进行操作,增加索引值。 index++; } } //打印输出删除后集合的长度 System.out.println(list.size()); //将集合转换为数组,用于遍历 Object[] obj = list.toArray(); for (int i = 0; i < obj.length; i++) { if(i != obj.length){ System.out.print(obj[i]+" "); }else{ System.out.print(obj[i]); } } } }
#include<stdio.h> #include<stdlib.h> #define ElemType int //创建节点 typedef struct Node{ ElemType data; struct Node *next; }Node; typedef struct Node *LinkList; int main(void) { ElemType i,n,k,m,t; LinkList p,r,L,q,s; //创建指定长度的链表 scanf("%d",&n); t=n; L=(LinkList)malloc(sizeof(Node)); r=L; for(i=0;i<n;i++) { scanf("%d",&k); p=(LinkList)malloc(sizeof(Node)); p->data=k; r->next=p; r=p; } r->next=NULL; scanf("%d",&m); //删除指定元素 s=L; q=L->next; while(q) { if(q->data==m) { s->next=q->next; free(q); q=s->next; --t; } else{ s=s->next; q=q->next; } } p=L->next; //遍历输出 printf("%d\n",t); for(i=0;i<t;i++) { printf("%d ",p->data); p=p->next; } return 0; }
//手写单链表 import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); Node head = null; Node tail = null; for (int i = 0; i < n; i++) { int data = scanner.nextInt(); Node newNode = new Node(data); if (head == null) { head = tail = newNode; } else { tail = tail.next = newNode; } } int delete = scanner.nextInt(); Node nodeP = head; Node nodeBefore = null; while (nodeP != null) { if (nodeP.data == delete) { if (nodeP == head) { nodeP = head = head.next; } else { nodeBefore.next = nodeP.next; nodeP = nodeP.next; } n--; } else { nodeBefore = nodeP; nodeP=nodeP.next; } } System.out.println(n); for (Node node = head; node != null; node = node.next) { System.out.print(node.data + " "); } } } class Node { public int data; public Node next; public Node(int data) { this.data = data; } }
package com.kingwan.niuke; import java.util.Iterator; import java.util.LinkedList; import java.util.Scanner; /** * Created by kingwan on 2020/10/9. * 说明:链表的迭代方式 */ public class Main { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int n = scanner.nextInt(); LinkedList<Integer> list = new LinkedList(); //填充链表 while (n>0){ list.add(scanner.nextInt()); n--; } int del = scanner.nextInt(); //使用迭代器迭代--注意:链表不是数组,使用for循环会抛异常的 Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ if(iterator.next()==del){ iterator.remove();//符合条件的删除 } } //按要求输出 System.out.println(list.size()); for (Integer integer : list) { if(integer!=del) System.out.print(integer+" "); } } }
#include<bits/stdc++.h> using namespace std; struct Node { int val; Node* next = nullptr; Node(int v) :val(v) { } }; int main() { Node* root = new Node(0); int n, a; cin >> n; Node* p = root; for(int i=0;i<n;i++) { cin >> a; p->next = new Node(a); p = p->next; } int del; cin >> del; p = root; while(p->next!=nullptr) { if(p->next->val == del) { p->next = p->next->next; n--; continue; } p = p->next; } cout << n << endl; for(p=root->next;p;p=p->next) cout << p->val << ' '; return 0; }
readline(); var arr = readline().split(' ').map(Number); var del = readline(); arr=arr.filter((item)=>{ return item!=del; } ); console.log(arr.length); var result =''; for(var i =0;i<arr.length;i++){ result += arr[i]+' '; } console.log(result);
#include <stdio.h> #include <stdlib.h> typedef struct linklist { struct linklist *next; int data; }LNode; int main() { LNode *L = (LNode *)malloc(sizeof(LNode)), *p, *pre; L->next = NULL; pre = L; int n, i; scanf("%d", &n); for(i = 0; i < n; i++) { p = (LNode *)malloc(sizeof(LNode)); scanf("%d", &(p->data)); pre->next = p ; p->next = NULL; pre = p ; } int del, del_count = 0; scanf("%d", &del); p = L->next;pre = L; while(p) { if(p->data == del) { pre->next = p->next; free(p); p = pre->next; del_count++; } else { pre = p; p = p->next; } } printf("%d\n", n - del_count); p = L->next; printf("%d", p->data); p = p->next; while(p) { printf(" %d", p->data); p = p->next; } putchar(10); pre = L;p = L->next; while(p) { free(pre); pre = p; p = p->next; } free(pre); return 0; }
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; int main() { int n = 0; struct Node* list = NULL; struct Node* tail = NULL; //输入结点 int i = 0; scanf("%d", &n); for (i = 0; i < n; i++) { int data = 0; scanf("%d", &data); struct Node* n = (struct Node*)malloc(sizeof(struct Node)); n->data = data; n->next = NULL; if (list == NULL) { list = n; tail = list; } else { tail->next = n; tail = tail->next; } } //删除结点 int del = 0; scanf("%d", &del); struct Node* cur = list; struct Node* prev = NULL; while (cur) { if (cur->data == del) { struct Node* pd = cur; //如果删除的是第一个结点 if (cur == list) { list = list->next; cur = list; } else { //删除的不是第一个结点 prev->next = cur->next; cur = prev->next; } n--; free(pd); } else { //找不到 prev = cur; cur = cur->next; } } cur = list; printf("%d\n", n); while (cur) { printf("%d ", cur->data); cur = cur->next; } //释放空间 cur = list; while (cur) { del = cur; cur = cur->next; free(del); } return 0; }
#include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } node; node* CreateNode(int x) { node* tmp = (node*)malloc(sizeof(node)); tmp->data = x; tmp->next = NULL; return tmp; } int main() { int n, m, i; scanf("%d", &n); node* list = NULL, *tail = NULL; while (n--) { scanf("%d", &i); node* cur = CreateNode(i); if (list == NULL) { list = cur; tail = list; } else { tail->next = cur; tail = tail->next; } } scanf("%d", &m); node* head = list; node* prev = NULL; int num = 0; while (list) { if (list->data == m) { node* tmp = list; if (prev != NULL) { prev->next = list->next; } else { head = list->next; } list = list->next; free(tmp); } else { prev = list; list = list->next; num++; } } list = head; printf("%d\n", num); while (list) { printf("%d ", list->data); list = list->next; } return 0; }
#include <stdio.h> #include <stdlib.h> typedef int SLDataType; //创建结构体 typedef struct Node { SLDataType data; struct Node *next; }Node; //初始化链表,返回头节点指针 Node* init_link_list() { Node* head = (Node*) malloc(sizeof(Node)); //初始化头节点指针指向NULL head->next = NULL; return head; } //在链表末尾插入节点 void insert_node(Node* head,int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; Node* p = head; while (p->next != NULL) { p = p->next; } p->next = newNode; } // 删除链表中所有值为target的节点 void delete_node(Node* head,int target) { Node* prev = head; Node* cur = head->next; while(cur != NULL) { if(cur->data == target) { prev->next = cur->next; free(cur); cur = prev->next; } else { prev = cur; cur = cur->next; } } } //打印链表 void print_link_list(Node* head) { Node* p = head->next; //跳过头节点head while(p != NULL) { printf("%d ",p->data); p = p->next; } printf("\n"); } //释放链表空间 void free_link_list(Node* head) { Node* p = head; while(p != NULL) { Node* tmp = p; p = p->next; free(tmp); } } int main() { int n = 0; scanf("%d", &n); // 初始化链表 Node *head = init_link_list(); // 输入数据建立链表 for (int i = 0; i < n; ++i) { SLDataType data; scanf("%d", &data); insert_node(head, data); } // 输入要删除的数据m int m = 0; scanf("%d", &m); // 删除链表中所有值为m的节点 delete_node(head, m); // 计算删除后的链表长度并输出 int length = 0; Node *p = head->next; // 跳过头结点 while (p != NULL) { length++; p = p->next; } printf("%d\n", length); // 输出删除数据后的链表 print_link_list(head); // 释放链表空间 free_link_list(head); return 0; }
#include <stdio.h> int main() { int n=0; scanf("%d",&n); int arr[100]; int i=0; for(i=0;i<n;i++) { scanf("%d ",&arr[i]); } int k=0; scanf("%d",&k); int count=0; for(i=0;i<n;i++) { if(arr[i]!=k) { count++; } } printf("%d\n",count); for(i=0;i<n;i++) { if(arr[i]!=k) { printf("%d ",arr[i]); } } return 0; }
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <assert.h> #include <stdbool.h> typedef struct list { int data; struct list* next; } list; list* find(list* phead, int x) { list* cur = phead->next; while (cur->data != x && cur != NULL) { cur = cur->next; } return cur; } list* get_newnode(int x) { list* newnode = (list*)malloc(sizeof(list)); assert(newnode); newnode->data = x; newnode->next = NULL; return newnode; } void listPushBack(list* phead, int x) { list* newnode = get_newnode(x); assert(newnode); list* cur = phead; while (cur -> next != NULL) { cur = cur->next; } cur->next = newnode; } void listpop(list* phead, list* pos) { assert(phead); list* cur = phead->next; list* prev = phead; list* back = pos->next; while (cur != pos) { prev = cur; cur = cur->next; } prev->next = back; free(pos); pos = NULL; } void Print(list* phead) { list* cur = phead -> next; while (cur != NULL) { printf("%d ", cur->data); cur = cur->next; } } int main() { list* a = (list*)malloc(sizeof(list)); assert(a); a->data = 0; a->next = NULL; int n = 0; scanf("%d", &n); int i = 0, tmp = 0; for (i = 0; i < n; i++) { scanf("%d", &tmp); listPushBack(a, tmp); } int x = 0; scanf("%d", &x); list* pos = find(a, x); while (pos != NULL) { listpop(a, pos); pos = find(a, x); n--; } printf("%d\n", n); Print(a); return 0; }
import java.util.Scanner; import java.util.LinkedList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 class LinkNode { public int val; public LinkNode next; public LinkNode (int val) { this.val = val; } public LinkNode(int val, LinkNode next) { this.val = val; this.next = next; } } class MyLinkedList { public LinkNode head; public LinkNode last; public Integer size = 0; public void addList(int val) { if (head == null) { this.head = new LinkNode(val); this.last = head; this.size = 1; return; } else { LinkNode cur = new LinkNode(val); this.last.next = cur; this.last = cur; this.size++; } } public void removeAll(int key) { if (this.head == null) return; LinkNode cur = this.head.next; LinkNode prev = this.head; while (cur != null) { if (cur.val == key) { prev.next = cur.next; this.size--; } else { prev = cur; } cur = cur.next; } if (this.head.val == key) { this.head = this.head.next; this.size--; } } public void display() { LinkNode cur = this.head; while (cur != null) { System.out.printf("%d ", cur.val); cur = cur.next; } } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int n = in.nextInt(); MyLinkedList list = new MyLinkedList(); for (int i = 0; i < n; i++) { list.addList(in.nextInt()); } list.removeAll(in.nextInt()); System.out.println(list.size); list.display(); } }