关于c语言中链表的小操作

学习了链表后写了一个在链表中各种操作的程序,可能功能也不全,该程序用处不大,如果想要使用,把数据域改成需要存储的数据类型可增加实用性。

代码如下:

#include<stdio.h> 
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
	int date;
	struct Node*next;
}Node;
void menu(void);
//假如原来链表是升序的,升序插入新节点
//不能插入节点后再排序,是升序插入新节点x
void SListNodeInsertPro(Node *head, int x);
Node* creathead();//创建头结点 
int creatNode(Node*head);//创建节点 
void showNode(Node*head);//遍历节点 
void downNode(Node*head);//销毁链表 
void SListNodeInsert(Node *head, int x, int y);//在值为x节点前插入值为y的节点
void ListNodeDel(Node *head, int x);//删除第一个值为x的结点
void SListNodeDelPro(Node *head, int x);//删除值为x的节点 
void SListNodeSort(Node *head);//节点排序 
Node* SListNodeReverse(Node *head);//翻转链表的节点(不是排序,是翻转)
Node* creathead()
{
	Node*head=NULL;
	head=(Node*)malloc(sizeof(Node)); 
	if(head==NULL)
	{
		printf("头结点创建失败\n");
		return NULL;
	}
	head->next=NULL;//指针域置空 
	return head;
 } 
