1.带头节点尾插法链表
#include<stdio.h>
#include<stdlib.h>
typedef struct N{
int data;
struct N *next;
}Node;
#include<stdlib.h>
typedef struct N{
int data;
struct N *next;
}Node;
int main(void)
{
Node *head;
Node *p,*q;
int a;
head=(Node*)malloc(sizeof(Node));
p=head;
scanf("%d",&a);
while(a!=-1){
q=(Node*)malloc(sizeof(Node));
q->data=a;
p->next=q;
p=q;
scanf("%d",&a);
}
p->next=NULL;
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
{
Node *head;
Node *p,*q;
int a;
head=(Node*)malloc(sizeof(Node));
p=head;
scanf("%d",&a);
while(a!=-1){
q=(Node*)malloc(sizeof(Node));
q->data=a;
p->next=q;
p=q;
scanf("%d",&a);
}
p->next=NULL;
p=head->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
2.尾插法不带头结点
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node*creat()
{
node *pnew1,*pnew2,*q;
int x;
pnew1=(node*)malloc(sizeof(node));
q=pnew1;
printf("please input numbers:");
scanf("%d",&x);
pnew1->data=x;
while(x)
{
pnew2=(node*)malloc(sizeof(node));
scanf("%d",&x);
pnew2->data=x;
pnew1->next=pnew2;
pnew1=pnew2;
}
pnew1->next=NULL;
return q;
}
void print(node *head)
{
while(head&&head->data)
{
printf("%5d",head->data);
head=head->next;
}
}
main()
{
node *s;
s=creat();
print(s);
}
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node*creat()
{
node *pnew1,*pnew2,*q;
int x;
pnew1=(node*)malloc(sizeof(node));
q=pnew1;
printf("please input numbers:");
scanf("%d",&x);
pnew1->data=x;
while(x)
{
pnew2=(node*)malloc(sizeof(node));
scanf("%d",&x);
pnew2->data=x;
pnew1->next=pnew2;
pnew1=pnew2;
}
pnew1->next=NULL;
return q;
}
void print(node *head)
{
while(head&&head->data)
{
printf("%5d",head->data);
head=head->next;
}
}
main()
{
node *s;
s=creat();
print(s);
}
3.带头节点头插法
#include<stdio.h>
#include<stdlib.h>
typedef struct N
{
int data;
struct N *next;
}Node,*linklist;
#include<stdlib.h>
typedef struct N
{
int data;
struct N *next;
}Node,*linklist;
linklist creat()
{
int x;
linklist p,q,head;
head=(linklist)malloc(sizeof(Node));
head->next=NULL;
p=head;
printf("please input numbers(遇0结束):");
scanf("%d",&x);
while(x)
{
q=(linklist)malloc(sizeof(Node));
q->data=x;
q->next=head->next;
head->next=q;
scanf("%d",&x);
}
return head;
}
{
int x;
linklist p,q,head;
head=(linklist)malloc(sizeof(Node));
head->next=NULL;
p=head;
printf("please input numbers(遇0结束):");
scanf("%d",&x);
while(x)
{
q=(linklist)malloc(sizeof(Node));
q->data=x;
q->next=head->next;
head->next=q;
scanf("%d",&x);
}
return head;
}
void print(linklist head)
{
linklist p;
p=head->next;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
{
linklist p;
p=head->next;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
int main(void)
{
linklist p;
p=creat();
print(p);
return 0;
}
{
linklist p;
p=creat();
print(p);
return 0;
}
4.头插不带头结点的单链表
#include<stdio.h>
#include<stdlib.h>
typedef struct N
{
int data;
struct N *next;
}Node,*linklist;
#include<stdlib.h>
typedef struct N
{
int data;
struct N *next;
}Node,*linklist;
linklist creat()
{
int x;
linklist p,q;
p=NULL;
printf("please input numbers(遇0结束):");
scanf("%d",&x);
while(x)
{
q=(linklist)malloc(sizeof(Node));
q->data=x;
q->next=p;
p=q;
scanf("%d",&x);
}
return p;
}
{
int x;
linklist p,q;
p=NULL;
printf("please input numbers(遇0结束):");
scanf("%d",&x);
while(x)
{
q=(linklist)malloc(sizeof(Node));
q->data=x;
q->next=p;
p=q;
scanf("%d",&x);
}
return p;
}
void print(linklist p)
{
linklist q;
q=p;
while(q)
{
printf("%3d",q->data);
q=q->next;
}
}
{
linklist q;
q=p;
while(q)
{
printf("%3d",q->data);
q=q->next;
}
}
int main(void)
{
linklist p;
p=creat();
print(p);
return 0;
}
{
linklist p;
p=creat();
print(p);
return 0;
}
5.指定位置插入
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
linklist creat()
{
int x;
node *pnew,*q;
linklist h;
h=(linklist)malloc(sizeof(node));
h->next=NULL;
q=h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
return h;
}
{
int x;
node *pnew,*q;
linklist h;
h=(linklist)malloc(sizeof(node));
h->next=NULL;
q=h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
return h;
}
void insert(linklist h)
{
int t=0,x,n;
node *p,*q,*m;
p=h;
printf("请输入要插入的数:");
scanf("%d",&x);
printf("请输入要插入的节点的位置:");
scanf("%d",&n);
while(p)
{
if(t==n-1)
{
m=p->next;
q=(node *)malloc(sizeof(node));
q->data=x;
p->next=q;
q->next=m;
break;
}
p=p->next;
t++;
}
}
{
int t=0,x,n;
node *p,*q,*m;
p=h;
printf("请输入要插入的数:");
scanf("%d",&x);
printf("请输入要插入的节点的位置:");
scanf("%d",&n);
while(p)
{
if(t==n-1)
{
m=p->next;
q=(node *)malloc(sizeof(node));
q->data=x;
p->next=q;
q->next=m;
break;
}
p=p->next;
t++;
}
}
void print(linklist h)
{
node *m;
m=h->next;
while(m)
{
printf("%5d",m->data);
m=m->next;
}
printf("\n");
}
{
node *m;
m=h->next;
while(m)
{
printf("%5d",m->data);
m=m->next;
}
printf("\n");
}
main()
{
linklist h;
h=creat();
print(h);
insert(h);
print(h);
}
{
linklist h;
h=creat();
print(h);
insert(h);
print(h);
}
6.输出链表中的最大值
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
void creat(linklist *h)
{
int x;
node *pnew,*q;
*h=(linklist)malloc(sizeof(node));
(*h)->next=NULL;
q=*h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
}
{
int x;
node *pnew,*q;
*h=(linklist)malloc(sizeof(node));
(*h)->next=NULL;
q=*h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
}
int compare(linklist *h)
{
linklist p,q;
int max;
p=(*h)->next;
max=p->data;
q=p->next;
while(q!=NULL)
{
if(max<(q->data))
{
max=q->data;
}
q=q->next;
}
return max;
}
main()
{
linklist h;
creat(&h);
printf("%d",compare(&h));
}
int max;
p=(*h)->next;
max=p->data;
q=p->next;
while(q!=NULL)
{
if(max<(q->data))
{
max=q->data;
}
q=q->next;
}
return max;
}
main()
{
linklist h;
creat(&h);
printf("%d",compare(&h));
}
7.删除值为X的节点
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
linklist creat()
{
int x;
node *pnew,*q;
linklist h;
h=(linklist)malloc(sizeof(node));
h->next=NULL;
q=h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
return h;
}
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node,*linklist;
linklist creat()
{
int x;
node *pnew,*q;
linklist h;
h=(linklist)malloc(sizeof(node));
h->next=NULL;
q=h;
printf("please input numbers(输0回车结束):");
scanf("%d",&x);
while(x)
{
pnew=(node *)malloc(sizeof(node));
pnew->data=x;
pnew->next=q->next;
q->next=pnew;
q=pnew;
scanf("%d",&x);
}
return h;
}
void deleted(linklist h,int n)
{
node* p;
p=h;
while(p->next)
{
if(p->next->data!=n)
{
p=p->next;
}
else
{
p->next=p->next->next;
}
}
}
void print(linklist h)
{
node *m;
m=h->next;
while(m)
{
printf("%5d",m->data);
m=m->next;
}
printf("\n");
}
{
node* p;
p=h;
while(p->next)
{
if(p->next->data!=n)
{
p=p->next;
}
else
{
p->next=p->next->next;
}
}
}
void print(linklist h)
{
node *m;
m=h->next;
while(m)
{
printf("%5d",m->data);
m=m->next;
}
printf("\n");
}
main()
{
linklist h;
node *q;
int n;
h=creat();
print(h);
printf("请输入要删除的值:");
scanf("%d",&n);
deleted(h,n);
print(h);
}
{
linklist h;
node *q;
int n;
h=creat();
print(h);
printf("请输入要删除的值:");
scanf("%d",&n);
deleted(h,n);
print(h);
}
8.删除链表中值为偶数的所有节点
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
typedef struct N
{
int data;
struct N *next;
}Node,*linklist;
{
int data;
struct N *next;
}Node,*linklist;
linklist creat()
{
int n;
linklist head,p,q;
head=(linklist)malloc(sizeof(Node));
head->next=NULL;
p=head;
printf("请输入值(遇-1停止):");
scanf("%d",&n);
while(n!=-1)
{
q=(linklist)malloc(sizeof(Node));
q->data=n;
q->next=p->next;
p->next=q;
p=q;
scanf("%d",&n);
}
return head;
}
{
int n;
linklist head,p,q;
head=(linklist)malloc(sizeof(Node));
head->next=NULL;
p=head;
printf("请输入值(遇-1停止):");
scanf("%d",&n);
while(n!=-1)
{
q=(linklist)malloc(sizeof(Node));
q->data=n;
q->next=p->next;
p->next=q;
p=q;
scanf("%d",&n);
}
return head;
}
linklist Delete(linklist head)
{
linklist p,t;
p=head;
while(p->next!=NULL)
{
if(p->next->data%2==0)
{
t=p->next;
p->next=t->next;
free(t);
t=NULL;
}
else
p=p->next;
}
return head;
}
{
linklist p,t;
p=head;
while(p->next!=NULL)
{
if(p->next->data%2==0)
{
t=p->next;
p->next=t->next;
free(t);
t=NULL;
}
else
p=p->next;
}
return head;
}
void print(linklist head)
{
linklist p;
p=head->next;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
{
linklist p;
p=head->next;
while(p)
{
printf("%3d",p->data);
p=p->next;
}
}
int main(void)
{
linklist p,q;
p=creat();
q=Delete(p);
print(q);
return 0;
}
{
linklist p,q;
p=creat();
q=Delete(p);
print(q);
return 0;
}