1、从键盘输入10个整数放入链表,要求链表中的元素与输入顺序一致,然后输出该链表。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node{
int data;
struct node* next;
};
struct node* createlist()
{
struct node* headnode =(struct node*)malloc(sizeof(struct node));
headnode->next=NULL;
return headnode;
}
struct node* createnode(int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void printlist(struct node* headnode)
{
struct node* pmove=headnode->next;
while (pmove)
{
printf("%d ",pmove->data);
pmove=pmove->next;
}
printf("\n");
}
void charu(struct node* lastnode,int data)
{
struct node* newnode=createnode(data);
struct node* posnode=lastnode;
while(posnode->next!=NULL)
{
posnode=posnode->next;
}
posnode->next=newnode;
}
int main(){
int i,n;
struct node* list=createlist();
for(i=0;i<10;i++){
scanf("%d",&n);
charu(list,n);}
printlist(list);
return 0;
}
2、随机生成100个随机整数并放入一个链表中,要求链表中的元素按从小到大顺序排列,然后输出该链表。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node{
int data;
struct node* next;
};
struct node* createlist()
{
struct node* headnode =(struct node*)malloc(sizeof(struct node));
headnode->next=NULL;
return headnode;
}
struct node* createnode(int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void printlist(struct node* headnode)
{
struct node* pmove=headnode->next;
while (pmove)
{
printf("%d ",pmove->data);
pmove=pmove->next;
}
printf("\n");
}
void charu(struct node* lastnode,int data)
{
struct node* newnode=createnode(data);
struct node* posnode=lastnode;
while(posnode->next!=NULL)
{
posnode=posnode->next;
}
posnode->next=newnode;
}
void paixucharu(struct node* l,int x)
{
struct node* newnode=createnode(x);
struct node* p=l;
while(p->next!=NULL&&p->next->data<=x)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
int main(){
int i,n;
srand((int)time(NULL));
struct node* list=createlist();
for(i=0;i<100;i++){ //2.随机数排序
n=rand()%100;
paixucharu(list,n);}
printlist(list);
return 0;
}
3、将两个从小到大排列的链表合并为一个新链表(仍然有序排列),输出合并前的两个链表,输出合并后的链表,检查合并是否成功。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node{
int data;
struct node* next;
};
struct node* createlist()
{
struct node* headnode =(struct node*)malloc(sizeof(struct node));
headnode->next=NULL;
return headnode;
}
struct node* createnode(int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void printlist(struct node* headnode)
{
struct node* pmove=headnode->next;
while (pmove)
{
printf("%d ",pmove->data);
pmove=pmove->next;
}
printf("\n");
}
void charu(struct node* lastnode,int data)
{
struct node* newnode=createnode(data);
struct node* posnode=lastnode;
while(posnode->next!=NULL)
{
posnode=posnode->next;
}
posnode->next=newnode;
}
void paixucharu(struct node* l,int x)
{
struct node* newnode=createnode(x);
struct node* p=l;
while(p->next!=NULL&&p->next->data<=x)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
void hebing(struct node* l1,struct node* l2,struct node* l3){
while(l2->next!=NULL)
{
l2=l2->next;
paixucharu(l3,l2->data);
}
while(l1->next!=NULL)
{
l1=l1->next;
paixucharu(l3,l1->data);
}
}
int main(){
int i,n;
srand((int)time(NULL));
struct node* list1=createlist();
struct node* list2=createlist();
struct node* list3=createlist();
for(i=0;i<5;i++){
n=rand()%100;
paixucharu(list1,n);}
for(i=0;i<5;i++){
n=rand()%100;
paixucharu(list2,n);} //3.合并链表
hebing(list1,list2,list3);
printlist(list1);
printlist(list2);
printlist(list3);
return 0;
}
4、将两个从小到大排列的链表合并为一个新链表(仍然有序排列),若遇到相同的元素,则在合并时去掉重复元素。输出合并前的两个链表,输出合并后的链表,检查合并是否成功。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node{
int data;
struct node* next;
};
struct node* createlist()
{
struct node* headnode =(struct node*)malloc(sizeof(struct node));
headnode->next=NULL;
return headnode;
}
struct node* createnode(int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void printlist(struct node* headnode)
{
struct node* pmove=headnode->next;
while (pmove)
{
printf("%d ",pmove->data);
pmove=pmove->next;
}
printf("\n");
}
void charu(struct node* lastnode,int data)
{
struct node* newnode=createnode(data);
struct node* posnode=lastnode;
while(posnode->next!=NULL)
{
posnode=posnode->next;
}
posnode->next=newnode;
}
void paixucharu(struct node* l,int x)
{
struct node* newnode=createnode(x);
struct node* p=l;
while(p->next!=NULL&&p->next->data<=x)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
int paixucharu2(struct node* l,int x)
{
struct node* newnode=createnode(x);
struct node* p=l;
while(p->next!=NULL)
{
p=p->next;
if(p->data==x)
return 0;
}
p=l;
while(p->next!=NULL&&p->next->data<x)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
void hebing2(struct node* l1,struct node* l2,struct node* l3){
while(l1->next!=NULL)
{
l1=l1->next;
paixucharu2(l3,l1->data);
}
while(l2->next!=NULL)
{
l2=l2->next;
paixucharu2(l3,l2->data);
}
}
int main(){
int i,n;
srand((int)time(NULL));
struct node* list1=createlist();
struct node* list2=createlist();
struct node* list3=createlist();
for(i=0;i<5;i++){
n=rand()%100;
paixucharu(list1,n);}
for(i=0;i<5;i++){
n=rand()%100;
paixucharu(list2,n);} //4.合并链表并且清除重复的数
hebing2(list1,list2,list3);
printlist(list1);
printlist(list2);
printlist(list3);
return 0;
}
5、随机生成100个整数存入链表,整数范围在[-100, 100]之间,输出该链表。将该链表分为两个,一个存放所有负整数,另一个存放所有非负整数,输出这两个链表。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct node{
int data;
struct node* next;
};
struct node* createlist()
{
struct node* headnode =(struct node*)malloc(sizeof(struct node));
headnode->next=NULL;
return headnode;
}
struct node* createnode(int data)
{
struct node* newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
return newnode;
}
void printlist(struct node* headnode)
{
struct node* pmove=headnode->next;
while (pmove)
{
printf("%d ",pmove->data);
pmove=pmove->next;
}
printf("\n");
}
void charu(struct node* lastnode,int data)
{
struct node* newnode=createnode(data);
struct node* posnode=lastnode;
while(posnode->next!=NULL)
{
posnode=posnode->next;
}
posnode->next=newnode;
}
void paixucharu(struct node* l,int x)
{
struct node* newnode=createnode(x);
struct node* p=l;
while(p->next!=NULL&&p->next->data<=x)
{
p=p->next;
}
newnode->next=p->next;
p->next=newnode;
}
void fenlei(struct node* l1,struct node* l2,struct node* l3){
while(l1->next!=NULL)
{
l1=l1->next;
if(l1->data<0)
paixucharu(l2,l1->data);
else
paixucharu(l3,l1->data);
}
}
int main(){
int i,n;
srand((int)time(NULL));
struct node* list1=createlist();
struct node* list2=createlist();
struct node* list3=createlist();
for(i=0;i<5;i++){
n=rand()%200-100;
paixucharu(list1,n);}
//5.正负数分类
fenlei(list1,list2,list3);
printlist(list1);
printlist(list2);
printlist(list3);
return 0;
}
6、随机生成10个整数存入一个循环双向链表中,然后按序输出这些整数和逆序输出这些整数。
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
typedef struct DNode
{
int data;
struct DNode *next,*Previous;
}Node,*LinkList;
void InitList(LinkList *L)
{
*L=(LinkList)malloc(sizeof(DNode));
(*L)->data=0;
(*L)->next=(*L)->Previous=*L;
}
void CreateLink(LinkList L,int n)
{ srand((int)time(NULL));
LinkList p=L,q;
while(n--)
{ int x=rand()%200-100;
q=(LinkList)malloc(sizeof(DNode));
q->data=x;
p->next=q;
q->Previous=p;
q->next=L;//尾插法
L->Previous=q;
p=q;
}
}
void TraverseList(LinkList L,int i)
{ int x=0;
LinkList p=L->Previous;
LinkList q=L->next;
while(x<i)
{
printf("%d ",p->data);
p=p->Previous;
x++;
}
printf("\n");
x=0;
while(x<i)
{
printf("%d ",q->data);
q=q->next;
x++;
}
}
int main()
{
srand((int)time(NULL));
int n;
LinkList L;
InitList(&L);
CreateLink(L,10); //0代表头节点的值,改变TraverseList的值改变输出的值
TraverseList(L,10);
return 0;
}