功能&指令
1.增加头节点
addHead + value
2.增加特定节点前的节点
addIndex + value + index
3.增加尾节点
addTail +value
4.删除头节点
deleteHead
5.删除特定节点前的节点
deleteIndex +index
6.翻转链表
reverse
7.打印链表
printList
8.清空链表
freeList
9.退出
EOF(Windows Ctrl+Z)(Linux/Unix Ctrl+D)
程序实现
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Node
{
int val;
struct Node* next;
}List;
List* creatList()
{
List* head=(List*)malloc(sizeof(List));
head->next=NULL;
return head;
}
int getValue(List* myList,int index)
{
if(index<=0)
return 0;
while(index--&&myList!=NULL)
{
myList=myList->next;
}
if(myList==NULL)
{
return 0;
}
return myList->val;
}
void addAtHead(List* myList,int val)
{
List* newNode=(List*)malloc(sizeof(List));
newNode->next=myList->next;
newNode->val=val;
myList->next=newNode;
}
void addAtTail(List* myList,int val)
{
List* newNode=(List*)malloc(sizeof(List));
newNode->next=NULL;
newNode->val=val;
while(myList->next!=NULL)
{
myList=myList->next;
}
myList->next=newNode;
}
int addAtIndex(List* myList,int index,int val)
{
if(index<=0)
return 0;
List* newNode=(List*)malloc(sizeof(List));
newNode->val=val;
while(--index&&myList!=NULL)
{
myList=myList->next;
}
if(myList==NULL)
return 0;
newNode->next=myList->next;
myList->next=newNode;
return 1;
}
int deleteAtHead(List* myList)
{
if(myList->next==NULL)
return 0;
List* temp=myList->next;
myList->next=temp->next;
free(temp);
return 1;
}
int deleteAtIndex(List* myList,int index)
{
if(index<=0)
return 0;
while(--index&&myList!=NULL)
{
myList=myList->next;
}
if(myList==NULL||myList->next==NULL)
{
return 0;
}
List* temp=NULL;
temp=myList->next;
myList->next=temp->next;
free(temp);
return 1;
}
void reverseList(List* myList)
{
List* pre=NULL;
List* cur=NULL;
List* nxt=NULL;
cur=myList->next;
while(cur!=NULL)
{
nxt=cur->next;
cur->next=pre;
pre=cur;
cur=nxt;
}
myList->next=pre;
}
void printList(List* myList)
{
myList=myList->next;
while(myList!=NULL)
{
printf("%d->",myList->val);
myList=myList->next;
}
printf("null\n");
}
void freeList(List* myList)
{
List* temp=NULL;
while(myList!=NULL)
{
temp=myList;
myList=temp->next;
free(temp);
}
}
int main()
{
List* myList=creatList();
char command[8][20]={{"addHead"},{"addIndex"},{"addTail"},{"deleteHead"},{"deleteIndex"},{"printList"},{"freeList"},{"reverse"}};
while(1)
{
char str[20];
if( scanf("%s",str)!=EOF )
{
for(int i=0;i<8;i++)
{
if( strcmp(command[i],str)==0)
{
switch(i)
{
case 0://addHead
{
int temp1;
scanf("%d",&temp1);
addAtHead(myList,temp1);
break;
}
case 1://addIndex
{
int temp1,temp2;
scanf("%d %d",&temp1,&temp2);
int temp3=addAtIndex(myList,temp1,temp2);
if(temp3) printf("\t\t\tAdded Successfully\n");
else printf("\t\t\tAdd attempt failed\n");
break;
}
case 2://addTail
{
int temp1;;
scanf("%d",&temp1);
addAtTail(myList,temp1);
break;
}
case 3://deletHead
{
int temp1=deleteAtHead(myList);
if(temp1) printf("\t\t\tDeleted Successfully\n");
else printf("\t\t\tDelet attempt failed\n");
break;
}
case 4://deletIndex
{
int temp1;;
scanf("%d",&temp1);
int temp2=deleteAtIndex(myList,temp1);
if(temp2) printf("\t\t\tDeleted Successfully\n");
else printf("\t\t\tDelet attempt failed\n");
break;
}
case 5://printList
{
printList(myList);
break;
}
case 6://freeList
{
freeList(myList);
break;
}
case 7://reverse
{
reverseList(myList);
printf("reverse true");
break;
}
default: break;
}
}
}
}
else
break;
}
return 0;
}