SDUT1138数据结构上机测试2-1:单链表操作A
2016-07-21 18:35
543 查看
此题坑点在对末尾节点的操作,避免指针越界
#include<bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }*head,*tail,*q,*p; int n,key; void built() { 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); tail->next=p; tail=p; } tail->next=NULL; } void print() { p=head->next; while(p) { printf("%d",p->data); if(p->next) printf(" "); p=p->next; } } void del() { p=head; while(p->next) { if(p->next->data==key) { if(p->next->next==NULL) { p->next=NULL; n--; break; } p->next=p->next->next; n--; } p=p->next; } } int main() { scanf("%d",&n); built(); scanf("%d",&key); printf("%d\n",n); print(); del(); printf("\n%d\n",n); print(); }
相关文章推荐
- 数据结构实验之数组一:矩阵转置
- SDUT1130数据结构上机测试1:顺序表的应用(上一个代码太蠢)
- 评 严蔚敏《数据结构》中的KMP算法
- 常见算法与数据结构整理
- 《数据结构》2.3单链表(single linked list)
- SDUTOJ 2125 数据结构实验之串二:字符串匹配
- javascript 引用类型 - Array与数据结构和重排序
- 数据结构实验之排序六:希尔排序
- Redis系统学习 二、数据结构
- Trie树数组版
- 浙大PAT-Sort with Swap(0, i)
- 队列——数据结构
- 数据结构与算法简记:非递归遍历二叉树
- 包含min函数的栈
- Codeforces548D Mike and Feet 数据结构+单调栈+优先队列
- CF620C - Pearls in a Row 数据结构map映射
- HDU 5233 Gunner II 数据结构map+vector
- 数据结构---------二叉搜索树
- 1.16 从大到小顺序返回x,y和z的值
- java实现栈的数据结构