//单链表:每个节点储存数据和指针指向下一个节点
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct list{
int data;
struct list *next;
}*head,*t;
int w;
//使用结构体储存链表的每一个结点
void createlist(){
w=0;
struct list *p,*q;
int i,n,k;
printf("please input:\n");
scanf("%d",&n);
head=NULL;//头指针指向空
for(i=0;i<n;i++){
w++;
printf("input %d:\n",i+1);
scanf("%d",&k);
p=(struct list *)malloc(sizeof(struct list));
p->data=k;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
printf("链表创建成功\n");
}
void addlist(struct list *head,int n){
struct list *p,*q;
t=head;
for(int i=0;i<w;i++)
{
if(t==NULL||t->next->data>n){
p=(struct list*)malloc(sizeof(struct list));
p->data=n;
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
t=head;
while(t!=NULL){
printf("%d\n",t->data);
t=t->next;
}
}
void deletelist(struct list *head,int n){
struct list *p,*q;
t=head;
for(int i=0;i<w+1;i++)
{
if(t->next==NULL&&t->data!=n){
printf("no list\n");
return;
}
else if(t->next->data==n){
p=t->next;
t->next=p->next;
free(p);
break;
}
t=t->next;
}
t=head;
while(t!=NULL){
printf("%d\n",t->data);
t=t->next;
}
}
int main(){
createlist();
int a,b;
scanf("%d",&a);
addlist(head,a);
scanf("%d",&b);
deletelist(head,b);
return 0;
}
//双向链表即加一个前面的指针*bef
//循环链表即输入最后一个数时t->next指向head
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
struct list{
int data;
struct list *next;
}*head,*t;
int w;
//使用结构体储存链表的每一个结点
void createlist(){
w=0;
struct list *p,*q;
int i,n,k;
printf("please input:\n");
scanf("%d",&n);
head=NULL;//头指针指向空
for(i=0;i<n;i++){
w++;
printf("input %d:\n",i+1);
scanf("%d",&k);
p=(struct list *)malloc(sizeof(struct list));
p->data=k;
p->next=NULL;
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
printf("链表创建成功\n");
}
void addlist(struct list *head,int n){
struct list *p,*q;
t=head;
for(int i=0;i<w;i++)
{
if(t==NULL||t->next->data>n){
p=(struct list*)malloc(sizeof(struct list));
p->data=n;
p->next=t->next;
t->next=p;
break;
}
t=t->next;
}
t=head;
while(t!=NULL){
printf("%d\n",t->data);
t=t->next;
}
}
void deletelist(struct list *head,int n){
struct list *p,*q;
t=head;
for(int i=0;i<w+1;i++)
{
if(t->next==NULL&&t->data!=n){
printf("no list\n");
return;
}
else if(t->next->data==n){
p=t->next;
t->next=p->next;
free(p);
break;
}
t=t->next;
}
t=head;
while(t!=NULL){
printf("%d\n",t->data);
t=t->next;
}
}
int main(){
createlist();
int a,b;
scanf("%d",&a);
addlist(head,a);
scanf("%d",&b);
deletelist(head,b);
return 0;
}
//双向链表即加一个前面的指针*bef
//循环链表即输入最后一个数时t->next指向head