数据结构上机测试2-1:单链表操作A
10 56 25 12 33 66 54 7 12 33 12 12
10 56 25 12 33 66 54 7 12 33 12 7 56 25 33 66 54 7 33
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *creat(int n) { struct node *head,*tail,*p; int i; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; tail=head; for(i=0;i<n;i++) { p=(struct node *)malloc(sizeof(struct node)); scanf("%d",&p->data); p->next=NULL; tail->next=p; tail=p; } return head; } int del(struct node *head,int key,int n) { struct node *p,*q; int flag=0; q=head; p=head->next; while(p!=NULL) { if(p->data==key) { q->next=p->next; free(p); p=q->next; n--; } else { q=p; p=p->next; } } return n; } void show(struct node *head) { struct node *p; p=head->next; while(p!=NULL) { if(p->next!=NULL) printf("%d ",p->data); else printf("%d\n",p->data); p=p->next; } } int main() { int n,m,k; struct node *head; scanf("%d",&n); head=creat(n); scanf("%d",&m); printf("%d\n",n); show(head); m=del(head,m,n); printf("%d\n",m); show(head); return 0; }
数据结构上机测试2-1:单链表操作A (顺序建表+关键字删除)
原文:http://blog.csdn.net/u013486414/article/details/40662399