int creatNode(Node*head)
 {
 	if(head==NULL)
 	{
 		printf("创建节点失败,头指针打开未成功");
 		return -2;
	 }
 	int n;//节点个数
	int i=0;	
	int date;//用于数据域输入 
 	Node*pcur=head;
 	Node*pnew=NULL;
	printf("请输入要创建节点的个数:");
	scanf("%d",&n);
	while(i!=n)
	{
		pnew=(Node*)malloc(sizeof(Node));
		if(pnew==NULL) 
		{
			printf("第%d节点创建失败",i+1);
			return -3;
		}
		printf("请输入第%d个节点的数据",i+1);
		scanf("%d",&date);
		pnew->date=date;//把数据放进去 
		pcur->next=pnew;//当前节点和新节点连接上 
		pnew->next=NULL;//新节点指针域置空 
		pcur=pnew;//当前节点变为新的那个节点
		i++; 
	}
	if(i==n)//节点全部创建成功 
	{
		return 1;
	}
	else
	{
	 return 0;//失败返回0 
    } 
}
void showNode(Node*head)
{
	Node*cur=head->next;
	printf("节点数据如下:\n");
	while(cur!=NULL)
	{
		printf("%d->",cur->date);
		cur=cur->next;//指向下一个节点 
	}
	if(NULL==cur)
	{
		printf("NULL\n"); 
	 } 
}
void downNode(Node*head)
{
	Node*cur=head->next;//前一个 
	Node*tail=cur->next;//后一个 
	if(cur==NULL||tail==NULL)
	{
		return ;
	}
	while(tail!=NULL)//头结点要手动free 
	{
		free(cur);
		cur=tail; 
		tail=tail->next;//指向下一个 
	 } 
}
void SListNodeInsert(Node *head, int x, int y)//在值为x节点前插入值为y的节点
{
  	Node *cur=head;
  	Node *tail=cur->next;
  	int flag=0;//0表示没找到 
  	if(cur==NULL)
  	{
  		printf("无节点");
		  return ; 
	  }
	  while(tail!=NULL)
	  {
	  	if(tail->date==x)//找到x值位置
		  {
		  	Node*k=(Node*)malloc(sizeof(Node));
		  	  k->date=y;
			  k->next=tail;
			  cur->next=k; 
			  flag=1; //找到了 
		  } 
		  cur=tail;
		  tail=tail->next;
	  }
	  if(flag==0)//没找到要在结尾插入
	  {
	  	Node*k=(Node*)malloc(sizeof(Node));
    	k->date=y;
    	cur->next=k;
    	k->next=NULL;
	  } 
}
void SListNodeDelPro(Node *head, int x)//删除全部值为x的结点 
{
	Node *cur=head;
  	Node *tail=head->next;
  	Node *feer=NULL; 
  	if(cur==NULL)
  	{
  		printf("无节点");
		  return ; 
	  }
	  while(tail!=NULL)
	  {
	  	if(tail->date==x)//找到x值位置
		  {
		
		   cur->next=tail->next;//连接上
		   free(tail);//释放tail节点 
		   tail=cur->next;
		  } 
		  else
          {
          	cur=tail;
		  tail=tail->next;
		  }
		  
	  }
	  return;
	
}
void ListNodeDel(Node *head, int x)//删除第一个值为x的结点
{
	Node *cur=head;
  	Node *tail=cur->next;
  	if(cur==NULL)
  	{
  		printf("无节点");
		  return ; 
	  }
	  while(cur->next!=NULL)
	  {
	  	if(tail->date==x)//找到x值位置
		  {
		   cur->next=tail->next;//连接上
		   free(tail);//释放tail节点 
		   return; 
		  } 
		  cur=cur->next;
		  tail=cur->next;
	  }
	  printf("没找到");
}
void SListNodeSort(Node *head)//排序 
{
	Node*cur=head->next;//第一个有效节点
	Node*tail=cur->next; 
	if(cur==NULL)
	{
		printf("无节点,无法排序");
		return; 
	 } 

	 Node swap;//交换的中间变量
	 //选择排序 
	for(cur;cur->next!=NULL;cur=cur->next)
	{
		for(tail=cur->next;tail!=NULL;tail=tail->next)
		{
			if(cur->date>tail->date)
			{
				//交换数据域 
				swap=*cur;
				*cur=*tail;
				*tail=swap;
				//交换指针域 
				swap.next=cur->next;
				cur->next=tail->next;
				tail->next=swap.next;
			}
		}
	}
} 
void SListNodeInsertPro(Node *head, int x)
{
	if(head!=NULL)
	{
		SListNodeSort(head);//先排序 
	}
	Node *cur=head;
	Node*tail=cur->next; 
	while(tail!=NULL)
	{
		if(tail->date>x)
		{
			Node*pnew=(Node*)malloc(sizeof(Node));
			pnew->date=x;
			pnew->next=tail;
			cur->next=pnew;
			return;
		}
		cur=tail;
		tail=tail->next; 
	 } 
	 Node*pnew=(Node*)malloc(sizeof(Node));
	pnew->date=x;
	cur->next=pnew;
	pnew->next=NULL;
	 
}
Node* SListNodeReverse(Node *head)//链表翻转
{
	Node*cur=head->next;
	Node*node=cur->next;
	Node*newhead=(Node*)malloc(sizeof(Node));
	newhead->next=NULL;
	newhead->date=cur->date;
	cur=cur->next;
	while(cur!=NULL) 
	{	
		node=cur;
	    cur=cur->next;//第二个有效值开始 
	    
		//当前链表头删操作
		
		node->next=newhead;//新链表接上去 
		newhead=node;
		
		
	}
	
	Node*nhead=(Node*)malloc(sizeof(Node));
	nhead->next=newhead; 
	return nhead; 
} 
void menu(void)
{
	printf("*************************************\n");
	printf("本程序功能如下:\n");
	printf("1.创建头结点\n");
	printf("2.创建结点\n");
	printf("3.删除一个指定值的结点\n");
	printf("4.删除全部指定值的结点\n");
	printf("5.插入结点\n");
	printf("6.清空结点\n");
	printf("7.遍历链表\n");
	printf("8.链表排序\n"); 
	printf("9.链表转置\n"); 
	printf("10.升序插入节点\n");
	printf("11.清屏\n");
	printf("12.打印菜单\n");
	printf("other.退出\n"); 
	printf("*************************************\n");
	
}
int main()
{
	int n;//检查节点创建是否成功 
	int cmp;
	int x,y,z;
	menu();
	Node*head=NULL;
	head=creathead();//创建头结点
	n=creatNode(head);//创建节点 
	if(n==0)//节点创建失败 
	{
	return -3;
	 } 
	 showNode(head);
	while(1)
	{
		printf("请输入你想要的操作\n");
		printf("cmp:");
		scanf("%d",&cmp);
		switch(cmp)
		{
			case 3:
			{
				printf("请输入你要删除的值为z的节点:");
				scanf("%d",&z);
				ListNodeDel(head,z);
				showNode(head);
			};break; 
			case 4:
			{
				printf("请输入你要删除的值为z的节点:");
				scanf("%d",&z);
				SListNodeDelPro(head,z);
				showNode(head);
			};break; 
			case 5:
			{
				printf("输入你要进行指令的x,y的值:");
				scanf("%d %d",&x,&y);
				SListNodeInsert(head, x, y);//在值为x节点前插入值为y的节点 
				showNode(head);
			};break; 
			case 6:
			{
				downNode(head);//置空链表 
				if(head!=NULL)//头结点手动置空 
				{
					free(head);
					head=NULL;
				 } 
				 printf("已清空");
			};break; 
			case 7:
			{
				showNode(head);
			};break; 
			case 8:
			{
				printf("排序前:"); 
				showNode(head);//遍历节点
				SListNodeSort(head);
    			printf("排序后:");
				showNode(head);  
			};break; 
			case 9:
			{
				head=SListNodeReverse(head);
				showNode(head);
			};break; 
			case 10:
			{
				printf("请输入你要插入升序后列表的x值:");
				scanf("%d",&x);
				SListNodeInsertPro(head,x);
				showNode(head); 
			};break; 
			case 11:
			system("cls");
			break; 
			case 12:
			{
				menu();
			 } break;
			default:
			return 0;
			break;
			
		}
	}	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言链表是一种常用的数据结构,用于存储和操作数据。下面是一个简单的示例,展示了如何在C语言使用链表: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构 struct Node { int data; struct Node* next; }; // 在链表末尾插入新节点 void insert(struct Node** head, int data) { // 创建新节点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; // 如果链表为空,将新节点作为头节点 if (*head == NULL) { *head = newNode; return; } // 找到链表末尾的节点 struct Node* current = *head; while (current->next != NULL) { current = current->next; } // 将新节点插入到末尾 current->next = newNode; } // 打印链表的所有节点 void printList(struct Node* head) { struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } // 释放链表内存 void freeList(struct Node* head) { struct Node* current = head; while (current != NULL) { struct Node* temp = current; current = current->next; free(temp); } } int main() { // 创建一个空链表 struct Node* head = NULL; // 在链表末尾插入节点 insert(&head, 1); insert(&head, 2); insert(&head, 3); // 打印链表 printf("链表的节点:"); printList(head); // 释放链表内存 freeList(head); return 0; } ``` 这个示例演示了如何定义链表节点结构,插入新节点,打印链表的所有节点,并释放链表内存。你可以根据自己的需求修改和扩展这个示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值