(1):链表之一
//环形链表
#include <stdio.h>
#include <stdlib.h>
struct student
{
int data;
struct student *next;
};
typedef struct student node;
typedef node* list;
int main(void)
{
list head,p1,p2;
int i;
head = (list)malloc(sizeof(node));
p1 = head;
p1->data = 1;
for(i=2;i<7;i++)
{
p2 = (list)malloc(sizeof(node));
p2->data = i;
p1->next = p2;
p2->next = NULL;
p1 = p2;
}
p2 = p1;
p1->next = head; //将尾节点指向头
do
{
printf("p1->data:%d\n",p1->data);
p1 = p1->next;
}while(p1 != p2);
p2 = head;
p1 = head;
do
{
p1 = p1->next;
free(p1);
}while(p2 != p1);-
return 0;
}
(2):链表之二双向链表
/*
双向链表:
data1-->data2-->data3-->NULL
NULL<--data1<--data2<--data3
*/
#include <stdio.h>
#include <stdlib.h>
struct student//存储数据
{
int data;
struct student *frount;
struct student *back;
};
struct node1//保存头尾节点
{
struct student *head;
struct student *list;
};
typedef struct student node;
typedef node* list;
typedef struct node1 Node;
void fun(Node * Node1,int len)//Node保存头为节点 len数据长度
{
int i;
list Frount;
list Back;
list ptr;
list ptr1;
ptr = (list)malloc(sizeof(node));//开辟第一个节点
if(ptr == NULL)
{
return ;
}
else
{
Frount = ptr;
ptr->frount = NULL;
ptr->back = NULL;
ptr->data = 1;
}
for(i=2;i<len;i++)//用循环开辟空间
{
ptr1 = (list)malloc(sizeof(node));
ptr1->data = i;
ptr->frount = ptr1;
ptr1->back = ptr;
ptr = ptr1;
}
ptr1->frount = NULL;
Back = ptr1;
Node1->head = Frount;
Node1->list = Back;
}
int main(void)
{
int len;
list Back = NULL;
list Frount = NULL;
Node Node1;
printf("输入数据长度:");
scanf("%d",&len);
fun(&Node1,len);
Back = Node1.list;
Frount = Node1.head;
//输出数据
while(Back != NULL)//反向输出
{
printf("Back-> back = %d\n",Back->data);
Back = Back->back;
}
while(Frount != NULL)//正向输出
{
printf("Frount -> frount = %d\n",Frount->data);
Frount = Frount->frount;
}
system("pause");
return 0;
}
(3):链表之三:将连接好的链表逆序
/*
将一条生成好的链表逆向 :
原链表:data1--->data2--->data3--->data4--->data5--->data6--->NULL
逆向后的链表: data6-->data5-->data4-->data3-->data2-->data1-->NULL;
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct llist
{
int num;
struct llist *next;
};
typedef struct llist node;
typedef node* llink;
int main()
{
llink p1,p2,p3,p4,p5;
llink last,mid;
llink head;
llink next;
//生成节点 p1---p5
p1 = (llink)malloc(sizeof(node));
p1->num = 1;
p2 = (llink)malloc(sizeof(node));
p2->num = 2;
p3 = (llink)malloc(sizeof(node));
p3->num = 3;
p4 = (llink)malloc(sizeof(node));
p4->num = 4;
p5 = (llink)malloc(sizeof(node));
p5->num = 5;
//将数据连接起来
p1->next = p2;
p2->next = p3;
p3->next = p4;
p4->next = p5;
p5->next = NULL;
head = p1;
while(head != NULL)//输出原链表的内容
{
printf("%d\n",head->num);
head = head->next;
}
printf("************\n");
head = p1;//头结点
mid = NULL;//mid是head前的节点
while(head != NULL)
{
last = mid; //last是mid前节点
mid = head;
head = head->next;//下一个节点
mid->next = last;//mid指向前节点last
}
//输出节点
while(mid != NULL)
{
printf("%d\n",mid->num);
next = mid;
mid = mid->next;
free(next);
}
system("pause");
return 0;
}
(4)链表之四:将两条链表合成一条链表:
/*
将两条链表合成一条链表:
一条链表:data1-->data2-->data3-->data4-->data5-->NULL;
另一条链表: temp1-->temp2-->temp3-->temp4-->temp5-->NULL;
合成后的链表:data1-->data2-->data3-->data4-->data5--> temp1-->temp2-->temp3-->temp4-->temp5-->NULL;
*/
#include <stdio.h>
#include <stdlib.h>
struct student
{
int number;
struct student *next;
};
typedef struct student node;
typedef node* list;
int main(void)
{
list head = (list)malloc(sizeof(list));
head->number = 1;
list pointer = (list)malloc(sizeof(list));
pointer->number = 5;
list head1 = head;
list pointer1 = pointer;
list next1;
int i;
for(i=2;i<5;i++)//第一条链表
{
next1 = (list)malloc(sizeof(list));
next1->number = i;
head1->next = next1;
head1 = next1;
next1 -> next = NULL;
}
for(i=6;i<10;i++)//第二条链表
{
next1 = (list)malloc(sizeof(list));
next1->number = i;
pointer1->next = next1;
pointer1 = next1;
next1->next = NULL;
}
head1->next = pointer;//将两条链表连接起来
next1 = head;
while(next1 != NULL)
{
printf("next->number: %d\n",next1->number);
next1 = next1->next;
}
next1 = head->next;
while(next1 != NULL)//释放节点
{
free(head);
head = next1;
next1 = head->next;
}
system("pause");
return 0;
}