单链表(2级指针)

#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<stdlib.h>

#define LEN sizeof(DataList_t)
#define N 10

typedef struct tagDataList_t
{
 int Data;
 struct tagDataList_t *next;
}DataList_t;

DataList_t * CreatList(int a[]);
void Print(DataList_t *pHead);
int Sereach(DataList_t * pHead,int n);
void Delete(DataList_t **pHead,int location);
void Insert(DataList_t **pHead,int location, int a[]);
void Reverse(DataList_t **pHead);
void Free(DataList_t * pHead);

void main(int argc, char *argv[])
{
 
 DataList_t *pHead;
 int a[N]={4,8,7,4,4,45,5,7,74,9};
 int location,number;
 pHead=CreatList(a);
 Print(pHead);
 printf("输入要查询的数字:");
 scanf("%d",&number);
 location=Sereach(pHead,number);
 if (location!=11)
 {
  printf("/n元素在链表的第%d个结点处/n",location);
 }  
    else
 {
  printf("/n所查数字不在其中/n");
 }
 Delete(&pHead,location);
 Insert(&pHead,location,a);
 Print(pHead);
 Reverse(&pHead);
 Print(pHead);
 Free(pHead);
}

DataList_t * CreatList(int a[])
{
 DataList_t *pHead = NULL, *p = NULL, *q = NULL;
 int i;
 if ((pHead = (DataList_t *)malloc(LEN))==NULL)
 {
  exit(1);
 }
 pHead->Data = a[0];
 pHead->next=NULL;
 q = pHead;

 for(i=1;i<N;i++)
 {
  p = (DataList_t *)malloc(LEN);
  p->Data=a[i];
  p->next=q->next;
  q->next=p;
  q=p;
 }
 return pHead;
}

void Print(DataList_t * pHead)
{
 DataList_t *p;
 assert(pHead!=NULL);
 printf("pHead");
 p=pHead;
 for(; p!=NULL; p=p->next)
 {
  printf("->%d",p->Data);
 }
 printf("/n");
}

void Delete(DataList_t **pHead,int location)
{
 DataList_t *p,*q;
 int i;
 p=*pHead;
 if (location==1)
 {
  *pHead = (*pHead)->next;//这样可以改变主函数的头结点
  free(p);
  return ;
 }
 else if(location>=11)
 {
  return ;
 }
 else
 {
  for(i=1; i<location-1; i++,p=p->next);
  q=p->next;
  p->next=p->next->next;
  free(q);
 }
}

int Sereach(DataList_t * pHead,int n)
{
 DataList_t *p;
 int i=1;
 assert(pHead!=NULL);
 p=pHead;
 while(p)
 {
  if(p->Data!=n)
  {
   p=p->next;
   i++;
  }
  else
  {
   break;
  }
  
 }
 return i;
}

void Insert(DataList_t **pHead,int location, int a[])
{
 int i;
 DataList_t *p,*q;
 p = *pHead;
 if((q=(DataList_t *)malloc(LEN))==NULL)
 {
  exit(1);
 }
 if(location!=10)
 {
  q->Data=a[location];
  q->next=NULL;
 }
 else
 {
  q->Data=a[location-1];
  q->next=NULL;
 }
 if(location==1)
 {
  q->next=*pHead;
  *pHead=q;
 }
 else if(location!=11&&location !=10)
 {
  for(i=1; i<location; i++,p=p->next);
  q->next=p->next;
  p->next=q;
 }
 else if(location==10)
 {
  for(i=1; i<location-1; i++,p=p->next);
  p->next=q;

 }
}

void Reverse(DataList_t **pHead)
{
 DataList_t *p, *q, *m;
 p = *pHead;
 m = p;
 p = p->next;
 m->next = NULL;
 while(p->next!=NULL)
 {
  q = p;
  p = p->next;
  q->next = m;
  m = q;
 }
 p->next = q;
 *pHead = p;
}

void Free(DataList_t * pHead)
{
 DataList_t *p;
 p=pHead->next;
 while (p)
 {
  pHead->next = p->next;
  free(p);
  p=pHead->next;
 }
 printf("/n释放成功!/n");
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用函数指针传递单链表,在C语言中,函数指针可以作为参数传递给其他函数,让函数能够调用指定的函数。 下面是一个示例代码,展示了如何使用函数指针传递单链表: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构体 struct Node { int data; struct Node* next; }; // 定义打印节点数据的函数 void printData(struct Node* node) { printf("%d ", node->data); } // 定义遍历链表的函数,接受一个函数指针作为参数 void traverse(struct Node* head, void (*func)(struct Node*)) { struct Node* current = head; while (current != NULL) { // 调用传入的函数指针 func(current); current = current->next; } } int main() { // 创建链表 struct Node* head = (struct Node*)malloc(sizeof(struct Node)); struct Node* second = (struct Node*)malloc(sizeof(struct Node)); struct Node* third = (struct Node*)malloc(sizeof(struct Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; // 使用函数指针遍历并打印链表数据 traverse(head, printData); // 释放内存 free(head); free(second); free(third); return 0; } ``` 在上述代码中,我们定义了一个 `printData` 函数来打印节点的数据,然后我们定义了一个 `traverse` 函数,该函数接受一个指向 `printData` 函数的函数指针作为参数。在 `traverse` 函数内部,我们通过调用传入的函数指针来打印节点的数据。 在 `main` 函数中,我们创建了一个包含三个节点的单链表,并通过调用 `traverse` 函数来遍历并打印链表的数据。 注意:在使用函数指针传递链表时,确保函数指针链表的节点类型匹配,以正确访问和操作链表的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值