(3) Write the function delete_first_node () to delete the first node in the list.
#include <iostream>using namespace std;struct node{int data; Node data struct node *next; Point to next node}; Node *head=null; Define the list header as a global variable, so that you can manipulate void make_list () later; Establish a linked list void out_list (); Output list void Delete_first_node (); int main () {make_list (); Out_list (); Delete_first_node (); Out_list (); return 0;} void Make_list () {int n; Node *p,*q; cout<< "Enter several positive numbers (ending with 0 or a negative number) to create a linked list:"; cin>>n; while (n>0)//Enter several positive numbers to create a linked list, enter a non-positive number when the build process ends {p=new Node; New node p->data=n;. p->next=null; if (head==null) head=p; Place the node of the first entered number at the end of the list else q->next=p; Q=p; cin>>n; Enter next number, prepare to create next node} return; void Out_list () {Node *p=head; cout<< "The data in the list is:" <<endl; while (p!=null) {cout<<p->data<< ""; p=p->next; } cout<<endl; return;} void Delete_first_node () {node *p=head; if (p!=null)//Delete first Node { Head = p->next; Delete p; } cout<< "after delete" <<endl; return;}
Operation Result:
Summary of Knowledge points:
After deleting a node, output the node value to zero directly.
Learning experience:
Good study Day Day up
Academic leave Project 1-Dynamic linked list Experience 3