Data Structure on-line Testing 2-1: Single Link List Operation A
Time Limit: 1000 ms Memory Limit: 4096 KiB
Problem Description
Input n integers, first establish a single linked list of leading nodes according to the order of data input, then input a data m, delete all the nodes whose value is m in the single linked list. Output the initial single linked list and deleted single linked list respectively.
Input
Number of input data n in the first line;
The second line enters n integers in turn.
The third line enters the data m to be deleted.
Output
The first line outputs the length of the original single-linked list.
The second row outputs the data of the original single-linked list in turn.
The third line outputs the length of single chain table after deletion.
The fourth row outputs the deleted single linked list data in turn.
Sample Input
10
56 25 12 33 66 54 7 12 33 12
12
Sample Output
10
56 25 12 33 66 54 7 12 33 12
7
56 25 33 66 54 7 33
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node*next; }; int main() { int n,m,q; scanf("%d",&n); struct node *head,*p,*tail,*t,*f,*j; head=(struct node*)malloc(sizeof(struct node)); head->next=NULL; tail=head; for(int i=0; i<n; i++) { p=(struct node*)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next = NULL; tail->next = p; tail=p; } scanf("%d",&m); q=n; printf("%d\n",n); for(p=head->next; p!=NULL; p=p->next) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); } p=head;//Pointing Node j=p->next;//j== next of the header node while(j){ if(j->data==m) { q--; //count p->next=j->next;//p pointer (deleting a node at a node) === deleting j pointer at a node free(j); j=p->next;//After release, j redirects to the next node } else { p=p->next; j=j->next; } } printf("%d\n",q); for(p=head->next; p!=NULL; p=p->next) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); } }