数据结构学习记录1-链表

//单链表:每个节点储存数据和指针指向下一个节点
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct list{
 int data;
 struct list *next;
}*head,*t;
int w;
//使用结构体储存链表的每一个结点
void createlist(){
 w=0;
 struct list *p,*q;
 int i,n,k;
 printf("please input:\n");
 scanf("%d",&n);
 head=NULL;//头指针指向空
 for(i=0;i<n;i++){
  w++;
  printf("input %d:\n",i+1);
  scanf("%d",&k);
  p=(struct list *)malloc(sizeof(struct list));
  p->data=k;
  p->next=NULL;
  if(head==NULL)
  head=p;
  else
  q->next=p;
  q=p;
 }
 printf("链表创建成功\n");
}
void addlist(struct list *head,int n){
 struct list *p,*q;
 t=head;
 for(int i=0;i<w;i++)
 {
  if(t==NULL||t->next->data>n){
   p=(struct list*)malloc(sizeof(struct list));
   p->data=n;
   p->next=t->next;
   t->next=p;
   break;
 }
  t=t->next;
 }
 t=head;
 while(t!=NULL){
  printf("%d\n",t->data);
  t=t->next;
 }
}
void deletelist(struct list *head,int n){
 struct list *p,*q;
 t=head;
 for(int i=0;i<w+1;i++)
 {
  if(t->next==NULL&&t->data!=n){
   printf("no list\n");
   return;
 }
 else if(t->next->data==n){
  p=t->next;
  t->next=p->next;
  free(p);
  break;
 }
 t=t->next;
 }
 t=head;
 while(t!=NULL){
  printf("%d\n",t->data);
  t=t->next;
 }
}
int main(){
 createlist();
 int a,b;
 scanf("%d",&a);
 addlist(head,a);
 scanf("%d",&b);
 deletelist(head,b);
 return 0;
}
//双向链表即加一个前面的指针*bef
//循环链表即输入最后一个数时t->next指向head
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值