单链表插入(完整版程序 在表头和表尾插入算法)
来源:互联网 发布:三菱plc伺服编程实例 编辑:程序博客网 时间:2024/06/08 09:43
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#include<string.h>#define N 3typedef struct node{ char name[20]; struct node *next;}ListNode;ListNode *creat(int n){ ListNode *p,*h,*s; int i; if((h=(ListNode*)malloc(sizeof(ListNode)))==NULL) { printf("不能分配内存空间!"); exit(0); } h->name[0] = '\0'; h->next=NULL; p = h; for(i = 0;i<n;i++) {if((s=(ListNode*)malloc(sizeof(ListNode)))==NULL) { printf("不能分配内存空间!"); exit(0); }p->next = s;printf("请输入第%d个人的名字",i+1);scanf("%s",s->name);s->next = NULL;p = s; } return(h);}void printList(ListNode *p){ while(p->next){ p = p->next; printf("%s\n",p->name);}}void reverse(ListNode *p){ ListNode *a,*b,*c; a = p; b = p->next; while(b->next!=NULL) { c=b->next; b->next = a; a = b; b=c; } b->next = a; p->next->next = NULL; p->next=b;}void find(ListNode* head,char *t){ ListNode *p,*s; p =head; while((p->next)&&(strcmp(p->next->name,t))) p = p->next; if(p->next) { printf("找到了!哦耶\n"); } else { printf("没找到,大哭!\n"); }}void Insert(ListNode* head){char t[10];ListNode *p,*s;int i,j;printf("请输入要插入的字符串:");scanf("%s",&t);p = head;j = 0;while(p->next&&strcmp(t,p->next->name)>0){ j++; p = p->next;}if(!strcmp(t,p->next->name)) printf("重复插入,不允许。\n");else{ s = (ListNode*)malloc(sizeof(ListNode)); strcpy(s->name,t); s->next = p->next; p->next = s;}}void InsertAtTail(ListNode *pHead){ListNode *pNew;if((pNew=(ListNode*)malloc(sizeof(ListNode)))==NULL) { printf("不能分配内存空间!"); exit(0); }//pNew->name = value;printf("请输入要插入的字符串:");scanf("%s",pNew->name);while(pHead->next){ pHead = pHead->next;}pHead->next = pNew;pNew->next = NULL;}void InsertAtHead(ListNode *pHead){ListNode *pNew;if((pNew=(ListNode*)malloc(sizeof(ListNode)))==NULL) { printf("不能分配内存空间!"); exit(0); }//pNew->name = value;printf("请输入要插入的字符串:");scanf("%s",pNew->name);pNew->next = pHead->next;pHead->next = pNew;}void main(){ int number; ListNode *head; char t1[80]; char *t2; number = N; head = creat(number); printf("创建好的链表为:\n"); printList(head); //reverse(head); //printf("单链表逆置之后:\n"); //printList(head); //printf("输入要查找的字符串\n"); // gets(t1); //scanf("%s",&t1); //find(head,t1); //Insert(head); //printList(head); InsertAtTail(head); printList(head); InsertAtHead(head); printList(head);}
这个程序是考虑存在头结点的情况下。
0 0
- 单链表插入(完整版程序 在表头和表尾插入算法)
- 在GridView中插入表头表尾
- 在表头插入生成单链表
- 顺序表及基本运算 && 在表头插入生成单链表 && 在表尾插入生成单链表
- 10.2在表头插入生成单链表
- 单链表的插入(完整版程序c语言实现,以字符串为数据)
- 在表头插入新的结点
- 用链表实现的栈(单向链表,表头插入和删除)
- 回头看看 链表的插入只分两种 表头插入和其他
- 表头插入法建立链表
- Excel插入斜线表头
- 算法-排序-插入排序(直接插入和希尔排序)
- 在表尾插入生成单链表
- 10.3在表尾插入生成单链表
- 线性表的插入和创建(完整程序)
- 在Excel表的每一行之前插入相同内容的表头的方法
- 用C实现链表的初始化,以及在表头插入节点,以及遍历整个列表
- 怎么在单文档中插入表头,List控件
- 国内源安装 nodejs与npm
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
- jquery选择器
- --获取约束信息
- js判断节点是否存在
- 单链表插入(完整版程序 在表头和表尾插入算法)
- LruCache
- Java的集合类型的继承关系图与Java的I/O流类图的继承图
- Eclipse常用快捷键
- 决策树decision tree分析
- 关于mybatis增加缓存引入的坑
- A/B Test测试完全指南
- centos minimal虚拟机测试javaee配置
- leetcode 25. Reverse Nodes in k-Group