链表

#include<stdlib.h>
#include<stdio.h>
typedef struct NODE
{
 struct NODE *next;
 int length;
 int date;
}node;
typedef struct NODE *list;
int initlist(list *head)
{
 *head=(list)malloc(sizeof(list));
 (*head)->next=NULL;
 (*head)->length=0;
 return 1;
}
int crelist(list h,int n)
{
 printf("输入数据i:");
 for(int i=0;i<n;i++)
 {
  node *p;
  p=(node *)malloc(sizeof(node));
  scanf("%d",&p->date);
  p->next=h->next;
  h->next=p;
  h->length++;
 }
 return 1;
}
int show(list r)
{
 node *p;
 p=(node *)malloc(sizeof(node));
 p=r->next;
 printf("length=%d ",r->length);
 while(p)
 {
  printf("%d ",p->date);
  p=p->next;
 }
 printf(" ");
 return 0;
}
node *found(list h,int n)
{
  node *p;
 p=(node *)malloc(sizeof(node));
 p=h;
 int j=0;
 while(p!=NULL&&j<n)
 {
  p=p->next;j++;
 }
 return p;
}
int insertlist(list &h,int w0,int s)
{
 if(w0<1||w0>h->length+1) return 0;
 node *p,*q;
 p=(node *)malloc(sizeof(node));
 q=(node *)malloc(sizeof(node));
 p=found(h,h->length-w0+1);
 q->date=s;
 q->next=p->next;
 p->next=q;
 h->length++;
 return 1;
}
int dele(list &r,int a)
{
 if(a<1||a>r->length) return 0;
 node *p;
 p=(node *)malloc(sizeof(node));
 p=found(r,r->length-a);
 p->next=p->next->next;
 r->length--;
 return 1;
}
int main()
{
 list w;
 initlist(&w);
 int n,date;
 printf("输入数据个数:");
 scanf("%d",&n);
 crelist(w,n);
 show(w);
 int wi,s;
 printf("输入插入数据位置和数据:");
 scanf("%d%d",&wi,&s);
 insertlist(w,wi,s);
 show(w);
 int a;
 printf("输入删除数据位置:");
 scanf("%d",&a);
 dele(w,a);
 show(w);
}
原文地址:https://www.cnblogs.com/jarrem/p/4888936.html