Winter vacation project 1-Dynamic Linked List experience (transformation) (4), winter vacation 1-

Source: Internet
Author: User

Winter vacation project 1-Dynamic Linked List experience (transformation) (4), winter vacation 1-

/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: test. cpp * Author: Liu Chang * completion date: January 22, 2015 * version No.: v1.0 ** Problem description: modifying the linked list in the blog example. * Input Description: Enter n (n is the node data ). * Program output: output dynamic linked list


(4) Compile the delete_node (int x) function to delete the node with the node value of x.

# Include <iostream> # include <cstdio> using namespace std; struct Node {int data; // the data of the Node struct Node * next; // points to the next Node }; node * head = NULL; // define the head of the linked list as a global variable for subsequent operations void make_list2 (); // create the void out_list (); // output linked list void delete_node (int x); // Delete the x node int main () {int x; cin> x; make_list2 (); out_list (); delete_node (x); out_list (); return 0;} void make_list2 () {int n; Node * p, * q; // p is used to point to the new Node, q points to cout at the end of the linked list <"enter a number of positive numbers (0 or a negative number) create a linked list: "<endl; cin> n; while (n> 0) {p = new Node; // create a new node p-> data = n; // put n into the data member of the new node p-> next = NULL; // set the pointer P to next to NULL if (head = NULL) head = p; else q-> next = p; q = p; // link p to the end of the table. cin >>n;} return ;}void out_list () {Node * p = head; cout <"the data in the linked list is: "<endl; while (p! = NULL) {cout <p-> data <"; p = p-> next;} cout <endl; return;} void delete_node (int x) {Node * p, * q; if (head = NULL) cout <"Empty linked list, cannot be deleted. "<Endl; else {while (head! = NULL & head-> data = x) {p = head; head = head-> next; delete p;} if (head! = NULL) {p = head; q = p-> next; while (q! = NULL) {if (q-> data = x) {p-> next = q-> next; delete q;} else {p = q ;} q = p-> next ;}}cout <"The linked list has been updated... "<endl;} return ;}

Running result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.