链表:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct Node //链表节点
{
int data;
struct Node* next;
};
typedef struct Node node ;
int prt(node *); //声明
struct Node* create(node **pt) //创建链表函数
{
int n;
int m=0;
printf("请输入你想创建的链表结点个数\n");
scanf("%d",&n);
node *head;
head = (node *)malloc(sizeof(node));
head->data = 0;
head->next = NULL;
node *p;
node *q;
q = head;
while(m != n)
{
int da_ta;
m++;
p = (node *)malloc(sizeof(node));
printf("请输入您创建的第%d个链表中的数据\n",m);
scanf("%d",&da_ta);
p->data = da_ta;
p->next = NULL;
q->next = p;
q = q->next;
}
p->next = NULL;
prt(head);
*pt = head;
return head;
}
int insert(node *head) //插入函数
{
node *p;
node *q;
int n;
int num;
printf("请输入您想插入的位置中的数据");
scanf("%d",&num);
printf("请输入您想插入的链表数据");
scanf("%d",&n);
p = (node *)malloc(sizeof(node));
q = head;
while(q->data != num)
{
if(q->next == NULL)
{
printf("没有找到您想插入数据内容\n");
return 1;
exit(1);
}
q = q->next;
}
p->next = q->next;
q->next = p;
p->data = n;
return 0;
}
int prt(node *head) //链表遍历输出函数
{
printf("正在输出链表中内容...\n");
node *q;
q = head;
while(q->next != NULL)
{
printf("%d",q->data);
q = q->next;
}
printf("%d",q->data);
printf("\n");
return 0;
}
int del(node *head) //链表节点删除
{
int num;
node *q;
node *p;
p = head->next;
q = head;
printf("请输入您想删除的位置中的数据");
scanf("%d",&num);
while(num != p->data)
{
if(p->next == NULL)
{
printf("没有找到您想删除数据内容\n");
return 1;
exit(1);
}
q = p;
p = p->next;
}
q->next = p->next;
free(p);
return 0;
}
void rev(node *head,node **hhead) //逆序
{
node *p;
node *q;
p = head->next;
q = head;
head->next = NULL;
while(p->next != NULL)
{
q = head;
head = p;
p = p->next;
head->next =q;
}
q = head;
head = p;
head->next = q;
*hhead = head;
}
void main()
{
node *Head; //创建头指针
create(&Head);
//insert(Head);
//del(Head);
/* //prt(Head);
rev(Head,&Head);
prt(Head);
rev(Head,&Head);
prt(Head);
*/
}