#include<stdio.h>
#include<conio.h>
#include<malloc.c>
struct dnode{
int data;
struct dnode *prev,*next;
};
typedef struct dnode DNODE;
DNODE *first;
DNODE *last;
DNODE* createDnode(int ele){
DNODE *nnode;
nnode=(DNODE *)malloc(sizeof(DNODE));
nnode->data=ele;
nnode->next=NULL;
nnode->prev=NULL;
return nnode;
}
//Insert First
DNODE *insertFirst(int ele){
DNODE *nnode;
nnode=createDnode(ele);
if(first==NULL){ //if node is creating first time
first=last=nnode;
return;
}
nnode->prev=NULL;
nnode->next=first;
first=nnode;
return first;
}
//Insert Last
DNODE *insertLast(int ele){
DNODE *nnode;
nnode=createDnode(ele);
if(first==NULL){ //if node is creating first time
first=last=nnode;
return;
}
last->next=nnode;
nnode->prev=last;
last=nnode;
return last;
}
//Insert Before an Element
DNODE *insertBefore(int ele,int key){
DNODE *nnode,*curr,*pre;
nnode=createDnode(ele);
if(first==NULL){ //if node is creating first time
first=last=nnode;
return;
}
if(first->data==key){ // if key is found at first node
nnode->next=first;
first=nnode;
return first;
}
curr=first;
while(curr && curr->data!=key){
pre=curr;
curr=curr->next;
}
if(!curr){ //if search not found then curr will be NULL, it's means the node is added at last
last->next=nnode;
nnode->prev=last;
last=nnode;
return last;
}
curr->prev->next=nnode;
nnode->prev=curr->prev;
nnode->next=curr;
curr->prev=nnode;
return;
}
// Insert After an Element
DNODE *insertAfter(int ele,int key){
DNODE *nnode,*curr,*pre;
nnode=createDnode(ele);
if(first==NULL){ //if node is creating first time
first=last=nnode;
return;
}
curr=first;
while(curr && curr->data!=key){
//pre=curr;
curr=curr->next;
}
if(!curr){ //if search not found then curr will be NULL, it's means the node is added at last
last->next=nnode;
nnode->prev=last;
last=nnode;
return last;
}
nnode->next=curr->next;
curr->next->prev=nnode;
nnode->prev=curr;
curr->next=nnode;
return;
}
//去除功能
int removeNode(int key){
DNODE *curr;
if(first==NULL){ //if node is creating first time
printf("\n\nList is Empty");
return -1;
}
curr=first;
if(first->data==key){ //if first node has key
first=first->next;
first->prev=NULL;
curr->next = NULL;
free(curr);
return;
}
while(curr && curr->data!=key){
curr=curr->next;
}
if(!curr){ //if search not found then curr will be NULL,
return -1;
}
curr->prev->next=curr->next;
curr->next->prev=curr->prev;
curr->next = curr->prev = NULL;
printf("\n\n%d is Successfully Deleted: ",curr->data);
free(curr);
return;
}
void display(){
DNODE *curr;
if(first==NULL)
printf("\n\nNothing to Display\n\n");
curr=first;
printf("\n\n\tElement in List\n\n\t");
while(curr){
printf("%d ",curr->data);
curr=curr->next;
}
}
main(){
int ele,ch,key;
do{
printf("\nEnter Your Choice: \n");
printf("1-Insert First\t2-Insert Last\n3-Insert Before\t4-Insert After\n5-Remove \t6-Display\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter Element To Insert: ");
scanf("%d",&ele);
insertFirst(ele);
break;
case 2:
printf("Enter Element To Insert: ");
scanf("%d",&ele);
insertLast(ele);
break;
case 3:
printf("Enter Element To Insert: ");
scanf("%d",&ele);
printf("\nEnter Key: ");
scanf("%d",&key);
insertBefore(ele,key);
break;
case 4:
printf("Enter Element To Insert: ");
scanf("%d",&ele);
printf("\nEnter Key: ");
scanf("%d",&key);
insertAfter(ele,key);
break;
case 5:
printf("Enter Key to Delete: ");
fflush(stdin);
scanf("%d",&key);
removeNode(key);
break;
case 6:
display();
break;
}
}while(ch!=7);
getch();
return 0;
}
你能在此操作過程更改列表結構呢?它可能會簡化一些事情,如果一旦打印完一行,就可以釋放所有這些節點並將它們從列表中刪除。 –
不,我無法改變結構 – PnP
嗯,這是愚蠢的。不是這是你的錯,但鏈表的主要優點是你可以重新排序元素。(另外,如果這是家庭作業,你應該添加作業標籤,我們仍然會幫助你,但是如果你應該學習,我們不想爲你做所有的工作。) –