1.
堆区:1、借助队列的思想,先进先出,内存申请从小地址到大地址
2、大小一般是几G
3、内存手动申请手动释放
栈区:1.借助栈的思想,先进后出,内存申请从大地址到小地址
2.大小一般是几G
3、内存计算机自动申请自动释放
2.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int rem(int *p,int key,int len)
{
int count=0;
for(int i=0;i<len;i++)
{
if(*(p+i)==key)
{
for(int j=i;j<len;j++)
{
*(p+j)=*(p+j+1);
}
count++;
i--;
len--;
}
}
if(count==0)
{
puts("not found");
}
return len;
}
int main(int argc, const char *argv[])
{
int n;
printf("enter arrsize:");
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
printf("enter no.%d element:",i+1);
scanf("%d",&arr[i]);
}
int key;
printf("enter key:");
scanf("%d",&key);
int len=rem(arr,key,n);
for(int i=0;i<len;i++)
{
printf("%-5d",arr[i]);
}
return 0;
}
结果
3.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int datatype;
typedef struct list
{
datatype data;
struct list* next;
}*Linklist;
Linklist create()
{
Linklist p=(Linklist)malloc(sizeof(struct list));
if(p==NULL)
return NULL;
p->next=NULL;
p->data=0;
return p;
}
Linklist insert_head(Linklist head,datatype element)
{
Linklist s=create();
s->data=element;
if(head==NULL)
{
head=s;
return head;
}
s->next=head;
head=s;
return head;
}
void output(Linklist head)
{
if(head==NULL)
return;
Linklist p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
}
Linklist insert_rear(Linklist head,datatype element)
{
Linklist s=create();
s->data=element;
Linklist p=head;
if(head==NULL)
{
head=s;
return head;
}
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
return head;
}
Linklist del_head(Linklist head)
{
if(head==NULL)
return head;
Linklist p=head;
head=head->next;
free(p);
p=NULL;
return head;
}
Linklist del_rear(Linklist head)
{
if(head==NULL)
return head;
Linklist p=head;
if(head->next==NULL)
{
free(head);
head=NULL;
return head;
}
while(p->next->next!=NULL)
{
p=p->next;
}
free(p->next);
p->next=NULL;
return head;
}
int main(int argc, const char *argv[])
{
int n=6;
Linklist head=NULL;
datatype element;
for(int i=0;i<n;i++)
{
printf("enter element:");
scanf("%d",&element);
head=insert_head(head,element);
//head=insert_rear(head,element);
}
head=del_head(head);
head=del_rear(head);
output(head);
puts("");
return 0;
}