多功能单链表实现

天大地大妈咪最大 提交于 2020-02-28 01:08:28

功能&指令

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;
}
	
//Get the node by subscript, the subscript of the head node is 1
//If the node value is successfully obtained, the value is returned
//If it fails, return 0
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;
}
	
//Add a node at the head
void addAtHead(List* myList,int val)
{
	List* newNode=(List*)malloc(sizeof(List));
	newNode->next=myList->next;
	newNode->val=val;
	myList->next=newNode;
}
	
//Add a node at the end
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;
}
	
//Add a node at subscript
//Add success returns 1.Add failure returns 0
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;
}
	
//Delete head node
//Delete success returns 1.Delete failure returns 0
int deleteAtHead(List* myList)
{
	if(myList->next==NULL)
	    return 0;
	List* temp=myList->next;
	myList->next=temp->next;
	free(temp);
    return 1;
}
	
//Delete subscript node
//Delete success returns 1.Delete failure returns 0
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;
}
	
//Reverse linked list
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;
}

//Print linked list
//list:0  1  2  3  4  5
//form:0->1->2->3->4->5->null
void printList(List* myList)
{
	myList=myList->next;
	while(myList!=NULL)
	{
		printf("%d->",myList->val);
		myList=myList->next;
	}
	printf("null\n");
}
	
//Free linked list
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;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!