线性表的链式存储
来源:互联网 发布:索尼微单 知乎 编辑:程序博客网 时间:2024/04/30 03:22
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#define N 500010
#define INF 10000000
#define LL long long
#define eps 10E-9
#define mem(a) memset(a,0,sizeof(a))
#define w(a) while(a)
#define s(a) scanf("%d",&a)
#define ss(a,b) scanf("%d%d",&a,&b)
#define sss(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
using namespace std;
typedef struct node
{
int data;
struct node *next;
} Node;
void create(Node **p)
{
*p=NULL;
}
int my_insert(Node **p, int x)
{
Node *p1;
p1 = (Node *) malloc(sizeof (Node));
if (p1 == NULL)
return false;
Node *p2=*p;
while(p2 != NULL)
{
if (p2->data == x)
break;
p2= p2->next;
}
p1->data = x;
p1->next = *p;
*p = p1;
return true;
}
int my_delete(Node **head, int x)
{
Node *p=*head, *q;
if (p->data == x)//考虑头结点就是要删除的元素
{
*head = (*head)->next;
free(p);
return true;
}
else
{
q = p; p = p->next; //q指向前一个节点,p指向下一个节点
while(p != NULL)
{
if (p->data == x)
{
q->next = p->next;
free(p);
return true;
}
q = p; p = p->next;
}
}
return false;
}
int my_find(Node **head, int x)
{
Node *p=*head;
while(p != NULL)
{
if (p->data == x)
break;
p = p->next;
}
return p->data;
}
void my_clear(Node **head)
{
Node *p=*head,*q;
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
}
void my_showalldata(Node **head)
{
Node *p=*head,*q;
while (p != NULL)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main()
{
int x, n;
Node *head;
create(&head);
cout<<"input the count(n) of data :"<<endl;
cin>>n;
cout<<"input the n'data:"<<endl;
while(n--){
cin>>x;
my_insert(&head,x);
}
cout<<"this is all the data of the table:"<<endl;
my_showalldata(&head);
cout<<"input the data you want to delete:"<<endl;
cin>>x;
my_delete(&head,x);
my_showalldata(&head);
cout<<"input the data you want to find:"<<endl;
cin>>x;
cout<<"the data you want find is:"<<my_find(&head,x)<<endl;
// my_clear(&head);// formating
free(head);
return 0;
}
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#define N 500010
#define INF 10000000
#define LL long long
#define eps 10E-9
#define mem(a) memset(a,0,sizeof(a))
#define w(a) while(a)
#define s(a) scanf("%d",&a)
#define ss(a,b) scanf("%d%d",&a,&b)
#define sss(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
#define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
using namespace std;
typedef struct node
{
int data;
struct node *next;
} Node;
void create(Node **p)
{
*p=NULL;
}
int my_insert(Node **p, int x)
{
Node *p1;
p1 = (Node *) malloc(sizeof (Node));
if (p1 == NULL)
return false;
Node *p2=*p;
while(p2 != NULL)
{
if (p2->data == x)
break;
p2= p2->next;
}
p1->data = x;
p1->next = *p;
*p = p1;
return true;
}
int my_delete(Node **head, int x)
{
Node *p=*head, *q;
if (p->data == x)//考虑头结点就是要删除的元素
{
*head = (*head)->next;
free(p);
return true;
}
else
{
q = p; p = p->next; //q指向前一个节点,p指向下一个节点
while(p != NULL)
{
if (p->data == x)
{
q->next = p->next;
free(p);
return true;
}
q = p; p = p->next;
}
}
return false;
}
int my_find(Node **head, int x)
{
Node *p=*head;
while(p != NULL)
{
if (p->data == x)
break;
p = p->next;
}
return p->data;
}
void my_clear(Node **head)
{
Node *p=*head,*q;
while (p != NULL)
{
q = p;
p = p->next;
free(q);
}
}
void my_showalldata(Node **head)
{
Node *p=*head,*q;
while (p != NULL)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
int main()
{
int x, n;
Node *head;
create(&head);
cout<<"input the count(n) of data :"<<endl;
cin>>n;
cout<<"input the n'data:"<<endl;
while(n--){
cin>>x;
my_insert(&head,x);
}
cout<<"this is all the data of the table:"<<endl;
my_showalldata(&head);
cout<<"input the data you want to delete:"<<endl;
cin>>x;
my_delete(&head,x);
my_showalldata(&head);
cout<<"input the data you want to find:"<<endl;
cin>>x;
cout<<"the data you want find is:"<<my_find(&head,x)<<endl;
// my_clear(&head);// formating
free(head);
return 0;
}
1 0
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 《线性表的链式存储》
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 线性表的链式存储
- 最近一段时间的感想
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- [BZOJ1430] 小猴打架
- Apple Watch -- 作为游戏开发者的你准备好了么?
- 深入理解Magento – 第一章 – Magento强大的配置系统
- 线性表的链式存储
- 从layout到extent的演变史(一)
- PL/SQL基础(二)
- 关于MongDB 与 Spring 整合的一点问题
- 列出对像属性,for(var i in obj)[转]
- 静态编译与动态编译的区别
- 无限极分类
- C#单例模式的三种写法
- 定时器和多线程的区别和联系