SDUT 1138 单链表操作A

来源:互联网 发布:开根号的算法 编辑:程序博客网 时间:2024/06/05 20:42

点击打开链接

#include <bits/stdc++.h>using namespace std;int n, m;struct node{    int data;    node *next;};node *head, *tail, *p;node *creat(int n);void print(node *head);node *del(node *head, int m);int main(){    cin >> n;    head = creat(n);    print(head);    cin >> m;    head = del(head, m);    print(head);    return 0;}node *creat(int n){    head = new node;    tail = head;    while(n --)    {        p = new node;        cin >> p -> data;        tail -> next = p;        tail = p;    }    tail -> next = NULL;    return head;}node *del(node *head, int m){    node *q;    p = head -> next;    tail = head;    while(p)    {        if(p -> data == m)        {            tail -> next = p -> next;            p = tail -> next;            n--;        }        else        {            tail = tail -> next;            p = p -> next;        }    }    return head;}void print(node *head){    cout << n << endl;    p = head -> next;    while(p)    {      if(p -> next)      {          cout << p -> data << ' ';      }      else      {          cout << p -> data << endl;      }      p = p -> next;    }}


0 0
原创粉丝点击