主题:关于双链表的程序
#include<stdio.h>
#include<malloc.h>
struct Node
{
char Data;
struct Node *next;
struct Node *pre;
};
struct Node *First; //链表头指针
struct Node *Rear; //链表尾指针
struct Node *Creat(); //创建新节点
void InsertFront(); //在链表头部插入新节点
void InsertMide(); //在链表中部插入新节点
void InsertRear(); //在链表尾部插入新节点
void PrintFront(); //从头部输出链表
void PrintRear(); //从尾部输出链表
void DelFront(); //在链表头部删除一个节点
void DelMide(); //在链表中删除一个节点
void DelRear(); //在链表尾部删除一个节点
int n; //全局变量
//---------------------------------------------------------
void main()
{
int i=1,j;
printf("Please input the character for creating a linklist!\n");
printf("--------------------------------------------------\n");
First=Creat();
PrintFront();
while(1)
{
printf("\nPlease choose the operation: \n");
printf(" 1. Insert a character at front\n");
printf(" 2. Insert a character at Middle\n");
printf(" 3. Insert a character at rear \n");
printf(" 4. Delete a character at front\n");
printf(" 5. Delete a character at Middle\n");
printf(" 6. Delete a character at rear \n");
printf(" 7. Output the linklist at rear\n");
printf(" 0. Exit \n");
scanf("%d",&j);
if(j==1)
InsertFront();
else if(j==2)
InsertMide();
else if(j==3)
InsertRear();
else if(j==4)
DelFront();
else if(j==6)
DelRear();
else if(j==7)
PrintRear();
else if(j==5)
DelMide();
else
break;
}
}
//--------------------------------------------------------
struct Node *Creat()
{
struct Node *head;
struct Node *p1,*p2;
n=0;
p1=p2=(struct Node *)malloc(sizeof(struct Node));
p1->next=p1->pre=NULL;
printf("Please input the character:");
scanf("%c",&p1->Data);
if(p1->Data=='\n')
{head=NULL;
return (head);}
else{
while(p1->Data!='\n')
{
n=n+1;
if(n==1)
head=p2=p1;
else
{
p2->next=p1;
p1->pre=p2;
}
p1->next=NULL;
p2=p1;
p1=(struct Node *)malloc(sizeof(struct Node));
scanf("%c",&p1->Data);
}
Rear=p2;}
return (head);
}
//----------------------------------------------------
void InsertFront()
{
struct Node *p,*s;
int k=1;
getchar();
while(k)
{
p=First;
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
scanf("%c",&s->Data);
if(!First)
{
First=Rear=s;
First->next=First->pre=NULL;
}
else
{
s->next=p;
s->pre=NULL;
p->pre=s;
First=s;
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//-------------------------------------------------------
[color=008000]/*函数void InsertMide()在执行过程中有一个问题
就是当链表中元素出现重复,而要插入的位置恰好
在该元素前面时,链表中有一些元素会消失掉,若
各个元素不一样,或插入位置不在重复元素的前面
则程序运行正确,经过反复调试,目前还没有解决
该问题。 2011-5-6 by cookles*/[/color]
void InsertMide()
{
struct Node *p,*s,*p1;
int k=1;
char x;
getchar();
while(k)
{
p=First;
printf("Please choose the character you want to insert before:");
scanf("%c",&x);
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
getchar();
scanf("%c",&s->Data);
while(p!=NULL&&p!=Rear)
{
p1=p->pre;
if(p->Data==x)
{
if(p==First)
{
p1=s;
p->pre=s;
s->next=p;
First=s;
First->pre=NULL;
}
else
{
p1->next=s;
s->pre=p1;
p->pre=s;
s->next=p;
}
}
p1=p;
p=p->next;
}
if(p==Rear)
{
if(p->Data==x)
{
p1->next=s;
s->pre=p1;
s->next=p;
p->pre=s;
Rear=p;
Rear->next=NULL;
}
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//---------------------------------------------------------
void InsertRear()
{
struct Node *p,*s;
int k=1;
getchar();
while(k)
{
p=Rear;
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
scanf("%c",&s->Data);
if(!Rear)
{
First=Rear=s;
Rear->pre=Rear->next=NULL;
}
else
{
s->next=NULL;
s->pre=Rear;
p->next=s;
Rear=s;
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//------------------------------------------------------
void PrintFront()
{
struct Node *p;
p=First;
printf("Output the linklist at front :");
if(!First)
printf("The linklist is empty!\n");
else
{
while(p!=NULL)
{
printf("%c ",p->Data);
p=p->next;
}
}
printf("\n");
}
//---------------------------------------------------------
void PrintRear()
{
struct Node *p;
p=Rear;
printf("Output the linklist at Rear :");
if(!First)
printf("The linklist is empty!\n");
else
{
while(p!=NULL)
{
printf("%c ",p->Data);
p=p->pre;
}
}
printf("\n");
}
//-----------------------------------------------------
void DelFront()
{
int k=1;
while(k)
{
struct Node *p;
p=First;
if(First==Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=Rear=NULL;
free(p);
PrintFront();
break;
}
if(First!=Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=p->next;
First->pre=NULL;
free(p);
PrintFront();
}
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
}
}
//----------------------------------------------------
void DelMide()
{
struct Node *p,*p1;
int k=1;
char x;
getchar();
while(k)
{
p=First;
printf("Please choose the character you want to delete:");
scanf("%c",&x);
while(p!=NULL&&p!=Rear)
{
p1=p->pre;
if(p->Data==x)
{
if(p==First)
{
p1=p->next;
First=p1;
First->pre=NULL;
}
else
{
(p->next)->pre=p1;
p1->next=p->next;
}
}
p1=p;
p=p->next;
}
if(p==Rear)
if(p->Data==x)
{
if(Rear==First)
{
Rear=First=NULL;
PrintFront();
break;
}
else
{
Rear=Rear->pre;
Rear->next=NULL;
}
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//-----------------------------------------------------
void DelRear()
{
int k=1;
while(k)
{
struct Node *p;
p=Rear;
if(First==Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=Rear=NULL;
free(p);
PrintFront();
break;
}
if(First!=Rear)
{
printf("\n%c is deleted!\n",p->Data);
Rear=p->pre;
Rear->next=NULL;
free(p);
PrintFront();
}
else
printf("The linklist is empty!\n");
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
}
}
问题如注释所述。
例如创建的双链表为:1 2 3 4 5 1
当选择2 ,在链表中间插入元素时,选择在1前面插入a,结果会是:a,1
后面的元素 2 3 4 5 1,都不见了。
不解。。。
#include<malloc.h>
struct Node
{
char Data;
struct Node *next;
struct Node *pre;
};
struct Node *First; //链表头指针
struct Node *Rear; //链表尾指针
struct Node *Creat(); //创建新节点
void InsertFront(); //在链表头部插入新节点
void InsertMide(); //在链表中部插入新节点
void InsertRear(); //在链表尾部插入新节点
void PrintFront(); //从头部输出链表
void PrintRear(); //从尾部输出链表
void DelFront(); //在链表头部删除一个节点
void DelMide(); //在链表中删除一个节点
void DelRear(); //在链表尾部删除一个节点
int n; //全局变量
//---------------------------------------------------------
void main()
{
int i=1,j;
printf("Please input the character for creating a linklist!\n");
printf("--------------------------------------------------\n");
First=Creat();
PrintFront();
while(1)
{
printf("\nPlease choose the operation: \n");
printf(" 1. Insert a character at front\n");
printf(" 2. Insert a character at Middle\n");
printf(" 3. Insert a character at rear \n");
printf(" 4. Delete a character at front\n");
printf(" 5. Delete a character at Middle\n");
printf(" 6. Delete a character at rear \n");
printf(" 7. Output the linklist at rear\n");
printf(" 0. Exit \n");
scanf("%d",&j);
if(j==1)
InsertFront();
else if(j==2)
InsertMide();
else if(j==3)
InsertRear();
else if(j==4)
DelFront();
else if(j==6)
DelRear();
else if(j==7)
PrintRear();
else if(j==5)
DelMide();
else
break;
}
}
//--------------------------------------------------------
struct Node *Creat()
{
struct Node *head;
struct Node *p1,*p2;
n=0;
p1=p2=(struct Node *)malloc(sizeof(struct Node));
p1->next=p1->pre=NULL;
printf("Please input the character:");
scanf("%c",&p1->Data);
if(p1->Data=='\n')
{head=NULL;
return (head);}
else{
while(p1->Data!='\n')
{
n=n+1;
if(n==1)
head=p2=p1;
else
{
p2->next=p1;
p1->pre=p2;
}
p1->next=NULL;
p2=p1;
p1=(struct Node *)malloc(sizeof(struct Node));
scanf("%c",&p1->Data);
}
Rear=p2;}
return (head);
}
//----------------------------------------------------
void InsertFront()
{
struct Node *p,*s;
int k=1;
getchar();
while(k)
{
p=First;
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
scanf("%c",&s->Data);
if(!First)
{
First=Rear=s;
First->next=First->pre=NULL;
}
else
{
s->next=p;
s->pre=NULL;
p->pre=s;
First=s;
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//-------------------------------------------------------
[color=008000]/*函数void InsertMide()在执行过程中有一个问题
就是当链表中元素出现重复,而要插入的位置恰好
在该元素前面时,链表中有一些元素会消失掉,若
各个元素不一样,或插入位置不在重复元素的前面
则程序运行正确,经过反复调试,目前还没有解决
该问题。 2011-5-6 by cookles*/[/color]
void InsertMide()
{
struct Node *p,*s,*p1;
int k=1;
char x;
getchar();
while(k)
{
p=First;
printf("Please choose the character you want to insert before:");
scanf("%c",&x);
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
getchar();
scanf("%c",&s->Data);
while(p!=NULL&&p!=Rear)
{
p1=p->pre;
if(p->Data==x)
{
if(p==First)
{
p1=s;
p->pre=s;
s->next=p;
First=s;
First->pre=NULL;
}
else
{
p1->next=s;
s->pre=p1;
p->pre=s;
s->next=p;
}
}
p1=p;
p=p->next;
}
if(p==Rear)
{
if(p->Data==x)
{
p1->next=s;
s->pre=p1;
s->next=p;
p->pre=s;
Rear=p;
Rear->next=NULL;
}
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//---------------------------------------------------------
void InsertRear()
{
struct Node *p,*s;
int k=1;
getchar();
while(k)
{
p=Rear;
s=(struct Node *)malloc(sizeof(struct Node));
printf("Please input a new character:");
scanf("%c",&s->Data);
if(!Rear)
{
First=Rear=s;
Rear->pre=Rear->next=NULL;
}
else
{
s->next=NULL;
s->pre=Rear;
p->next=s;
Rear=s;
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//------------------------------------------------------
void PrintFront()
{
struct Node *p;
p=First;
printf("Output the linklist at front :");
if(!First)
printf("The linklist is empty!\n");
else
{
while(p!=NULL)
{
printf("%c ",p->Data);
p=p->next;
}
}
printf("\n");
}
//---------------------------------------------------------
void PrintRear()
{
struct Node *p;
p=Rear;
printf("Output the linklist at Rear :");
if(!First)
printf("The linklist is empty!\n");
else
{
while(p!=NULL)
{
printf("%c ",p->Data);
p=p->pre;
}
}
printf("\n");
}
//-----------------------------------------------------
void DelFront()
{
int k=1;
while(k)
{
struct Node *p;
p=First;
if(First==Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=Rear=NULL;
free(p);
PrintFront();
break;
}
if(First!=Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=p->next;
First->pre=NULL;
free(p);
PrintFront();
}
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
}
}
//----------------------------------------------------
void DelMide()
{
struct Node *p,*p1;
int k=1;
char x;
getchar();
while(k)
{
p=First;
printf("Please choose the character you want to delete:");
scanf("%c",&x);
while(p!=NULL&&p!=Rear)
{
p1=p->pre;
if(p->Data==x)
{
if(p==First)
{
p1=p->next;
First=p1;
First->pre=NULL;
}
else
{
(p->next)->pre=p1;
p1->next=p->next;
}
}
p1=p;
p=p->next;
}
if(p==Rear)
if(p->Data==x)
{
if(Rear==First)
{
Rear=First=NULL;
PrintFront();
break;
}
else
{
Rear=Rear->pre;
Rear->next=NULL;
}
}
PrintFront();
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
getchar();
}
}
//-----------------------------------------------------
void DelRear()
{
int k=1;
while(k)
{
struct Node *p;
p=Rear;
if(First==Rear)
{
printf("\n%c is deleted!\n",p->Data);
First=Rear=NULL;
free(p);
PrintFront();
break;
}
if(First!=Rear)
{
printf("\n%c is deleted!\n",p->Data);
Rear=p->pre;
Rear->next=NULL;
free(p);
PrintFront();
}
else
printf("The linklist is empty!\n");
printf("Are you want to continue(Y/N)?:");
if(getchar()=='Y'||getchar()=='y')
k=1;
else
k=0;
}
}
问题如注释所述。
例如创建的双链表为:1 2 3 4 5 1
当选择2 ,在链表中间插入元素时,选择在1前面插入a,结果会是:a,1
后面的元素 2 3 4 5 1,都不见了。
不解。。。