#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct LISTS
{
int datas;
LISTS *next;
}lists;
lists *creates_T(lists *head)
{
int i;
lists *p;
scanf("%d", &i);
head = (lists*) malloc(sizeof(lists));
head->next = NULL;
for (int j = 0; j < i; j++)
{
p = (lists*) malloc(sizeof(lists));
scanf("%d", &p->datas);
p->next = head->next;
head->next = p;
}
return head;
}
lists *creates_W(lists *head)
{
lists *p1, *p2;
int i;
head = (lists*) malloc(sizeof(lists));
p1 = head;
scanf("%d", &i);
for (int J = 0; J < i; J++)
{
p2 = (lists*) malloc(sizeof(lists));
scanf("%d", &p2->datas);
p1->next = p2;
p1 = p2;
}
p1->next = NULL;
return head;
}
void printfs(lists *head)
{
lists *p;
p = head->next;//因为是头插法,有头结点 但是头结点是不能要数据的 所以要next
//printf("打印\n");
while (p)
{
printf(" %d", p->datas);
p = p->next;
}
printf("\n");
}
void inserthead(lists *head)//插入到表头
{
lists *p;
p = (lists*) malloc(sizeof(lists));
p->datas = 5;
p->next = head->next; //头指针的作用
head->next = p;
}
void inserttaile(lists *head)//插入到表尾
{
lists *p1, *p2;
p2 = head;
p1 = (lists*) malloc(sizeof(lists));
p1->datas = 5;
while (p2->next != NULL)
{
p2 = p2->next;
}
p1->next = NULL;
p2->next = p1;
}
void insertother(lists *head)
{
int a, j = 0;
scanf("%d", &a);
lists *p1, *p2, *p3;
p1 = head;
p2 = (lists*) malloc(sizeof(lists));
p2->datas = 6;
while (j < a - 1)
{
p1 = p1->next;
j++;
}
p3 = p1->next;
p1->next = p2;
p2->next = p3;
}
void deletehead(lists *head)
{
lists *p;
p = head->next;
head->next = head->next->next;
free(p);
}
void deletetail(lists *head)
{
lists *p2;
while (head->next != NULL)
{
p2 = head;
head = head->next;
}
p2->next = NULL;
free(head);
}
void deleteother(lists *head)
{
int a, j;
scanf("%d", &a);
lists *p1, *p2;
p1 = head;
for (j = 0; j < a - 1; p1 != NULL)
{
j++;
p1 = p1->next;
}
p2 = p1->next;
p1->next = p2->next;
free(p2);
}
void deletesum(lists *head)
{
lists *p, *q;
p = head;
int a;
scanf("%d", &a);
while (p->next != NULL)
{
if (p->next->datas == a)
{
q = p->next;
p->next = p->next->next;
free(q);
}
else
p = p->next;
}
}
void repeat(lists *head)
{
lists *p, *q;
p = head;
while (p->next != NULL)
{
if (p->next->datas == p->datas)
{
q = p->next;
p->next = p->next->next;
free(q);
}
else
p = p->next;
}
}
void find(lists *head)
{
int j = 1;
int a;
lists *p;
p = head->next;
scanf("%d", &a);
while (p&&j < a)
{
p = p->next;
j++;
}
printf("%d\n", p->datas);
}
void select(lists *head)
{
int j = 0;
int a;
lists *p;
p = head->next;
scanf("%d", &a);
while (p != NULL)
{
j++;
if (p->datas == a)
printf("%d ", j);
p = p->next;
}
}
void update(lists *head)
{
int j = 1;
int a;
lists *p;
p = head->next;
scanf("%d", &a);
while (p&&j < a)
{
p = p->next;
j++;
}
p->datas = 222;
}
lists *join(lists *head, lists *head1, lists *head2)
{
lists *p = head1->next, *q = head2->next, *r = head;
while (p != NULL)
{
r->next = p;
r = p;
p = p->next;
}
while (q != NULL)
{
r->next = q;
r = q;
q = q->next;
}
return head;
}
void sort_M(lists *head)
{
lists *p, *q, *temp;
temp = (lists*) malloc(sizeof(lists));
for (p = head; p != NULL; p = p->next)
{
for (q = p->next; q != NULL; q = q->next)
{
if (p->datas > q->datas)
{
temp->datas = q->datas;
q->datas = p->datas;
p->datas = temp->datas;
}
}
}
}
void compare(lists *head1, lists *head2)
{
lists *a,*b;
a = head1->next;
b = head2->next;
int array [10];
int flags=0;
while (a!= NULL&&b!=NULL)
{
if (a->datas == b->datas)
{
flags++;
printf("位置是--->%d\n", flags);
}
else
exit(1);
b = b->next;
}
}
int main()
{
lists *head1 = (lists*) malloc(sizeof(lists));
lists *head2 = (lists*) malloc(sizeof(lists));
lists *head = (lists*) malloc(sizeof(lists));
head1 = creates_W(head1);
//printf("1--->"); printfs(head1);
head2 = creates_W(head2);
//printf("2--->"); printfs(head2);
compare(head1,head2);
}
C语言 链表
最新推荐文章于 2024-08-22 23:45:07 发布