结构体8

作者: 时间:2020-07-09 点击数:

#include <stdio.h>
#include <malloc.h>
struct node
   {
       int data;
    struct node *next;
   };
//建立n个数据节点的链表
void createlist(struct node **head,int n)
   {
       int i;
    struct node *p,*q;
    p=(struct node *)malloc(sizeof(struct node));   //头节点
    *head=q=p;     //把头节点的地址放到了head指向的单元中, head指向的单元是实参head的单元
    for(i=0;i<n;i++)
    {
            p=(struct node *)malloc(sizeof(struct node));
   scanf("%d",&p->data);  //从键盘输入一个整数到p->data
   q->next=p; q=p;
    }
    q->next=NULL;  //p->next=NULL;    
   }
//输出链表内容函数
void printlist(struct node *head)
{
//判断链表是否为空
//p指向第一个数据节点
 while (exp)  //exp表示节点是否存在
{
 //输出该节点数据;p=p->next;    NULL:0
}
struct node *p;
p=head;
if (p!=NULL)
{
 p=p->next;
 while (p->next!=NULL)
 {
  printf("%d->",p->data);
  p=p->next;
 }
 //单独处理一下最后一个节点
 printf("%d",p->data);
}
}
//把x插入到链表的第k个位置(0,1,2)
void insertx(struct node *p,int k,int x)
{
int i=0;
struct node *q;
q=(struct node *)malloc(sizeof(struct node));
q->data=x;
while (i<k)
{p=p->next;i++;}
//p指向了插入位置的前一个节点
q->next=p->next;
p->next=q;
}

上一页 [1] [2] [3]

Copyright© 2020 Anyang Institute of Technology.All rights reserved.

学院地址:河南省安阳市黄河大道西段