数据结构第二天链表

 头文件部分
     1	#ifndef __HEAD_H__
     2	#define __HEAD_H__
     3	#include 
     4	#include 
     5	#include 
     6	typedef int dataType;
     7	typedef struct node
     8	{
     9		union{
    10			dataType data;
    11			int len;
    12		}  txt;
    13		struct node* next;
    14	} seq;
    15	struct node *create_link();
    16	void insert_LinFront_tail(struct node *head);
    17	void insert_LinkFront_head(seq*head);
    18	void show_linklist(seq *p);
    19	
    20	void delect_linkfount_head(struct node *head);
    21	void delect_linkfount_tail(seq* head);
    22	void insert_linkfront_position(struct node *head);
    23	void lookup_linkfront(seq *p);
    24	void delect_linkfront_position(seq*head);
    25	void insert_sort(struct node* head);
    26	void Flip_list(seq *head);
    27	#endif
    函数部分
    28	#include "03_head.h"
    29	
    30	
    31	
    32	
    33	struct node* create_link()
    34	{
    35		struct node *head = (struct node*)malloc(sizeof(struct node));
    36		memset(head,0,sizeof(struct node));
    37		if(NULL == head)
    38		{
    39			printf("创建头结点失败\n");
    40			return NULL;
    41		}
    42		head->txt.len = 0;
    43		head->next = NULL;
    44		printf("创建成功\n");
    45	
    46		return head;
    47	}
    48	
    49	
    50	void insert_LinkFront_head(struct node *head)
    51	{
    52		printf("请输入数据\n");
    53		int data;
    54		scanf("%d",&data);
    55		seq *temp = (seq*)malloc(sizeof(seq));
    56		if(NULL == temp)
    57		{
    58			printf("创建失败,插入数据失败\n");
    59			return;
    60		}
    61		temp->next = NULL;
    62		temp->txt.data = data;
    63		head->txt.len++;
    64	
    65		temp->next = head->next;
    66		head->next = temp;
    67	}
    68	void insert_LinFront_tail(struct node *head)
    69	{
    70	
    71		printf("请输入数据\n");
    72		int data;
    73		scanf("%d",&data);
    74		struct node *temp = (seq*)malloc(sizeof(seq));
    75		struct node *list = head;
    76		if(NULL == temp)
    77		{
    78			printf("创建失败,插入数据失败\n");
    79			return;
    80		}
    81		temp->next = NULL;
    82		temp->txt.data = data;
    83		printf("%d\n",temp->txt.data);
    84		while(list->next != NULL)
    85		{
    86			list = list->next;
    87		}
    88		list->next=temp;
    89		head->txt.len++;
    90		return;
    91	}
    92	void insert_linkfront_position(struct node *head)
    93	{
    94		printf("请输入数据\n");
    95		int select;
    96		scanf("%d",&select);
    97		struct node *temp=(seq*)malloc(sizeof(seq));
    98		struct node *list = head;
    99		temp->next = NULL;
   100		temp->txt.data = select;
   101		printf("请输入要插入的位置\n");
   102		int a;
   103		scanf("%d",&a);
   104		if(a < 0 || a > head->txt.len)
   105		{
   106			printf("位置不合法\n");
   107			return;
   108		}
   109		for(int i=1;inext;
   112		}
   113		temp->next=list->next;
   114		list->next = temp;
   115		head->txt.len++;
   116		return;
   117	}
   118	void lookup_linkfront(seq *p)
   119	{
   120		printf("请输入位置\n");
   121		int select;
   122		scanf("%d",&select);
   123		seq *list = p;
   124		for(int i=0;inext;
   127		}
   128		printf("你想要的数据为::%d\n",list->txt.data);
   129		return;
   130	}
   131	void delect_linkfount_head(struct node *head)
   132	{
   133		struct node* temp = head->next;
   134		head->next = head->next->next;
   135		free(temp);
   136		head->txt.len--;
   137		return;
   138	}
   139	void delect_linkfount_tail(seq* head)
   140	{
   141		struct node *temp = head;
   142		while(temp->next->next != NULL)
   143		{
   144			temp = temp->next;
   145		}
   146		free(temp->next);
   147		temp->next = NULL;
   148		head->txt.len--;
   149		return;
   150	}
   151	void delect_linkfront_position(seq*head)
   152	{
   153		struct node *list = head;
   154		struct node *temp = head;
   155		printf("请输入要删除的位置\n");
   156		int select=0;
   157		scanf("%d",&select);
   158		for(int i=0;inext;
   161		}
   162		temp = list->next;
   163		list->next=temp->next;
   164		free(temp);
   165		head->txt.len--;
   166	}
   167	void show_linklist(seq *head)
   168	{
   169		if(NULL == head)
   170		{
   171			printf("链表错误\n");
   172			return;
   173		}
   174		int j=0;
   175		struct node *p = head->next;
   176		while(jtxt.len)
   177		{
   178			printf("%d ",p->txt.data);
   179			p = p->next;
   180			j++;
   181		}
   182		putchar(10);
   183		return;
   184	}
   185	void insert_sort(struct node* head)
   186	{
   187		printf("请输入数据\n");
   188		int select;
   189		scanf("%d",&select);
   190		struct node *temp = (seq*)malloc(sizeof(seq));
   191		temp->txt.data = select;
   192		seq *list = head;
   193		head->txt.len++;
   194		for(int i=0;itxt.len-1;i++)
   195		{
   196			if(head->next == NULL)
   197			{
   198				break;
   199			}
   200			if(list->next->txt.data < select)
   201			{
   202				list = list->next;
   203			}
   204		}
   205		temp->next = list->next;
   206		list->next = temp;
   207		int j=0;
   208		struct node *p = head->next;
   209		while(jtxt.len)
   210		{
   211			printf("%d ",p->txt.data);
   212			p = p->next;
   213			j++;
   214		}
   215		putchar(10);
   216		return;
   217	}
   218	void Flip_list(seq *head)
   219	{
   220		seq *p1 = NULL;
   221		seq *p2 = NULL;
   222		p1 = head->next;
   223		p2 = p1->next;
   224		while(p2 != NULL)
   225		{
   226			p1->next = p2->next;
   227			p2->next = head->next;
   228			head->next = p2;
   229			p2 = p1->next;
   230		}
   231		int j=0;
   232		struct node *p = head->next;
   233		while(jtxt.len)
   234		{
   235			printf("%d ",p->txt.data);
   236			p = p->next;
   237			j++;
   238		}
   239		putchar(10);
   240		return;
   241	
   242	
   243	}
   244	
  main主文件部分
   245	#include "03_head.h"
   246	int main(int argc, const char *argv[])
   247	{
   248		struct node *list = create_link();
   249		int select = 0;
   250		int a=0,b=0,c=0,d=0;
   251		while(1)
   252		{
   253			printf("插入:::1\n");
   254			printf("查询:::2\n");
   255			printf("删除:::3\n");
   256			printf("遍历:::4\n");
   257			printf("倒转:::5\n");
   258			printf("退出:::6\n");
   259			printf("请输入::::::");
   260			scanf("%d",&select);
   261			switch(select)
   262			{
   263			case 1:
   264				insert_sort(list);
   265				break;
   266			case 2:
   267					lookup_linkfront(list);
   268					break;
   269			case 3:
   270				printf("1\n");
   271				printf("2\n");
   272				printf("3\n");
   273				scanf("%d",&d);
   274				switch(d)
   275				{
   276				case 1:
   277	
   278					delect_linkfount_head(list);
   279					break;
   280				case 2:
   281					delect_linkfount_tail(list);
   282					break;
   283				case 3:
   284					delect_linkfront_position(list);
   285				}
   286				break;
   287			case 4:
   288				show_linklist(list);
   289				break;
   290			case 5:
   291				Flip_list(list);
   292				break;
   293			case 6:
   294				goto Founce;
   295			}
   296		}
   297	Founce:
   298		return 0;
   299	}

你可能感兴趣的:(数据结构,链表)