码迷,mamicode.com
首页 > 编程语言 > 详细

c语言数据结构---动态链表操作

时间:2020-02-15 15:15:57      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:head   linked   lib   splay   upd   struct   highlight   创建   lis   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define LEN sizeof(LinkedList);

typedef struct Student{
	int score;
	char name[10];
	struct Student *next;
}LinkedList;
/**
*链表的创建,n为创建的节点的个数
**/
LinkedList * create(int n){
	LinkedList *head,*node,*end;
	head = (LinkedList*)malloc(sizeof(LinkedList));
	end = head;
	char name[10];
	for(int i=0;i<n;i++){
		node = (LinkedList*)malloc(sizeof(LinkedList));
		printf("enter student%d‘s name:\n",i+1);
		scanf("%s",name);
		strcpy(node->name,name);
		printf("enter student%d‘s score:\n",i+1);
		scanf("%d",&node->score);
		end->next = node;
		end = node;
	}
	end->next=NULL;
	return head;
}
/**
*链表数据插入,index为第index个节点,node为要插入的节点,old为指向原链表的指针
**/
void insert(LinkedList *old,LinkedList *node,int index){
	LinkedList *temp=old;
	int n=0;
	while(n<index&&temp->next!=NULL){
		temp = temp->next;
		n++;
	}
	if(temp != NULL){
		node->next = temp->next;
		temp->next = node;
	}
	else
		puts("index outof range");

}
/*
*链表元素的追加
*/
void append(LinkedList *list,LinkedList *append){
	LinkedList *p;
	p = list;
	while(p->next!=NULL){
		p = p->next;
	}
	p->next = append;
	p = append;
}
/**
*链表的打印展示
**/
void displaylist(LinkedList *link){
	while(link->next!=NULL){
		link = link->next;
		printf("name:%s\tscore:%d\n",link->name,link->score);
	}
}
/**
*链表的删除
**/
void dellist(LinkedList *link,int index){
	LinkedList *t;
	int temp = 0;
	while(temp<index&&link->next!=NULL){
		t = link;
		temp ++;
		link = link->next;
	}
	if(link != NULL){
		t->next = link->next;
		free(link);
	}else{
		puts("index outof range");
	}
}
/**
*链表的更改
**/
void updatelist(LinkedList *link,LinkedList *node,int index){
	LinkedList *t=link,*t1;
	int temp = 0;
	while(temp<index&&t->next!=NULL){
		t1 = t;
		temp ++;
		t = t->next;
	}
	if(t != NULL){
		node->next=t->next;
		t1->next = node;
		free(t);
	}else{
		puts("index outof range");
	}
}
/**
*链表获取指定节点
*head为指向链表表头的指针
*index为要获取的第几个节点
**/
LinkedList getnode(LinkedList *head,int index){
	int i=0;
	LinkedList *p,node;
	p=head;
	while(i<index&&p!=NULL){
		i++;
		p=p->next;
	}
	node = *p;
	return node;
}
/**
*链表数据域的打印
**/
void display(LinkedList node){
	printf("LinkedList:{name:%s,score:%d}\n",node.name,node.score);
}
int main(void){
	int n;
	puts("enter node number:");
	scanf("%d",&n);
	LinkedList *list = create(n);
	displaylist(list);
	puts("after insert a node:");
	LinkedList temp,*node;
	temp.score = 88;
	strcpy(temp.name,"gwb");
	node = &temp;
	insert(list,node,1);
	displaylist(list);
	puts("after append:");
	LinkedList *tp = (LinkedList*)malloc(sizeof(LinkedList));
	tp->score=99;
	strcpy(tp->name,"小明");
	append(list,tp);
	displaylist(list);
	puts("after change:");
	LinkedList *tp1 = (LinkedList*)malloc(sizeof(LinkedList));
	tp1->score=99;
	strcpy(tp1->name,"小芳");
	updatelist(list,tp1,3);
	displaylist(list);
	puts("after delete:");
	dellist(list,2);
	displaylist(list);
	LinkedList nodes = getnode(list,2);
	display(nodes);
	return 0;
}

  

c语言数据结构---动态链表操作

标签:head   linked   lib   splay   upd   struct   highlight   创建   lis   

原文地址:https://www.cnblogs.com/g177w/p/12312063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!