单链表常见操作
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *next;
}LinkListNode;
typedef struct {
LinkListNode *head;
int length;
}LinkList;
LinkList * createList(LinkList *);
LinkList * initList(LinkList *);
int getLength(LinkList *);
LinkList * insertList(LinkList *,int ,int);
LinkList * insertListAtFirst(LinkList *,int);
LinkList * deleteListByValue(LinkList *,int);
LinkList * deleteListByIndex(LinkList *,int);
LinkList * deleteFirstNode(LinkList *);
LinkListNode * searchByValue(LinkList *,int);
LinkListNode * searchByIndex(LinkList *,int);
LinkListNode * searchByValue2(LinkList *,int);
void displayList(LinkList *);
LinkList *removess(LinkList *);
LinkList * createList(LinkList *list){
list = (LinkList *)malloc(sizeof(LinkList));
printf("\n创建成功");
return list;
}
LinkList * initList(LinkList *list){
list->head = NULL;
list->length = 0;
printf("\n初始化成功");
return list;
}
//前插
LinkList * insertListAtFirst(LinkList *list,int value){
LinkListNode *temp;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
temp = (LinkListNode *)malloc(sizeof(LinkListNode));
temp->data = value;
temp->next = list->head;
list->head = temp;
list->length++;
printf("\n插入成功");
return list;
}
//随意插
LinkList * insertList(LinkList *list,int pos,int value){
LinkListNode *temp = NULL,*newNode;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
if(pos < 1 || pos > list->length+1)
{
printf("\n插入位置不正确");
return list;
}
if(pos == 1)
{
list = insertListAtFirst(list,value);
return list;
}
temp = searchByIndex(list,pos-1);
if(temp)
{
newNode = (LinkListNode *)malloc(sizeof(LinkListNode));
newNode->data = value;
newNode->next = temp->next;
temp->next = newNode;
list->length++;
printf("\n插入成功");
}
return list;
}
//按序号查找
LinkListNode * searchByIndex(LinkList *list,int pos){
int cnt = 1;
LinkListNode *p;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
if(pos < 1 || pos > list->length)
{
printf("\n查找的位置不正确");
return NULL;
}
p = list->head;
while(cnt != pos){
p = p->next;
cnt++;
}
return p;
}
//输出
void displayList(LinkList *list){
LinkListNode *p;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return ;
}
p = list->head;
while(p)
{
printf("%d\t",p->data);
p = p->next;
}
printf("\n输出完毕");
return ;
}
//求表长
int getLength(LinkList *list){
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return ;
}
return list->length;
}
//按值查找
LinkListNode * searchByValue(LinkList *list,int value){
LinkListNode *p = NULL;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
p = list->head;
while(p != NULL){
if(p->data == value)
{
return p;
}
else
p = p->next;
}
return NULL;
}
//删除链表第一个节点
LinkList * deleteFirstNode(LinkList *list){
LinkListNode *q = NULL;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
q = list->head;
list->head = q->next;
free(q);
list->length--;
printf("\n删除成功");
return list;
}
//按序号删除
LinkList * deleteListByIndex(LinkList *list,int pos){
LinkListNode *p = NULL,*q = NULL;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
if(pos < 1 || pos > list->length)
{
printf("\n删除位置不合法");
return list;
}
if(pos == 1)
{
list = deleteFirstNode(list);
}
p = searchByIndex(list,pos-1);
if(p != NULL)
{
q = p->next;
p->next = q->next;
free(q);
list->length--;
printf("\n删除成功");
}
return list;
}
//查找值为value的节点的前一个节点
LinkListNode * searchByValue2(LinkList *list,int value){
LinkListNode *p = NULL;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
p = list->head;
while(p->next != NULL){
if(p->next->data == value)
{
return p;
}
else
p = p->next;
}
return NULL;
}
//按值删除
LinkList * deleteListByValue(LinkList *list,int value){
LinkListNode *p = NULL,*q = NULL;
if(list == NULL)
{
printf("\n表不存在,请先创建并初始化");
return NULL;
}
p = list->head;
if(p->data == value)
{
list = deleteFirstNode(list);
return list;
}
p = searchByValue2(list,value) ;
if(p != NULL)
{
q = p->next;
p->next = q->next;
free(q);
list->length--;
printf("\n删除成功");
}
return list;
}
//去重
LinkList *removess(LinkList *list){
LinkListNode *p=NULL,*q=NULL,*a=NULL;
if(list==NULL)
{
printf("\n表不存在");
return NULL;
}
p=list->head;//头节点
//遍历
while(p)
{
q=p;
while(q->next)
{
if(q->next->data != p->data){
q=q->next;
//p=p->next;
}
else
{
a=q->next;
q->next=a->next;//再次连接
free(a);//
}
}
p=p->next;
}
return list;
}
int main(int argc, char *argv[])
{
LinkList *list = NULL;
LinkListNode *tempNode = NULL;
int choice;
int value;
int pos;
int length;
while(1)
{
printf("\n-------链表----------------\n");
printf("\n-------1.创建表------------\n");
printf("\n-------2.初始化------------\n");
printf("\n-------3.求表长------------\n");
printf("\n-------4.头部插入----------\n");
printf("\n-------5.随意插入----------\n");
printf("\n-------6.按值删除----------\n");
printf("\n-------7.按序号删除--------\n");
printf("\n-------8.按值查找----------\n");
printf("\n-------9.按序号查找--------\n");
printf("\n-------10.去重-------------\n");
printf("\n-------11.输出表-----------\n");
printf("\n-------0.退出--------------\n");
printf("\n您的选择是:");
scanf("%d",&choice);
switch(choice)
{
case 1: list = createList(list);
break;
case 2: list = initList(list);
break;
case 3: length = getLength(list);
printf("\n表长为%d",length);
break;
case 4: printf("\n请输入要插入的值:");
scanf("%d",&value);
list = insertListAtFirst(list,value);
break;
case 5: printf("\n请输入插入的位置:");
scanf("%d",&pos);
printf("\n请输入插入的值:");
scanf("%d",&value);
list = insertList(list,pos,value);
break;
case 6: printf("\n请输入要删除的值:");
scanf("%d",&value);
list = deleteListByValue(list,value);
break;
case 7: printf("\n请输入要删除的元素位置:");
scanf("%d",&pos);
list = deleteListByIndex(list,pos);
break;
case 8: printf("\n请输入要查找的值:");
scanf("%d",&value);
tempNode = searchByValue(list,value);
if(tempNode == NULL)
{
printf("\n查找失败");
}
else
{
printf("\n该节点地址:%x",tempNode);
printf("\n该节点的值:%d",tempNode->data);
}
break;
case 9: printf("\n请输入查找的位置:");
scanf("%d",&pos);
tempNode = searchByIndex(list,pos);
if(tempNode == NULL)
{
printf("\n查找不成功");
}
else{
printf("\n结点的值:%d",tempNode->data);
printf("\n结点的地址:%x",tempNode);
}
break;
case 10: list=removess(list);
printf("\n去重成功!");
break;
case 11:displayList(list);
break;
case 0: return 0;
default:
printf("\n您的选择有误,请重新选择");
}
}
return 0;
}
Comments (暂无评论)